@@ -20,12 +20,20 @@ func NewBranchCommands(gitCommon *GitCommon) *BranchCommands {
2020
2121// New creates a new branch
2222func (self * BranchCommands ) New (name string , base string ) error {
23- return self .cmd .New (fmt .Sprintf ("git checkout -b %s %s" , self .cmd .Quote (name ), self .cmd .Quote (base ))).Run ()
23+ cmdStr := NewGitCmd ("checkout" ).
24+ Arg ("-b" , self .cmd .Quote (name ), self .cmd .Quote (base )).
25+ ToString ()
26+
27+ return self .cmd .New (cmdStr ).Run ()
2428}
2529
2630// CurrentBranchInfo get the current branch information.
2731func (self * BranchCommands ) CurrentBranchInfo () (BranchInfo , error ) {
28- branchName , err := self .cmd .New ("git symbolic-ref --short HEAD" ).DontLog ().RunWithOutput ()
32+ branchName , err := self .cmd .New (
33+ NewGitCmd ("symbolic-ref" ).
34+ Arg ("--short" , "HEAD" ).
35+ ToString (),
36+ ).DontLog ().RunWithOutput ()
2937 if err == nil && branchName != "HEAD\n " {
3038 trimmedBranchName := strings .TrimSpace (branchName )
3139 return BranchInfo {
@@ -34,7 +42,11 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
3442 DetachedHead : false ,
3543 }, nil
3644 }
37- output , err := self .cmd .New (`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"` ).DontLog ().RunWithOutput ()
45+ output , err := self .cmd .New (
46+ NewGitCmd ("branch" ).
47+ Arg ("--points-at=HEAD" , "--format=\" %(HEAD)%00%(objectname)%00%(refname)\" " ).
48+ ToString (),
49+ ).DontLog ().RunWithOutput ()
3850 if err != nil {
3951 return BranchInfo {}, err
4052 }
@@ -59,13 +71,12 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) {
5971
6072// Delete delete branch
6173func (self * BranchCommands ) Delete (branch string , force bool ) error {
62- command := "git branch -d"
63-
64- if force {
65- command = "git branch -D"
66- }
74+ cmdStr := NewGitCmd ("branch" ).
75+ ArgIfElse (force , "-D" , "-d" ).
76+ Arg (self .cmd .Quote (branch )).
77+ ToString ()
6778
68- return self .cmd .New (fmt . Sprintf ( "%s %s" , command , self . cmd . Quote ( branch )) ).Run ()
79+ return self .cmd .New (cmdStr ).Run ()
6980}
7081
7182// Checkout checks out a branch (or commit), with --force if you set the force arg to true
@@ -75,12 +86,12 @@ type CheckoutOptions struct {
7586}
7687
7788func (self * BranchCommands ) Checkout (branch string , options CheckoutOptions ) error {
78- forceArg := ""
79- if options .Force {
80- forceArg = " --force"
81- }
89+ cmdStr := NewGitCmd ( "checkout" ).
90+ ArgIf ( options .Force , "--force" ).
91+ Arg ( self . cmd . Quote ( branch )).
92+ ToString ()
8293
83- return self .cmd .New (fmt . Sprintf ( "git checkout%s %s" , forceArg , self . cmd . Quote ( branch )) ).
94+ return self .cmd .New (cmdStr ).
8495 // prevents git from prompting us for input which would freeze the program
8596 // TODO: see if this is actually needed here
8697 AddEnvVars ("GIT_TERMINAL_PROMPT=0" ).
@@ -104,15 +115,27 @@ func (self *BranchCommands) GetGraphCmdObj(branchName string) oscommands.ICmdObj
104115}
105116
106117func (self * BranchCommands ) SetCurrentBranchUpstream (remoteName string , remoteBranchName string ) error {
107- return self .cmd .New (fmt .Sprintf ("git branch --set-upstream-to=%s/%s" , self .cmd .Quote (remoteName ), self .cmd .Quote (remoteBranchName ))).Run ()
118+ cmdStr := NewGitCmd ("branch" ).
119+ Arg (fmt .Sprintf ("--set-upstream-to=%s/%s" , self .cmd .Quote (remoteName ), self .cmd .Quote (remoteBranchName ))).
120+ ToString ()
121+
122+ return self .cmd .New (cmdStr ).Run ()
108123}
109124
110125func (self * BranchCommands ) SetUpstream (remoteName string , remoteBranchName string , branchName string ) error {
111- return self .cmd .New (fmt .Sprintf ("git branch --set-upstream-to=%s/%s %s" , self .cmd .Quote (remoteName ), self .cmd .Quote (remoteBranchName ), self .cmd .Quote (branchName ))).Run ()
126+ cmdStr := NewGitCmd ("branch" ).
127+ Arg (fmt .Sprintf ("--set-upstream-to=%s/%s" , self .cmd .Quote (remoteName ), self .cmd .Quote (remoteBranchName ))).
128+ Arg (self .cmd .Quote (branchName )).
129+ ToString ()
130+
131+ return self .cmd .New (cmdStr ).Run ()
112132}
113133
114134func (self * BranchCommands ) UnsetUpstream (branchName string ) error {
115- return self .cmd .New (fmt .Sprintf ("git branch --unset-upstream %s" , self .cmd .Quote (branchName ))).Run ()
135+ cmdStr := NewGitCmd ("branch" ).Arg ("--unset-upstream" , self .cmd .Quote (branchName )).
136+ ToString ()
137+
138+ return self .cmd .New (cmdStr ).Run ()
116139}
117140
118141func (self * BranchCommands ) GetCurrentBranchUpstreamDifferenceCount () (string , string ) {
@@ -126,45 +149,62 @@ func (self *BranchCommands) GetUpstreamDifferenceCount(branchName string) (strin
126149// GetCommitDifferences checks how many pushables/pullables there are for the
127150// current branch
128151func (self * BranchCommands ) GetCommitDifferences (from , to string ) (string , string ) {
129- command := "git rev-list %s..%s --count"
130- pushableCount , err := self .cmd .New (fmt .Sprintf (command , to , from )).DontLog ().RunWithOutput ()
152+ pushableCount , err := self .countDifferences (to , from )
131153 if err != nil {
132154 return "?" , "?"
133155 }
134- pullableCount , err := self .cmd . New ( fmt . Sprintf ( command , from , to )). DontLog (). RunWithOutput ( )
156+ pullableCount , err := self .countDifferences ( from , to )
135157 if err != nil {
136158 return "?" , "?"
137159 }
138160 return strings .TrimSpace (pushableCount ), strings .TrimSpace (pullableCount )
139161}
140162
163+ func (self * BranchCommands ) countDifferences (from , to string ) (string , error ) {
164+ cmdStr := NewGitCmd ("rev-list" ).
165+ Arg (fmt .Sprintf ("%s..%s" , from , to )).
166+ Arg ("--count" ).
167+ ToString ()
168+
169+ return self .cmd .New (cmdStr ).DontLog ().RunWithOutput ()
170+ }
171+
141172func (self * BranchCommands ) IsHeadDetached () bool {
142- err := self .cmd .New ("git symbolic-ref -q HEAD" ).DontLog ().Run ()
173+ cmdStr := NewGitCmd ("symbolic-ref" ).Arg ("-q" , "HEAD" ).ToString ()
174+
175+ err := self .cmd .New (cmdStr ).DontLog ().Run ()
143176 return err != nil
144177}
145178
146179func (self * BranchCommands ) Rename (oldName string , newName string ) error {
147- return self .cmd .New (fmt .Sprintf ("git branch --move %s %s" , self .cmd .Quote (oldName ), self .cmd .Quote (newName ))).Run ()
180+ cmdStr := NewGitCmd ("branch" ).
181+ Arg ("--move" , self .cmd .Quote (oldName ), self .cmd .Quote (newName )).
182+ ToString ()
183+
184+ return self .cmd .New (cmdStr ).Run ()
148185}
149186
150187func (self * BranchCommands ) GetRawBranches () (string , error ) {
151- return self .cmd .New (`git for-each-ref --sort=-committerdate --format="%(HEAD)%00%(refname:short)%00%(upstream:short)%00%(upstream:track)" refs/heads` ).DontLog ().RunWithOutput ()
188+ cmdStr := NewGitCmd ("for-each-ref" ).
189+ Arg ("--sort=-committerdate" ).
190+ Arg (`--format="%(HEAD)%00%(refname:short)%00%(upstream:short)%00%(upstream:track)"` ).
191+ Arg ("refs/heads" ).
192+ ToString ()
193+
194+ return self .cmd .New (cmdStr ).DontLog ().RunWithOutput ()
152195}
153196
154197type MergeOpts struct {
155198 FastForwardOnly bool
156199}
157200
158201func (self * BranchCommands ) Merge (branchName string , opts MergeOpts ) error {
159- mergeArg := ""
160- if self .UserConfig .Git .Merging .Args != "" {
161- mergeArg = " " + self .UserConfig .Git .Merging .Args
162- }
163-
164- command := fmt .Sprintf ("git merge --no-edit%s %s" , mergeArg , self .cmd .Quote (branchName ))
165- if opts .FastForwardOnly {
166- command = fmt .Sprintf ("%s --ff-only" , command )
167- }
202+ command := NewGitCmd ("merge" ).
203+ Arg ("--no-edit" ).
204+ ArgIf (self .UserConfig .Git .Merging .Args != "" , self .UserConfig .Git .Merging .Args ).
205+ ArgIf (opts .FastForwardOnly , "--ff-only" ).
206+ Arg (self .cmd .Quote (branchName )).
207+ ToString ()
168208
169209 return self .cmd .New (command ).Run ()
170210}
0 commit comments