@@ -4,8 +4,83 @@ import (
44 "fmt"
55 "strings"
66 "time"
7+ "unicode/utf8"
78)
89
10+ const (
11+ DefaultWorkBranchPrefix = "feature/"
12+ WorkBranchNamingPrefix = "prefix"
13+ WorkBranchNamingAI = "ai"
14+ )
15+
16+ // WorkBranchNaming controls how a repository names newly generated work
17+ // branches. An absent setting uses the legacy prefix. In prefix mode an empty
18+ // Prefix deliberately produces a branch with no prefix.
19+ type WorkBranchNaming struct {
20+ Mode string `json:"mode"`
21+ Prefix string `json:"prefix,omitempty"`
22+ ExecutorID string `json:"executorId,omitempty"`
23+ Model string `json:"model,omitempty"`
24+ Prompt string `json:"prompt,omitempty"`
25+ }
26+
27+ func (n WorkBranchNaming ) Normalize () (WorkBranchNaming , error ) {
28+ switch n .Mode {
29+ case WorkBranchNamingPrefix :
30+ n .Prefix = strings .TrimSpace (n .Prefix )
31+ n .ExecutorID , n .Model , n .Prompt = "" , "" , ""
32+ if ! ValidNewWorkBranchName (n .Prefix + "work-branch" ) {
33+ return WorkBranchNaming {}, fmt .Errorf ("工作分支前缀不能生成合法的 Git 分支名" )
34+ }
35+ case WorkBranchNamingAI :
36+ n .ExecutorID = strings .TrimSpace (n .ExecutorID )
37+ n .Model = strings .TrimSpace (n .Model )
38+ n .Prompt = strings .TrimSpace (n .Prompt )
39+ n .Prefix = ""
40+ if n .ExecutorID == "" || n .Model == "" || n .Model == "default" || n .Model == "default-model" {
41+ return WorkBranchNaming {}, fmt .Errorf ("请在仓库设置中为 AI 分支命名选择 CLI 和具体模型" )
42+ }
43+ if strings .HasPrefix (n .Model , "-" ) {
44+ return WorkBranchNaming {}, fmt .Errorf ("模型 ID 不能以 - 开头" )
45+ }
46+ if err := ValidateCLIModelID (n .Model ); err != nil {
47+ return WorkBranchNaming {}, err
48+ }
49+ if utf8 .RuneCountInString (n .Prompt ) > 2000 {
50+ return WorkBranchNaming {}, fmt .Errorf ("分支命名要求最多 2000 个字符" )
51+ }
52+ default :
53+ return WorkBranchNaming {}, fmt .Errorf ("工作分支命名方式必须是 prefix 或 ai" )
54+ }
55+ return n , nil
56+ }
57+
58+ // ValidGitBranchName accepts literal branch names, excluding revisions and
59+ // values that could be interpreted as command options.
60+ func ValidGitBranchName (branch string ) bool {
61+ if branch == "" || branch == "HEAD" || strings .HasPrefix (branch , "-" ) ||
62+ strings .HasSuffix (branch , "." ) || strings .Contains (branch , ".." ) ||
63+ strings .Contains (branch , "@{" ) || strings .ContainsAny (branch , "~^:?*[\\ " ) {
64+ return false
65+ }
66+ for _ , r := range branch {
67+ if r <= ' ' || r == 127 {
68+ return false
69+ }
70+ }
71+ for _ , component := range strings .Split (branch , "/" ) {
72+ if component == "" || strings .HasPrefix (component , "." ) || strings .HasSuffix (component , ".lock" ) {
73+ return false
74+ }
75+ }
76+ return true
77+ }
78+
79+ // ValidNewWorkBranchName expects a short name suitable for a checkout.
80+ func ValidNewWorkBranchName (branch string ) bool {
81+ return ValidGitBranchName (branch ) && branch != "@" && ! strings .HasPrefix (branch , "refs/heads/" )
82+ }
83+
984// DefaultWorkBranch names the git work branch for an issue run. Gongfeng issues
1085// use fix-issue-{iid}; TAPD stories use story-{tapdId}; platform requirements use
1186// requirement-{number}. The suffix is a local timestamp that keeps branch names
@@ -17,26 +92,26 @@ func DefaultWorkBranch(issue Issue, now time.Time) string {
1792
1893 if source == "tapd" || strings .HasPrefix (strings .TrimSpace (issue .ID ), "tapd-" ) {
1994 if id := tapdStoryID (issue ); id != "" {
20- return "feature/ story-" + SafeGitRefSegment (id ) + "-" + suffix
95+ return DefaultWorkBranchPrefix + " story-" + SafeGitRefSegment (id ) + "-" + suffix
2196 }
2297 }
2398 if source == "gongfeng" || kind == "issue" {
2499 if n := gongfengIssueNumber (issue ); n > 0 {
25- return fmt .Sprintf ("feature/fix -issue-%d-%s" , n , suffix )
100+ return fmt .Sprintf ("%sfix -issue-%d-%s" , DefaultWorkBranchPrefix , n , suffix )
26101 }
27102 }
28103 if kind == "requirement" || source == "" || source == "local" {
29104 if issue .Number > 0 {
30- return fmt .Sprintf ("feature/requirement -%d-%s" , issue .Number , suffix )
105+ return fmt .Sprintf ("%srequirement -%d-%s" , DefaultWorkBranchPrefix , issue .Number , suffix )
31106 }
32107 }
33108 if issue .Number > 0 {
34- return fmt .Sprintf ("feature/fix -issue-%d-%s" , issue .Number , suffix )
109+ return fmt .Sprintf ("%sfix -issue-%d-%s" , DefaultWorkBranchPrefix , issue .Number , suffix )
35110 }
36111 if issue .ID != "" {
37- return "feature/" + SafeGitRefSegment (issue .ID ) + "-" + suffix
112+ return DefaultWorkBranchPrefix + SafeGitRefSegment (issue .ID ) + "-" + suffix
38113 }
39- return "feature/ fix-" + suffix
114+ return DefaultWorkBranchPrefix + " fix-" + suffix
40115}
41116
42117func gongfengIssueNumber (issue Issue ) int {
0 commit comments