diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-12-29 15:05:52 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-12-29 15:05:52 +1100 |
commit | 26c07b1ab33860a1a7591a0638f9925ccf497ffa (patch) | |
tree | ff02672e87275e6f6d3cd610877a9ab2f09600a1 | |
parent | 3d4470a6c072208722e5ae9a54bcb9634959a1c5 (diff) |
WIP
26 files changed, 143 insertions, 134 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go index b378fdb97..131f05c04 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -151,7 +151,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { } func (app *App) validateGitVersion() error { - output, err := app.OSCommand.RunWithOutput(app.OSCommand.NewCmdObj("git --version")) + output, err := app.OSCommand.NewCmdObj("git --version").RunWithOutput() // if we get an error anywhere here we'll show the same status minVersionError := errors.New(app.Tr.MinGitVersionError) if err != nil { diff --git a/pkg/commands/branches.go b/pkg/commands/branches.go index ee16d544e..57997b8dd 100644 --- a/pkg/commands/branches.go +++ b/pkg/commands/branches.go @@ -91,11 +91,11 @@ func (c *GitCommand) GetBranchGraphCmdObj(branchName string) oscommands.ICmdObj } func (c *GitCommand) SetUpstreamBranch(upstream string) error { - return c.Run(c.NewCmdObj("git branch -u " + c.OSCommand.Quote(upstream))) + return c.NewCmdObj("git branch -u " + c.OSCommand.Quote(upstream)).Run() } func (c *GitCommand) SetBranchUpstream(remoteName string, remoteBranchName string, branchName string) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName)))) + return c.NewCmdObj(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName))).Run() } func (c *GitCommand) GetCurrentBranchUpstreamDifferenceCount() (string, string) { @@ -110,11 +110,11 @@ func (c *GitCommand) GetBranchUpstreamDifferenceCount(branchName string) (string // current branch func (c *GitCommand) GetCommitDifferences(from, to string) (string, string) { command := "git rev-list %s..%s --count" - pushableCount, err := c.RunWithOutput(c.NewCmdObj(fmt.Sprintf(command, to, from))) + pushableCount, err := c.NewCmdObj(fmt.Sprintf(command, to, from)).RunWithOutput() if err != nil { return "?", "?" } - pullableCount, err := c.RunWithOutput(c.NewCmdObj(fmt.Sprintf(command, from, to))) + pullableCount, err := c.NewCmdObj(fmt.Sprintf(command, from, to)).RunWithOutput() if err != nil { return "?", "?" } @@ -134,37 +134,37 @@ func (c *GitCommand) Merge(branchName string, opts MergeOpts) error { command = fmt.Sprintf("%s --ff-only", command) } - return c.OSCommand.Run(c.OSCommand.NewCmdObj(command)) + return c.OSCommand.NewCmdObj(command).Run() } // AbortMerge abort merge func (c *GitCommand) AbortMerge() error { - return c.Run(c.NewCmdObj("git merge --abort")) + return c.NewCmdObj("git merge --abort").Run() } func (c *GitCommand) IsHeadDetached() bool { - err := c.Run(c.NewCmdObj("git symbolic-ref -q HEAD")) + err := c.NewCmdObj("git symbolic-ref -q HEAD").Run() return err != nil } // ResetHardHead runs `git reset --hard` func (c *GitCommand) ResetHard(ref string) error { - return c.Run(c.NewCmdObj("git reset --hard " + c.OSCommand.Quote(ref))) + return c.NewCmdObj("git reset --hard " + c.OSCommand.Quote(ref)).Run() } // ResetSoft runs `git reset --soft HEAD` func (c *GitCommand) ResetSoft(ref string) error { - return c.Run(c.NewCmdObj("git reset --soft " + c.OSCommand.Quote(ref))) + return c.NewCmdObj("git reset --soft " + c.OSCommand.Quote(ref)).Run() } func (c *GitCommand) ResetMixed(ref string) error { - return c.Run(c.NewCmdObj("git reset --mixed " + c.OSCommand.Quote(ref))) + return c.NewCmdObj("git reset --mixed " + c.OSCommand.Quote(ref)).Run() } func (c *GitCommand) RenameBranch(oldName string, newName string) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git branch --move %s %s", c.OSCommand.Quote(oldName), c.OSCommand.Quote(newName)))) + return c.NewCmdObj(fmt.Sprintf("git branch --move %s %s", c.OSCommand.Quote(oldName), c.OSCommand.Quote(newName))).Run() } func (c *GitCommand) GetRawBranches() (string, error) { - return c.RunWithOutput(c.NewCmdObj(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`)) + return c.NewCmdObj(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`).RunWithOutput() } diff --git a/pkg/commands/branches_test.go b/pkg/commands/branches_test.go index 7064f4968..a187a1a5d 100644 --- a/pkg/commands/branches_test.go +++ b/pkg/commands/branches_test.go @@ -210,7 +210,7 @@ func TestGitCommandGetAllBranchGraph(t *testing.T) { return secureexec.Command("echo") } cmdStr := gitCmd.UserConfig.Git.AllBranchesLogCmd - _, err := gitCmd.OSCommand.RunWithOutput(gitCmd.NewCmdObj(cmdStr)) + _, err := gitCmd.NewCmdObj(cmdStr).RunWithOutput() assert.NoError(t, err) } diff --git a/pkg/commands/commits.go b/pkg/commands/commits.go index 447e9adb8..bd3e6ea0b 100644 --- a/pkg/commands/commits.go +++ b/pkg/commands/commits.go @@ -10,18 +10,17 @@ import ( // RenameCommit renames the topmost commit with the given name func (c *GitCommand) RenameCommit(name string) error { - return c.Run(c.NewCmdObj("git commit --allow-empty --amend --only -m " + c.OSCommand.Quote(name))) + return c.NewCmdObj("git commit --allow-empty --amend --only -m " + c.OSCommand.Quote(name)).Run() } // ResetToCommit reset to commit func (c *GitCommand) ResetToCommit(sha string, strength string, envVars []string) error { - cmdObj := c.NewCmdObj(fmt.Sprintf("git reset --%s %s", strength, sha)). + return c.NewCmdObj(fmt.Sprintf("git reset --%s %s", strength, sha)). // 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"). - AddEnvVars(envVars...) - - return c.OSCommand.Run(cmdObj) + AddEnvVars(envVars...). + Run() } func (c *GitCommand) CommitCmdObj(message string, flags string) oscommands.ICmdObj { @@ -41,24 +40,24 @@ func (c *GitCommand) CommitCmdObj(message string, flags string) oscommands.ICmdO // Get the subject of the HEAD commit func (c *GitCommand) GetHeadCommitMessage() (string, error) { - message, err := c.RunWithOutput(c.NewCmdObj("git log -1 --pretty=%s")) + message, err := c.NewCmdObj("git log -1 --pretty=%s").RunWithOutput() return strings.TrimSpace(message), err } func (c *GitCommand) GetCommitMessage(commitSha string) (string, error) { cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha - messageWithHeader, err := c.RunWithOutput(c.NewCmdObj(cmdStr)) + messageWithHeader, err := c.NewCmdObj(cmdStr).RunWithOutput() message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n") return strings.TrimSpace(message), err } func (c *GitCommand) GetCommitMessageFirstLine(sha string) (string, error) { - return c.RunWithOutput(c.NewCmdObj(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha))) + return c.NewCmdObj(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)).RunWithOutput() } // AmendHead amends HEAD with whatever is staged in your working tree func (c *GitCommand) AmendHead() error { - return c.OSCommand.Run(c.AmendHeadCmdObj()) + return c.AmendHeadCmdObj().Run() } func (c *GitCommand) AmendHeadCmdObj() oscommands.ICmdObj { @@ -78,11 +77,11 @@ func (c *GitCommand) ShowCmdObj(sha string, filterPath string) oscommands.ICmdOb // Revert reverts the selected commit by sha func (c *GitCommand) Revert(sha string) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git revert %s", sha))) + return c.NewCmdObj(fmt.Sprintf("git revert %s", sha)).Run() } func (c *GitCommand) RevertMerge(sha string, parentNumber int) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git revert %s -m %d", sha, parentNumber))) + return c.NewCmdObj(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run() } // CherryPickCommits begins an interactive rebase with the given shas being cherry picked onto HEAD @@ -92,15 +91,15 @@ func (c *GitCommand) CherryPickCommits(commits []*models.Commit) error { todo = "pick " + commit.Sha + " " + commit.Name + "\n" + todo } - cmd, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false) + cmdObj, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false) if err != nil { return err } - return c.OSCommand.Run(cmd) + return cmdObj.Run() } // CreateFixupCommit creates a commit that fixes up a previous commit func (c *GitCommand) CreateFixupCommit(sha string) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git commit --fixup=%s", sha))) + return c.NewCmdObj(fmt.Sprintf("git commit --fixup=%s", sha)).Run() } diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 28f37612f..119d45ad9 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -29,22 +29,22 @@ func (c *GitCommand) OpenMergeToolCmdObj() oscommands.ICmdObj { } func (c *GitCommand) OpenMergeTool() error { - return c.Run(c.OpenMergeToolCmdObj()) + return c.OpenMergeToolCmdObj().Run() } // StageFile stages a file func (c *GitCommand) StageFile(fileName string) error { - return c.Run(c.NewCmdObj("git add -- " + c.OSCommand.Quote(fileName))) + return c.NewCmdObj("git add -- " + c.OSCommand.Quote(fileName)).Run() } // StageAll stages all files func (c *GitCommand) StageAll() error { - return c.Run(c.NewCmdObj("git add -A")) + return c.NewCmdObj("git add -A").Run() } // UnstageAll unstages all files func (c *GitCommand) UnstageAll() error { - return c.Run(c.NewCmdObj("git reset")) + return c.NewCmdObj("git reset").Run() } // UnStageFile unstages a file @@ -57,8 +57,8 @@ func (c *GitCommand) UnStageFile(fileNames []string, reset bool) error { } for _, name := range fileNames { - cmdObj := c.NewCmdObj(fmt.Sprintf(command, c.OSCommand.Quote(name))) - if err := c.Run(cmdObj); err != nil { + err := c.NewCmdObj(fmt.Sprintf(command, c.OSCommand.Quote(name))).Run() + if err != nil { return err } } @@ -122,22 +122,22 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error { quotedFileName := c.OSCommand.Quote(file.Name) if file.ShortStatus == "AA" { - if err := c.Run(c.NewCmdObj("git checkout --ours -- " + quotedFileName)); err != nil { + if err := c.NewCmdObj("git checkout --ours -- " + quotedFileName).Run(); err != nil { return err } - if err := c.Run(c.NewCmdObj("git add -- " + quotedFileName)); err != nil { + if err := c.NewCmdObj("git add -- " + quotedFileName).Run(); err != nil { return err } return nil } if file.ShortStatus == "DU" { - return c.Run(c.NewCmdObj("git rm -- " + quotedFileName)) + return c.NewCmdObj("git rm -- " + quotedFileName).Run() } // if the file isn't tracked, we assume you want to delete it if file.HasStagedChanges || file.HasMergeConflicts { - if err := c.Run(c.NewCmdObj("git reset -- " + quotedFileName)); err != nil { + if err := c.NewCmdObj("git reset -- " + quotedFileName).Run(); err != nil { return err } } @@ -163,7 +163,7 @@ func (c *GitCommand) DiscardUnstagedDirChanges(node *filetree.FileNode) error { } quotedPath := c.OSCommand.Quote(node.GetPath()) - if err := c.Run(c.NewCmdObj("git checkout -- " + quotedPath)); err != nil { + if err := c.NewCmdObj("git checkout -- " + quotedPath).Run(); err != nil { return err } @@ -188,7 +188,7 @@ func (c *GitCommand) RemoveUntrackedDirFiles(node *filetree.FileNode) error { // DiscardUnstagedFileChanges directly func (c *GitCommand) DiscardUnstagedFileChanges(file *models.File) error { quotedFileName := c.OSCommand.Quote(file.Name) - return c.Run(c.NewCmdObj("git checkout -- " + quotedFileName)) + return c.NewCmdObj("git checkout -- " + quotedFileName).Run() } // Ignore adds a file to the gitignore for the repo @@ -199,7 +199,7 @@ func (c *GitCommand) Ignore(filename string) error { // WorktreeFileDiff returns the diff of a file func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string { // for now we assume an error means the file was deleted - s, _ := c.OSCommand.RunWithOutput(c.WorktreeFileDiffCmdObj(file, plain, cached, ignoreWhitespace)) + s, _ := c.WorktreeFileDiffCmdObj(file, plain, cached, ignoreWhitespace).RunWithOutput() return s } @@ -240,14 +240,13 @@ func (c *GitCommand) ApplyPatch(patch string, flags ...string) error { flagStr += " --" + flag } - return c.Run(c.NewCmdObj(fmt.Sprintf("git apply %s %s", flagStr, c.OSCommand.Quote(filepath)))) + return c.NewCmdObj(fmt.Sprintf("git apply %s %s", flagStr, c.OSCommand.Quote(filepath))).Run() } // ShowFileDiff get the diff of specified from and to. Typically this will be used for a single commit so it'll be 123abc^..123abc // but when we're in diff mode it could be any 'from' to any 'to'. The reverse flag is also here thanks to diff mode. func (c *GitCommand) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool) (string, error) { - cmdObj := c.ShowFileDiffCmdObj(from, to, reverse, fileName, plain) - return c.RunWithOutput(cmdObj) + return c.ShowFileDiffCmdObj(from, to, reverse, fileName, plain).RunWithOutput() } func (c *GitCommand) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) oscommands.ICmdObj { @@ -267,7 +266,7 @@ func (c *GitCommand) ShowFileDiffCmdObj(from string, to string, reverse bool, fi // CheckoutFile checks out the file for the given commit func (c *GitCommand) CheckoutFile(commitSha, fileName string) error { - return c.Run(c.NewCmdObj(fmt.Sprintf("git checkout %s -- %s", commitSha, c.OSCommand.Quote(fileName)))) + return c.NewCmdObj(fmt.Sprintf("git checkout %s -- %s", commitSha, c.OSCommand.Quote(fileName))).Run() } // DiscardOldFileChanges discards changes to a file from an old commit @@ -277,7 +276,7 @@ func (c *GitCommand) DiscardOldFileChanges(commits []*models.Commit, commitIndex } // check if file exists in previous commit (this command returns an error if the file doesn't exist) - if err := c.Run(c.NewCmdObj("git cat-file -e HEAD^:" + c.OSCommand.Quote(fileName))); err != nil { + if err := c.NewCmdObj("git cat-file -e HEAD^:" + c.OSCommand.Quote(fileName)).Run(); err != nil { if err := c.OSCommand.Remove(fileName); err != nil { return err } @@ -300,17 +299,17 @@ func (c *GitCommand) DiscardOldFileChanges(commits []*models.Commit, commitIndex // DiscardAnyUnstagedFileChanges discards any unstages file changes via `git checkout -- .` func (c *GitCommand) DiscardAnyUnstagedFileChanges() error { - return c.Run(c.NewCmdObj("git checkout -- .")) + return c.NewCmdObj("git checkout -- .").Run() } // RemoveTrackedFiles will delete the given file(s) even if they are currently tracked func (c *GitCommand) RemoveTrackedFiles(name string) error { - return c.Run(c.NewCmdObj("git rm -r --cached -- " + c.OSCommand.Quote(name))) + return c.NewCmdObj("git rm -r --cached -- " + c.OSCommand.Quote(name)).Run() } // RemoveUntrackedFiles runs `git clean -fd` func (c *GitCommand) RemoveUntrackedFiles() error { - return c.Run(c.NewCmdObj("git clean -fd")) + return c.NewCmdObj("git clean -fd").Run() } // ResetAndClean removes all unstaged changes and removes all untracked files @@ -350,7 +349,7 @@ func (c *GitCommand) EditFileCmdStr(filename string, lineNumber int) (string, er editor = c.OSCommand.Getenv("EDITOR") } if editor == "" { - if err := c.OSCommand.Run(c.NewCmdObj("which vi")); err == nil { + if err := c.OSCommand.NewCmdObj("which vi").Run(); err == nil { editor = "vi" } } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 32062ccb4..81d46d7c5 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -215,7 +215,7 @@ func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filenam } func VerifyInGitRepo(osCommand *oscommands.OSCommand) error { - return osCommand.Run(osCommand.NewCmdObj("git rev-parse --git-dir")) + return osCommand.NewCmdObj("git rev-parse --git-dir").Run() } func (c *GitCommand) Run(cmdObj oscommands.ICmdObj) error { @@ -260,3 +260,7 @@ func (c *GitCommand) NewCmdObjWithLog(cmdStr string) oscommands.ICmdObj { c.OSCommand.LogCmdObj(cmdObj) return cmdObj } + +func (c *GitCommand) Quote(str string) string { + return c.OSCommand.Quote(str) +} diff --git a/pkg/commands/loading_commit_files.go b/pkg/commands/loading_commit_files.go index 95d98d668..f390bdc64 100644 --- a/pkg/commands/loading_commit_files.go +++ b/pkg/commands/loading_commit_files.go @@ -14,7 +14,7 @@ func (c *GitCommand) GetFilesInDiff(from string, to string, reverse bool) ([]*mo reverseFlag = " -R " } - filenames, err := c.RunWithOutput(c.NewCmdObj(fmt.Sprintf("git diff --submodule --no-ext-diff --name-status -z --no-renames %s %s %s", reverseFlag, from, to))) + filenames, err := c.NewCmdObj(fmt.Sprintf("git diff --submodule --no-ext-diff --name-status -z --no-renames %s %s %s", reverseFlag, from, to)).RunWithOutput() if err != nil { return nil, err } diff --git a/pkg/commands/loading_commits.go b/pkg/commands/loading_commits.go index 736c0fd26..9f107d6e8 100644 --- a/pkg/commands/loading_commits.go +++ b/pkg/commands/loading_commits.go @@ -26,10 +26,16 @@ import ( const SEPARATION_CHAR = "|" +// TODO: swap out for 'cmd' +type CmdObjBuilder interface { + NewCmdObj(command string) oscommands.ICmdObj + Quote(str string) string +} + // CommitListBuilder returns a list of Branch objects for the current repo type CommitListBuilder struct { *common.Common - cmd oscommands.ICmdObjBuilder + cmd CmdObjBuilder getCurrentBranchName func() (string, string, error) getRebaseMode func() (string, error) @@ -44,9 +50,8 @@ func NewCommitListBuilder( osCommand *oscommands.OSCommand, ) *CommitListBuilder { return &CommitListBuilder{ - Common: cmn, - cmd: gitCommand, - + Common: cmn, + cmd: gitCommand, getCurrentBranchName: gitCommand.CurrentBranchName, getRebaseMode: gitCommand.RebaseMode, dotGitDir: gitCommand.DotGitDir, @@ -202,7 +207,7 @@ func (c *CommitListBuilder) getHydratedRebasingCommits(rebaseMode string) ([]*mo // note that we're not filtering these as we do non-rebasing commits just because // I suspect that will cause some damage - cmdObj := c.cmd.New( + cmdObj := c.cmd.NewCmdObj( fmt.Sprintf( "git show %s --no-patch --oneline %s --abbrev=%d", strings.Join(commitShas, " "), @@ -375,7 +380,7 @@ func (c *CommitListBuilder) getMergeBase(refName string) (string, error) { } // swallowing error because it's not a big deal; probably because there are no commits yet - output, _ := c.cmd.New(fmt.Sprintf("git merge-base %s %s", c.cmd.Quote(refName), c.cmd.Quote(baseBranch))).RunWithOutput() + output, _ := c.cmd.NewCmdObj(fmt.Sprintf("git merge-base %s %s", c.cmd.Quote(refName), c.cmd.Quote(baseBranch))).RunWithOutput() return ignoringWarnings(output), nil } @@ -392,7 +397,7 @@ func ignoringWarnings(commandOutput string) string { // getFirstPushedCommit returns the first commit SHA which has been pushed to the ref's upstream. // all commits above this are deemed unpushed and marked as such. func (c *CommitListBuilder) getFirstPushedCommit(refName string) (string, error) { - output, err := c.cmd.New(fmt.Sprintf("git merge-base %s %s@{u}", c.cmd.Quote(refName), c.cmd.Quote(refName))).RunWithOutput() + output, err := c.cmd.NewCmdObj(fmt.Sprintf("git merge-base %s %s@{u}", c.cmd.Quote(refName), c.cmd.Quote(refName))).RunWithOutput() if err != nil { return "", err } @@ -420,7 +425,7 @@ func (c *CommitListBuilder) getLogCmd(opts GetCommitsOptions) oscommands.ICmdObj allFlag = " --all" } - return c.cmd.New( + return c.cmd.NewCmdObj( fmt.Sprintf( "git log %s %s %s --oneline %s %s --abbrev=%d %s", c.cmd.Quote(opts.RefName), diff --git a/pkg/commands/loading_files.go b/pkg/commands/loading_files.go index df7be0dfe..f3d9af016 100644 --- a/pkg/commands/loading_files.go +++ b/pkg/commands/loading_files.go @@ -80,7 +80,7 @@ func (c *GitCommand) GitStatus(opts GitStatusOptions) ([]FileStatus, error) { noRenamesFlag = "--no-renames" } - statusLines, err := c.RunWithOutput(c.NewCmdObj(fmt.Sprintf("git status %s --porcelain -z %s", opts.UntrackedFilesArg, noRenamesFlag))) + statusLines, err := c.NewCmdObj(fmt.Sprintf("git status %s --porcelain -z %s", opts.UntrackedFilesArg, noRenamesFlag)).RunWithOutput() if err != nil { return []FileStatus{}, err } diff --git a/pkg/commands/loading_reflog_commits.go b/pkg/commands/loading_reflog_commits.go index 80c2e815d..db49a11fd 100644 --- a/pkg/commands/loading_reflog_commits.go +++ b/pkg/commands/loading_reflog_commits.go @@ -20,7 +20,7 @@ func (c *GitCommand) GetReflogCommits(lastReflogCommit *models.Commit, filterPat cmdObj := c.OSCommand.NewCmdObj(fmt.Sprintf(`git log -g --abbrev=20 --format="%%h %%ct %%gs" %s`, filterPathArg)) onlyObtainedNewReflogCommits := false - err := c.OSCommand.RunLineOutputCmd(cmdObj, func(line string) (bool, error) { + err := cmdObj.RunLineOutputCmd(func(line string) (bool, error) { fields := strings.SplitN(line, " ", 3) if len(fields) <= 2 { return false, nil diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go index af460034a..b0e0c6bab 100644 --- a/pkg/commands/loading_remotes.go +++ b/pkg/commands/loading_remotes.go @@ -10,7 +10,7 @@ import ( ) func (c *GitCommand) GetRemotes() ([]*models.Remote, error) { - remoteBranchesStr, err := c.RunWithOutput(c.NewCmdObj("git branch -r")) + remoteBranchesStr, err := c.NewCmdObj("git branch -r").RunWithOutput() if err != nil { return nil, err } diff --git a/pkg/commands/loading_stash.go b/pkg/commands/loading_stash.go index 40867a26b..3433878d6 100644 --- a/pkg/commands/loading_stash.go +++ b/pkg/commands/loading_stash.go @@ -10,7 +10,7 @@ import ( ) func (c *GitCommand) getUnfilteredStashEntries() []*models.StashEntry { - rawString, _ := c.RunWithOutput(c.NewCmdObj("git stash list --pretty='%gs'")) + rawString, _ := c.NewCmdObj("git stash list --pretty='%gs'").RunWithOutput() stashEntries := []*models.StashEntry{} for i, line := range utils.SplitLines(rawString) { stashEntries = append(stashEntries, stashEntryFromLine(line, i)) @@ -24,7 +24,7 @@ func (c *GitCommand) GetStashEntries(filterPath string) []*models.StashEntry { return c.getUnfilteredStashEntries() } - rawString, err := c.RunWithOutput(c.NewCmdObj("git stash list --name-only")) + rawString, err := c.NewCmdObj("git stash list --name-only").RunWithOutput() if err != nil { return c.getUnfilteredStashEntries() } diff --git a/pkg/commands/loading_tags.go b/pkg/commands/loading_tags.go index f97985ce9..5353de177 100644 --- a/pkg/commands/loading_tags.go +++ b/pkg/commands/loading_tags.go @@ -10,7 +10,7 @@ import ( func (c *GitCommand) GetTags() ([]*models.Tag, error) { // get remote branches, sorted by creation date (descending) // see: https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---sortltkeygt - remoteBranchesStr, err := c.OSCommand.RunWithOutput(c.NewCmdObj(`git tag --list --sort=-creatordate`)) + remoteBranchesStr, err := c.NewCmdObj(`git tag --list --sort=-creatordate`).RunWithOutput() if err != nil { return nil, err } diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index b81e06364..2992719b1 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -55,6 +55,8 @@ type OSCommand struct { removeFile func(string) error + Cmd ICmdObjBuilder + ICmdObjRunner } @@ -162,8 +164,7 @@ func (c *OSCommand) OpenFile(filename string) error { "filename": c.Quote(filename), } command := utils.ResolvePlaceholderString(commandTemplate, templateValues) - err := c.Run(c.NewShellCmdObj(command)) - return err + return c.NewShellCmdObj(command).Run() } // OpenLink opens a file with the given @@ -175,8 +176,7 @@ func (c *OSCommand) OpenLink(link string) error { } command := utils.ResolvePlaceholderString(commandTemplate, templateValues) - err := c.Run(c.NewShellCmdObj(command)) - return err + return c.NewShellCmdObj(command).Run() } // Quote wraps a message in platform-specific quotation marks @@ -381,8 +381,10 @@ func (c *OSCommand) NewCmdObj(cmdStr string) ICmdObj { cmd.Env = os.Environ() return &CmdObj{ - cmdStr: cmdStr, - cmd: cmd, + cmdStr: cmdStr, + cmd: cmd, + runner: c.ICmdObjRunner, + logCommand: c.LogCmdObj, } } @@ -391,8 +393,10 @@ func (c *OSCommand) NewCmdObjFromArgs(args []string) ICmdObj { cmd.Env = os.Environ() return &CmdObj{ - cmdStr: strings.Join(args, " "), - cmd: cmd, + cmdStr: strings.Join(args, " "), + cmd: cmd, + runner: c.ICmdObjRunner, + logCommand: c.LogCmdObj, } } diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 2c2a58802..98109f408 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -33,7 +33,7 @@ func TestOSCommandRunWithOutput(t *testing.T) { for _, s := range scenarios { c := NewDummyOSCommand() - s.test(NewDummyOSCommand().RunWithOutput(c.NewCmdObj(s.command))) + s.test(c.NewCmdObj(s.command).RunWithOutput()) } } @@ -55,7 +55,7 @@ func TestOSCommandRun(t *testing.T) { for _, s := range scenarios { c := NewDummyOSCommand() - s.test(c.Run(c.NewCmdObj(s.command))) + s.test(c.NewCmdObj(s.command)).Run() } } diff --git a/pkg/commands/patch_rebases.go b/pkg/commands/patch_rebases.go index d9947b18e..8f33479bf 100644 --- a/pkg/commands/patch_rebases.go +++ b/pkg/commands/patch_rebases.go @@ -85,12 +85,12 @@ func (c *GitCommand) MovePatchToSelectedCommit(commits []*models.Commit, sourceC todo = a + " " + commit.Sha + " " + commit.Name + "\n" + todo } - cmd, err := c.PrepareInteractiveRebaseCommand(commits[baseIndex].Sha, todo, tru |