@@ -56,136 +56,24 @@ Examples:
5656 srcc branch -M old-name new-name` ,
5757 Args : cobra .MaximumNArgs (2 ),
5858 RunE : func (cmd * cobra.Command , args []string ) error {
59- // Find repository
6059 repo , err := findRepository ()
6160 if err != nil {
6261 return err
6362 }
6463
65- // Create branch manager
6664 manager := branch .NewManager (repo )
67-
6865 ctx := context .Background ()
6966
70- // Handle rename
71- if renameFlag {
72- var oldName , newName string
73-
74- if len (args ) == 0 {
75- return fmt .Errorf ("new branch name required for rename" )
76- } else if len (args ) == 1 {
77- // Rename current branch
78- currentBranch , err := manager .CurrentBranch ()
79- if err != nil {
80- return fmt .Errorf ("failed to get current branch: %w" , err )
81- }
82- if currentBranch == "" {
83- return fmt .Errorf ("not on any branch (detached HEAD)" )
84- }
85- oldName = currentBranch
86- newName = args [0 ]
87- } else {
88- // Rename specified branch
89- oldName = args [0 ]
90- newName = args [1 ]
91- }
92-
93- opts := []branch.RenameOption {}
94- if forceFlag {
95- opts = append (opts , branch .WithForceRename ())
96- }
97-
98- if err := manager .RenameBranch (ctx , oldName , newName , opts ... ); err != nil {
99- return fmt .Errorf ("failed to rename branch: %w" , err )
100- }
101-
102- fmt .Printf ("Branch %s renamed to %s\n " , oldName , newName )
103- return nil
104- }
105-
106- // Handle delete
107- if deleteFlag {
108- if len (args ) == 0 {
109- return fmt .Errorf ("branch name required for deletion" )
110- }
111- branchName := args [0 ]
112-
113- opts := []branch.DeleteOption {}
114- if forceFlag {
115- opts = append (opts , branch .WithForceDelete ())
116- }
117-
118- if err := manager .DeleteBranch (ctx , branchName , opts ... ); err != nil {
119- return fmt .Errorf ("failed to delete branch: %w" , err )
120- }
121-
122- fmt .Printf ("Deleted branch %s\n " , branchName )
123- return nil
124- }
125-
126- // Handle list (default) or create
127- if len (args ) == 0 || listFlag {
128- // List branches
129- branches , err := manager .ListBranches (ctx )
130- if err != nil {
131- return fmt .Errorf ("failed to list branches: %w" , err )
132- }
133-
134- // Get current branch
135- currentBranch , _ := manager .CurrentBranch ()
136-
137- // Display branches
138- if len (branches ) == 0 {
139- fmt .Println ("No branches found" )
140- return nil
141- }
142-
143- for _ , br := range branches {
144- prefix := " "
145- if br .Name == currentBranch {
146- prefix = "* "
147- }
148-
149- if verboseFlag {
150- // Verbose output with commit info
151- fmt .Printf ("%s%-20s %s %s\n " ,
152- prefix ,
153- br .Name ,
154- br .SHA .Short (),
155- br .LastCommitMessage )
156- } else {
157- // Simple output
158- fmt .Printf ("%s%s\n " , prefix , br .Name )
159- }
160- }
161-
162- return nil
163- }
164-
165- // Create new branch
166- branchName := args [0 ]
167-
168- // Build create options
169- opts := []branch.CreateOption {}
170-
171- // Handle start point from args or flag
172- if len (args ) > 1 {
173- opts = append (opts , branch .WithStartPoint (args [1 ]))
174- } else if startPoint != "" {
175- opts = append (opts , branch .WithStartPoint (startPoint ))
176- }
177-
178- if forceFlag {
179- opts = append (opts , branch .WithForceCreate ())
180- }
181-
182- if _ , err := manager .CreateBranch (ctx , branchName , opts ... ); err != nil {
183- return fmt .Errorf ("failed to create branch: %w" , err )
67+ switch {
68+ case renameFlag :
69+ return renameBranch (ctx , args , manager , forceFlag )
70+ case deleteFlag :
71+ return deleteBranch (ctx , args , manager , forceFlag )
72+ case len (args ) == 0 || listFlag :
73+ return listBranches (ctx , manager , verboseFlag )
74+ default :
75+ return createBranch (ctx , args , manager , startPoint , forceFlag )
18476 }
185-
186- fmt .Printf ("Created branch %s\n " , branchName )
187-
188- return nil
18977 },
19078 }
19179
@@ -195,12 +83,9 @@ Examples:
19583 cmd .Flags ().BoolVarP (& verboseFlag , "verbose" , "v" , false , "Show verbose output with commit info" )
19684 cmd .Flags ().BoolVarP (& forceFlag , "force" , "f" , false , "Force operation (use with -d or -m)" )
19785 cmd .Flags ().StringVar (& startPoint , "start-point" , "" , "Create branch from this commit/branch" )
198-
199- // Add uppercase aliases for force operations
20086 cmd .Flags ().BoolP ("force-delete" , "D" , false , "Force delete a branch (shorthand for -d -f)" )
20187 cmd .Flags ().BoolP ("force-move" , "M" , false , "Force rename a branch (shorthand for -m -f)" )
20288
203- // Handle uppercase flag aliases
20489 cmd .PreRunE = func (cmd * cobra.Command , args []string ) error {
20590 if forceDelete , _ := cmd .Flags ().GetBool ("force-delete" ); forceDelete {
20691 deleteFlag = true
@@ -215,3 +100,109 @@ Examples:
215100
216101 return cmd
217102}
103+
104+ func renameBranch (ctx context.Context , args []string , manager * branch.Manager , force bool ) error {
105+ var oldName , newName string
106+
107+ if len (args ) == 0 {
108+ return fmt .Errorf ("new branch name required for rename" )
109+ } else if len (args ) == 1 {
110+ currentBranch , err := manager .CurrentBranch ()
111+ if err != nil {
112+ return fmt .Errorf ("failed to get current branch: %w" , err )
113+ }
114+ if currentBranch == "" {
115+ return fmt .Errorf ("not on any branch (detached HEAD)" )
116+ }
117+ oldName = currentBranch
118+ newName = args [0 ]
119+ } else {
120+ oldName = args [0 ]
121+ newName = args [1 ]
122+ }
123+
124+ opts := []branch.RenameOption {}
125+ if force {
126+ opts = append (opts , branch .WithForceRename ())
127+ }
128+
129+ if err := manager .RenameBranch (ctx , oldName , newName , opts ... ); err != nil {
130+ return fmt .Errorf ("failed to rename branch: %w" , err )
131+ }
132+
133+ fmt .Printf ("Branch %s renamed to %s\n " , oldName , newName )
134+ return nil
135+ }
136+
137+ func deleteBranch (ctx context.Context , args []string , manager * branch.Manager , force bool ) error {
138+ if len (args ) == 0 {
139+ return fmt .Errorf ("branch name required for deletion" )
140+ }
141+ branchName := args [0 ]
142+
143+ opts := []branch.DeleteOption {}
144+ if force {
145+ opts = append (opts , branch .WithForceDelete ())
146+ }
147+
148+ if err := manager .DeleteBranch (ctx , branchName , opts ... ); err != nil {
149+ return fmt .Errorf ("failed to delete branch: %w" , err )
150+ }
151+
152+ fmt .Printf ("Deleted branch %s\n " , branchName )
153+ return nil
154+ }
155+
156+ func listBranches (ctx context.Context , manager * branch.Manager , verbose bool ) error {
157+ branches , err := manager .ListBranches (ctx )
158+ if err != nil {
159+ return fmt .Errorf ("failed to list branches: %w" , err )
160+ }
161+ currentBranch , _ := manager .CurrentBranch ()
162+
163+ if len (branches ) == 0 {
164+ fmt .Println ("No branches found" )
165+ return nil
166+ }
167+
168+ for _ , br := range branches {
169+ prefix := " "
170+ if br .Name == currentBranch {
171+ prefix = "* "
172+ }
173+
174+ if verbose {
175+ fmt .Printf ("%s%-20s %s %s\n " ,
176+ prefix ,
177+ br .Name ,
178+ br .SHA .Short (),
179+ br .LastCommitMessage )
180+ } else {
181+ fmt .Printf ("%s%s\n " , prefix , br .Name )
182+ }
183+ }
184+
185+ return nil
186+ }
187+
188+ func createBranch (ctx context.Context , args []string , manager * branch.Manager , startPoint string , force bool ) error {
189+ branchName := args [0 ]
190+ opts := []branch.CreateOption {}
191+
192+ if len (args ) > 1 {
193+ opts = append (opts , branch .WithStartPoint (args [1 ]))
194+ } else if startPoint != "" {
195+ opts = append (opts , branch .WithStartPoint (startPoint ))
196+ }
197+
198+ if force {
199+ opts = append (opts , branch .WithForceCreate ())
200+ }
201+
202+ if _ , err := manager .CreateBranch (ctx , branchName , opts ... ); err != nil {
203+ return fmt .Errorf ("failed to create branch: %w" , err )
204+ }
205+
206+ fmt .Printf ("Created branch %s\n " , branchName )
207+ return nil
208+ }
0 commit comments