diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-05-20 20:58:18 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-20 20:58:18 +1000 |
commit | b30ec538fb59ea0a0bb6a8a65456d433f3989075 (patch) | |
tree | ca3b42cbf95b58fc3dd05a2cff1434aef422eaae | |
parent | b07c4fc0019e6432d981127019c938dcefcf808a (diff) | |
parent | ee11046d354e167abd6b6b3b6f6fa7157ea67a31 (diff) |
Merge pull request #2645 from jesseduffield/convenient-git-command-building
32 files changed, 1036 insertions, 529 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 4facd4f24..95e46086f 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -117,8 +117,7 @@ func NewGitCommandAux( workingTreeCommands := git_commands.NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader) rebaseCommands := git_commands.NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands) stashCommands := git_commands.NewStashCommands(gitCommon, fileLoader, workingTreeCommands) - // TODO: have patch builder take workingTreeCommands in its entirety - patchBuilder := patch.NewPatchBuilder(cmn.Log, workingTreeCommands.ApplyPatch, + patchBuilder := patch.NewPatchBuilder(cmn.Log, func(from string, to string, reverse bool, filename string, plain bool) (string, error) { // TODO: make patch builder take Gui.IgnoreWhitespaceInDiffView into // account. For now we just pass false. diff --git a/pkg/commands/git_commands/bisect.go b/pkg/commands/git_commands/bisect.go index b09a50bf5..898151d9c 100644 --- a/pkg/commands/git_commands/bisect.go +++ b/pkg/commands/git_commands/bisect.go @@ -1,7 +1,6 @@ package git_commands import ( - "fmt" "os" "path/filepath" "strings" @@ -98,13 +97,15 @@ func (self *BisectCommands) GetInfo() *BisectInfo { } func (self *BisectCommands) Reset() error { - return self.cmd.New("git bisect reset").StreamOutput().Run() + cmdStr := NewGitCmd("bisect").Arg("reset").ToString() + + return self.cmd.New(cmdStr).StreamOutput().Run() } func (self *BisectCommands) Mark(ref string, term string) error { - return self.cmd.New( - fmt.Sprintf("git bisect %s %s", term, ref), - ). + cmdStr := NewGitCmd("bisect").Arg(term, ref).ToString() + + return self.cmd.New(cmdStr). IgnoreEmptyError(). StreamOutput(). Run() @@ -115,7 +116,9 @@ func (self *BisectCommands) Skip(ref string) error { } func (self *BisectCommands) Start() error { - return self.cmd.New("git bisect start").StreamOutput().Run() + cmdStr := NewGitCmd("bisect").Arg("start").ToString() + + return self.cmd.New(cmdStr).StreamOutput().Run() } // 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) { done := false candidates := []string{} - err := self.cmd.New(fmt.Sprintf("git rev-list %s", newSha)).RunAndProcessLines(func(line string) (bool, error) { + cmdStr := NewGitCmd("rev-list").Arg(newSha).ToString() + err := self.cmd.New(cmdStr).RunAndProcessLines(func(line string) (bool, error) { sha := strings.TrimSpace(line) if status, ok := info.statusMap[sha]; ok { @@ -167,9 +171,11 @@ func (self *BisectCommands) IsDone() (bool, []string, error) { // bisecting is actually a descendant of our current bisect commit. If it's not, we need to // render the commits from the bad commit. func (self *BisectCommands) ReachableFromStart(bisectInfo *BisectInfo) bool { - err := self.cmd.New( - fmt.Sprintf("git merge-base --is-ancestor %s %s", bisectInfo.GetNewSha(), bisectInfo.GetStartSha()), - ).DontLog().Run() + cmdStr := NewGitCmd("merge-base"). + Arg("--is-ancestor", bisectInfo.GetNewSha(), bisectInfo.GetStartSha()). + ToString() + + err := self.cmd.New(cmdStr).DontLog().Run() return err == nil } diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index a71e365ea..0952e59e1 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -20,12 +20,20 @@ func NewBranchCommands(gitCommon *GitCommon) *BranchCommands { // New creates a new branch func (self *BranchCommands) New(name string, base string) error { - return self.cmd.New(fmt.Sprintf("git checkout -b %s %s", self.cmd.Quote(name), self.cmd.Quote(base))).Run() + cmdStr := NewGitCmd("checkout"). + Arg("-b", self.cmd.Quote(name), self.cmd.Quote(base)). + ToString() + + return self.cmd.New(cmdStr).Run() } // CurrentBranchInfo get the current branch information. func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { - branchName, err := self.cmd.New("git symbolic-ref --short HEAD").DontLog().RunWithOutput() + branchName, err := self.cmd.New( + NewGitCmd("symbolic-ref"). + Arg("--short", "HEAD"). + ToString(), + ).DontLog().RunWithOutput() if err == nil && branchName != "HEAD\n" { trimmedBranchName := strings.TrimSpace(branchName) return BranchInfo{ @@ -34,7 +42,11 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { DetachedHead: false, }, nil } - output, err := self.cmd.New(`git branch --points-at=HEAD --format="%(HEAD)%00%(objectname)%00%(refname)"`).DontLog().RunWithOutput() + output, err := self.cmd.New( + NewGitCmd("branch"). + Arg("--points-at=HEAD", "--format=\"%(HEAD)%00%(objectname)%00%(refname)\""). + ToString(), + ).DontLog().RunWithOutput() if err != nil { return BranchInfo{}, err } @@ -59,13 +71,12 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { // Delete delete branch func (self *BranchCommands) Delete(branch string, force bool) error { - command := "git branch -d" - - if force { - command = "git branch -D" - } + cmdStr := NewGitCmd("branch"). + ArgIfElse(force, "-D", "-d"). + Arg(self.cmd.Quote(branch)). + ToString() - return self.cmd.New(fmt.Sprintf("%s %s", command, self.cmd.Quote(branch))).Run() + return self.cmd.New(cmdStr).Run() } // Checkout checks out a branch (or commit), with --force if you set the force arg to true @@ -75,12 +86,12 @@ type CheckoutOptions struct { } func (self *BranchCommands) Checkout(branch string, options CheckoutOptions) error { - forceArg := "" - if options.Force { - forceArg = " --force" - } + cmdStr := NewGitCmd("checkout"). + ArgIf(options.Force, "--force"). + Arg(self.cmd.Quote(branch)). + ToString() - return self.cmd.New(fmt.Sprintf("git checkout%s %s", forceArg, self.cmd.Quote(branch))). + return self.cmd.New(cmdStr). // prevents git from prompting us for input which would freeze the program // TODO: see if this is actually needed here AddEnvVars("GIT_TERMINAL_PROMPT=0"). @@ -104,15 +115,27 @@ func (self *BranchCommands) GetGraphCmdObj(branchName string) oscommands.ICmdObj } func (self *BranchCommands) SetCurrentBranchUpstream(remoteName string, remoteBranchName string) error { - return self.cmd.New(fmt.Sprintf("git branch --set-upstream-to=%s/%s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName))).Run() + cmdStr := NewGitCmd("branch"). + Arg(fmt.Sprintf("--set-upstream-to=%s/%s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName))). + ToString() + + return self.cmd.New(cmdStr).Run() } func (self *BranchCommands) SetUpstream(remoteName string, remoteBranchName string, branchName string) error { - 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() + cmdStr := NewGitCmd("branch"). + Arg(fmt.Sprintf("--set-upstream-to=%s/%s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName))). + Arg(self.cmd.Quote(branchName)). + ToString() + + return self.cmd.New(cmdStr).Run() } func (self *BranchCommands) UnsetUpstream(branchName string) error { - return self.cmd.New(fmt.Sprintf("git branch --unset-upstream %s", self.cmd.Quote(branchName))).Run() + cmdStr := NewGitCmd("branch").Arg("--unset-upstream", self.cmd.Quote(branchName)). + ToString() + + return self.cmd.New(cmdStr).Run() } func (self *BranchCommands) GetCurrentBranchUpstreamDifferenceCount() (string, string) { @@ -126,29 +149,49 @@ func (self *BranchCommands) GetUpstreamDifferenceCount(branchName string) (strin // GetCommitDifferences checks how many pushables/pullables there are for the // current branch func (self *BranchCommands) GetCommitDifferences(from, to string) (string, string) { - command := "git rev-list %s..%s --count" - pushableCount, err := self.cmd.New(fmt.Sprintf(command, to, from)).DontLog().RunWithOutput() + pushableCount, err := self.countDifferences(to, from) if err != nil { return "?", "?" } - pullableCount, err := self.cmd.New(fmt.Sprintf(command, from, to)).DontLog().RunWithOutput() + pullableCount, err := self.countDifferences(from, to) if err != nil { return "?", "?" } return strings.TrimSpace(pushableCount), strings.TrimSpace(pullableCount) } +func (self *BranchCommands) countDifferences(from, to string) (string, error) { + cmdStr := NewGitCmd("rev-list"). + Arg(fmt.Sprintf("%s..%s", from, to)). + Arg("--count"). + ToString() + + return self.cmd.New(cmdStr).DontLog().RunWithOutput() +} + func (self *BranchCommands) IsHeadDetached() bool { - err := self.cmd.New("git symbolic-ref -q HEAD").DontLog().Run() + cmdStr := NewGitCmd("symbolic-ref").Arg("-q", "HEAD").ToString() + + err := self.cmd.New(cmdStr).DontLog().Run() return err != nil } func (self *BranchCommands) Rename(oldName string, newName string) error { - return self.cmd.New(fmt.Sprintf("git branch --move %s %s", self.cmd.Quote(oldName), self.cmd.Quote(newName))).Run() + cmdStr := NewGitCmd("branch"). + Arg("--move", self.cmd.Quote(oldName), self.cmd.Quote(newName)). + ToString() + + return self.cmd.New(cmdStr).Run() } func (self *BranchCommands) GetRawBranches() (string, error) { - 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() + cmdStr := NewGitCmd("for-each-ref"). + Arg("--sort=-committerdate"). + Arg(`--format="%(HEAD)%00%(refname:short)%00%(upstream:short)%00%(upstream:track)"`). + Arg("refs/heads"). + ToString() + + return self.cmd.New(cmdStr).DontLog().RunWithOutput() } type MergeOpts struct { @@ -156,15 +199,12 @@ type MergeOpts struct { } func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error { - mergeArg := "" - if self.UserConfig.Git.Merging.Args != "" { - mergeArg = " " + self.UserConfig.Git.Merging.Args - } - - command := fmt.Sprintf("git merge --no-edit%s %s", mergeArg, self.cmd.Quote(branchName)) - if opts.FastForwardOnly { - command = fmt.Sprintf("%s --ff-only", command) - } + command := NewGitCmd("merge"). + Arg("--no-edit"). + ArgIf(self.UserConfig.Git.Merging.Args != "", self.UserConfig.Git.Merging.Args). + ArgIf(opts.FastForwardOnly, "--ff-only"). + Arg(self.cmd.Quote(branchName)). + ToString() return self.cmd.New(command).Run() } diff --git a/pkg/commands/git_commands/branch_test.go b/pkg/commands/git_commands/branch_test.go index 2fdf7d9c2..75c288203 100644 --- a/pkg/commands/git_commands/branch_test.go +++ b/pkg/commands/git_commands/branch_test.go @@ -5,6 +5,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/stretchr/testify/assert" ) @@ -99,12 +100,53 @@ func TestBranchDeleteBranch(t *testing.T) { } func TestBranchMerge(t *testing.T) { - runner := oscommands.NewFakeRunner(t). - Expect(`git merge --no-edit "test"`, "", nil) - instance := buildBranchCommands(commonDeps{runner: runner}) + scenarios := []struct { + testName string + userConfig *config.UserConfig + opts MergeOpts + branchName string + expected string + }{ + { + testName: "basic", + userConfig: &config.UserConfig{}, + opts: MergeOpts{}, + branchName: "mybranch", + expected: `git merge --no-edit "mybranch"`, + }, + { + testName: "merging args", + userConfig: &config.UserConfig{ + Git: config.GitConfig{ + Merging: config.MergingConfig{ + Args: "--merging-args", // it's up to the user what they put here + }, + }, + }, + opts: MergeOpts{}, + branchName: "mybranch", + expected: `git merge --no-edit --merging-args "mybranch"`, + }, + { + testName: "fast forward only", + userConfig: &config.UserConfig{}, + opts: MergeOpts{FastForwardOnly: true}, + branchName: "mybranch", + expected: `git merge --no-edit --ff-only "mybranch"`, + }, + } - assert.NoError(t, instance.Merge("test", MergeOpts{})) - runner.CheckForMissingCalls() + for _, s := range scenarios { + s := s + t.Run(s.testName, func(t *testing.T) { + runner := oscommands.NewFakeRunner(t). + Expect(s.expected, "", nil) + instance := buildBranchCommands(commonDeps{runner: runner, userConfig: s.userConfig}) + + assert.NoError(t, instance.Merge(s.branchName, s.opts)) + runner.CheckForMissingCalls() + }) + } } func TestBranchCheckout(t *testing.T) { diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 0c5008e38..bca68c3ac 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -22,18 +22,27 @@ func NewCommitCommands(gitCommon *GitCommon) *CommitCommands { // ResetAuthor resets the author of the topmost commit func (self *CommitCommands) ResetAuthor() error { - return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run() + cmdStr := NewGitCmd("commit"). + Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author"). + ToString() + + return self.cmd.New(cmdStr).Run() } // Sets the commit's author to the supplied value. Value is expected to be of the form 'Name <Email>' func (self *CommitCommands) SetAuthor(value string) error { - commandStr := fmt.Sprintf("git commit --allow-empty --only --no-edit --amend --author=%s", self.cmd.Quote(value)) - return self.cmd.New(commandStr).Run() + cmdStr := NewGitCmd("commit"). + Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+self.cmd.Quote(value)). + ToString() + + return self.cmd.New(cmdStr).Run() } // ResetToCommit reset to commit func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { - return self.cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)). + cmdStr := NewGitCmd("reset").Arg("--"+strength, sha).ToString() + + return self.cmd.New(cmdStr). // prevents git from prompting us for input which would freeze the program // TODO: see if this is actually needed here AddEnvVars("GIT_TERMINAL_PROMPT=0"). @@ -45,38 +54,52 @@ func (self *CommitCommands) CommitCmdObj(message string) oscommands.ICmdObj { messageArgs := self.commitMessageArgs(message) skipHookPrefix := self.UserConfig.Git.SkipHookPrefix - noVerifyFlag := "" - if skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix) { - noVerifyFlag = " --no-verify" - } - return self.cmd.New(fmt.Sprintf("git commit%s%s%s", noVerifyFlag, self.signoffFlag(), messageArgs)) + cmdStr := NewGitCmd("commit"). + ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify"). + ArgIf(self.signoffFlag() != "", self.signoffFlag()). + Arg(messageArgs...). + ToString() + + return self.cmd.New(cmdStr) } // RewordLastCommit rewords the topmost commit with the given message func (self *CommitCommands) RewordLastCommit(message string) error { messageArgs := self.commitMessageArgs(message) - return self.cmd.New(fmt.Sprintf("git commit --allow-empty --amend --only%s", messageArgs)).Run() + + cmdStr := NewGitCmd("commit"). + Arg("--allow-empty", "--amend", "--only"). + Arg(messageArgs...). + ToString() + + return self.cmd.New(cmdStr).Run() } -func (self *CommitCommands) commitMessageArgs(message string) string { +func (self *CommitCommands) commitMessageArgs(message string) []string { msg, description, _ := strings.Cut(message, "\n") - descriptionArgs := "" + args := []string{"-m", self.cmd.Quote(msg)} + if description != "" { - descriptionArgs = fmt.Sprintf(" -m %s", self.cmd.Quote(description)) + args = append(args, "-m", self.cmd.Quote(description)) } - return fmt.Sprintf(" -m %s%s", self.cmd.Quote(msg), descriptionArgs) + return args } // runs git commit without the -m argument meaning it will invoke the user's editor func (self *CommitCommands) CommitEditorCmdObj() oscommands.ICmdObj { - return self.cmd.New(fmt.Sprintf("git commit%s%s", self.signoffFlag(), self.verboseFlag())) + cmdStr := NewGitCmd("commit"). + ArgIf(self.signoffFlag() != "", self.signoffFlag()). + ArgIf(self.verboseFlag() != "", self.verboseFlag()). + ToString() + + return self.cmd.New(cmdStr) } func (self *CommitCommands) signoffFlag() string { if self.UserConfig.Git.Commit.SignOff { - return " --signoff" + return "--signoff" } else { return "" } @@ -85,9 +108,9 @@ func (self *CommitCommands) signoffFlag() string { func (self *CommitCommands) verboseFlag() string { switch self.config.UserConfig.Git.Commit.Verbose { case "always": - return " --verbose" + return "--verbose" case "never": - return " --no-verbose" + return "--no-verbose" default: return "" } @@ -95,19 +118,25 @@ func (self *CommitCommands) verboseFlag() string { // Get the subject of the HEAD commit func (self *CommitCommands) GetHeadCommitMessage() (string, error) { - message, err := self.cmd.New("git log -1 --pretty=%s").DontLog().RunWithOutput() + cmdStr := NewGitCmd("log").Arg("-1", "--pretty=%s").ToString() + + message, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() return strings.TrimSpace(message), err } func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) { - cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha + cmdStr := NewGitCmd("rev-list"). + Arg("--format=%B", "--max-count=1", commitSha). + ToString() + messageWithHeader, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "") return strings.TrimSpace(message), err } func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) { - cmdStr := "git show --no-color " + commitSha + cmdStr := NewGitCmd("show").Arg("--no-color", commitSha).ToString() + diff, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() return diff, err } @@ -118,7 +147,10 @@ type Author struct { } func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) { - cmdStr := "git show --no-patch --pretty=format:'%an%x00%ae' " + commitSha + cmdStr := NewGitCmd("show"). + Arg("--no-patch", "--pretty=format:'%an%x00%ae'", commitSha). + ToString() + output, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() if err != nil { return Author{}, err @@ -138,15 +170,21 @@ func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error } func (self *CommitCommands) GetCommitMessagesFirstLine(shas []string) (string, error) { - return self.cmd.New( - fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", strings.Join(shas, " ")), - ).DontLog().RunWithOutput() + cmdStr := NewGitCmd("show"). + Arg("--no-patch", "--pretty=format:%s"). + Arg(shas...). + ToString() + + return self.cmd.New(cmdStr).DontLog().RunWithOutput() } func (self *CommitCommands) GetCommitsOneline(shas []string) (string, error) { - return self.cmd.New( - fmt.Sprintf("git show --no-patch --oneline %s", strings.Join(shas, " ")), - ).DontLog().RunWithOutput() + cmdStr := NewGitCmd("show"). + Arg("--no-patch", "--oneline"). + Arg(shas...). + ToString() + + return self.cmd.New(cmdStr).DontLog().RunWithOutput() } // AmendHead amends HEAD with whatever is staged in your working tree @@ -155,42 +193,57 @@ func (self *CommitCommands) AmendHead() error { } func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj { - return self.cmd.New("git commit --amend --no-edit --allow-empty") + cmdStr := NewGitCmd("commit"). + Arg("--amend", "--no-edit", "--allow-empty"). + ToString() + + return self.cmd.New(cmdStr) } func (self *CommitCommands) ShowCmdObj(sha string, filterPath string, ignoreWhitespace bool) oscommands.ICmdObj { contextSize := self.UserConfig.Git.DiffContextSize - filterPathArg := "" - if filterPath != "" { - filterPathArg = fmt.Sprintf(" -- %s", self.cmd.Quote(filterPath)) - } - ignoreWhitespaceArg := "" - if ignoreWhitespace { - ignoreWhitespaceArg = " --ignore-all-space" - } - cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --stat -p %s%s%s", - self.UserConfig.Git.Paging.ColorArg, contextSize, sha, ignoreWhitespaceArg, filterPathArg) + cmdStr := NewGitCmd("show"). + Arg("--submodule"). + Arg("--color="+self.UserConfig.Git.Paging.ColorArg). + Arg(fmt.Sprintf("--unified=%d", contextSize)). + Arg("--stat"). + Arg("-p"). + Arg(sha). + ArgIf(ignoreWhitespace, "--ignore-all-space"). + ArgIf(filterPath != "", "--", self.cmd.Quote(filterPath)). + ToString() + return self.cmd.New(cmdStr).DontLog() } // Revert reverts the selected commit by sha func (self *CommitCommands) Revert(sha string) error { - return self.cmd.New(fmt.Sprintf("git revert %s", sha)).Run() + cmdStr := NewGitCmd("revert").Arg(sha).ToString() + + return self.cmd.New(cmdStr).Run() } func (self *CommitCommands) RevertMerge(sha string, parentNumber int) error { - return self.cmd.New(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run() + cmdStr := NewGitCmd("revert").Arg(sha, "-m", fmt.Sprintf("%d", parentNumber)). + ToString() + + return self.cmd.New(cmdStr).Run() } // CreateFixupCommit creates a commit that fixes up a previous commit func (self *CommitCommands) CreateFixupCommit(sha string) error { - return self.cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run() + cmdStr := NewGitCmd("commit").Arg("--fixup=" + sha).ToString() + + return self.cmd.New(cmdStr).Run() } // a value of 0 means the head commit, 1 is the parent commit, etc func (self *CommitCommands) GetCommitMessageFromHistory(value int) (string, error) { - hash, _ := self.cmd.New(fmt.Sprintf("git log -1 --skip=%d --pretty=%%H", value)).DontLog().RunWithOutput() + cmdStr := NewGitCmd("log").Arg("-1", fmt.Sprintf("--skip=%d", value), "--pretty=%H"). + ToString() + + hash, _ := self.cmd.New(cmdStr).DontLog().RunWithOutput() formattedHash := strings.TrimSpace(hash) if len(formattedHash) == 0 { return "", ErrInvalidCommitIndex diff --git a/pkg/commands/git_commands/commit_file_loader.go b/pkg/commands/git_commands/commit_file_loader.go index 0b606ae86..53ca046ba 100644 --- a/pkg/commands/git_commands/commit_file_loader.go +++ b/pkg/commands/git_commands/commit_file_loader.go @@ -1,7 +1,6 @@ package git_commands import ( - "fmt" "strings" "github.com/jesseduffield/generics/slices" @@ -25,12 +24,18 @@ func NewCommitFileLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) * // GetFilesInDiff get the specified commit files func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) { - reverseFlag := "" - if reverse { - reverseFlag = " -R " - } - - filenames, err := self.cmd.New(fmt.Sprintf("git diff --submodule --no-ext-diff --name-status -z --no-renames %s %s %s", reverseFlag, from, to)).DontLog().RunWithOutput() + cmdStr := NewGitCmd("diff"). + Arg("--submodule"). + Arg("--no-ext-diff"). + Arg("--name-status"). + Arg("-z"). + Arg("--no-renames"). + ArgIf(reverse, "-R"). + Arg(from). + Arg(to). + ToString() + + filenames, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() if err != nil { return nil, err } diff --git a/pkg/commands/git_commands/commit_loader.go b/pkg/commands/git_commands/commit_loader.go index e68f56ebb..00a468ab2 100644 --- a/pkg/commands/git_commands/commit_loader.go +++ b/pkg/commands/git_commands/commit_loader.go @@ -201,12 +201,11 @@ func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode // note that we're not filtering these as we do non-re |