Skip to content

Commit 25f8b03

Browse files
committed
Add convenience builder for git commands
1 parent 63ddc52 commit 25f8b03

22 files changed

+713
-388
lines changed

pkg/commands/git_commands/bisect.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git_commands
22

33
import (
4-
"fmt"
54
"os"
65
"path/filepath"
76
"strings"
@@ -98,13 +97,15 @@ func (self *BisectCommands) GetInfo() *BisectInfo {
9897
}
9998

10099
func (self *BisectCommands) Reset() error {
101-
return self.cmd.New("git bisect reset").StreamOutput().Run()
100+
cmdStr := NewGitCmd("bisect").Arg("reset").ToString()
101+
102+
return self.cmd.New(cmdStr).StreamOutput().Run()
102103
}
103104

104105
func (self *BisectCommands) Mark(ref string, term string) error {
105-
return self.cmd.New(
106-
fmt.Sprintf("git bisect %s %s", term, ref),
107-
).
106+
cmdStr := NewGitCmd("bisect").Arg(term, ref).ToString()
107+
108+
return self.cmd.New(cmdStr).
108109
IgnoreEmptyError().
109110
StreamOutput().
110111
Run()
@@ -115,7 +116,9 @@ func (self *BisectCommands) Skip(ref string) error {
115116
}
116117

117118
func (self *BisectCommands) Start() error {
118-
return self.cmd.New("git bisect start").StreamOutput().Run()
119+
cmdStr := NewGitCmd("bisect").Arg("start").ToString()
120+
121+
return self.cmd.New(cmdStr).StreamOutput().Run()
119122
}
120123

121124
// tells us whether we've found our problem commit(s). We return a string slice of
@@ -137,7 +140,8 @@ func (self *BisectCommands) IsDone() (bool, []string, error) {
137140
done := false
138141
candidates := []string{}
139142

140-
err := self.cmd.New(fmt.Sprintf("git rev-list %s", newSha)).RunAndProcessLines(func(line string) (bool, error) {
143+
cmdStr := NewGitCmd("rev-list").Arg(newSha).ToString()
144+
err := self.cmd.New(cmdStr).RunAndProcessLines(func(line string) (bool, error) {
141145
sha := strings.TrimSpace(line)
142146

143147
if status, ok := info.statusMap[sha]; ok {
@@ -167,9 +171,11 @@ func (self *BisectCommands) IsDone() (bool, []string, error) {
167171
// bisecting is actually a descendant of our current bisect commit. If it's not, we need to
168172
// render the commits from the bad commit.
169173
func (self *BisectCommands) ReachableFromStart(bisectInfo *BisectInfo) bool {
170-
err := self.cmd.New(
171-
fmt.Sprintf("git merge-base --is-ancestor %s %s", bisectInfo.GetNewSha(), bisectInfo.GetStartSha()),
172-
).DontLog().Run()
174+
cmdStr := NewGitCmd("merge-base").
175+
Arg("--is-ancestor", bisectInfo.GetNewSha(), bisectInfo.GetStartSha()).
176+
ToString()
177+
178+
err := self.cmd.New(cmdStr).DontLog().Run()
173179

174180
return err == nil
175181
}

pkg/commands/git_commands/branch.go

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@ func NewBranchCommands(gitCommon *GitCommon) *BranchCommands {
2020

2121
// New creates a new branch
2222
func (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.
2731
func (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
6173
func (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

7788
func (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

106117
func (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

110125
func (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

114134
func (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

118141
func (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
128151
func (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+
141172
func (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

146179
func (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

150187
func (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

154197
type MergeOpts struct {
155198
FastForwardOnly bool
156199
}
157200

158201
func (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

Comments
 (0)