diff options
author | Andrew Hynes <andrewjhynes@gmail.com> | 2022-10-06 22:59:06 -0230 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-06 22:59:06 -0230 |
commit | 8c46a0110d7ff25b2c915a1eacfb7f4030b678b4 (patch) | |
tree | 5851623b5f49d5efa65bed58a9b9295bdef8f262 /pkg/commands | |
parent | a30d924afe8e996dd0517bc70855803858efc362 (diff) | |
parent | 7b4b42abd6f458eb667d322ec870bc6b20024812 (diff) |
Merge branch 'master' into stash-untracked-changes
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 3 | ||||
-rw-r--r-- | pkg/commands/git_cmd_obj_runner.go | 4 | ||||
-rw-r--r-- | pkg/commands/git_commands/file.go | 4 | ||||
-rw-r--r-- | pkg/commands/git_commands/rebase.go | 12 | ||||
-rw-r--r-- | pkg/commands/git_commands/working_tree_test.go | 4 | ||||
-rw-r--r-- | pkg/commands/git_config/get_key.go | 4 | ||||
-rw-r--r-- | pkg/commands/loaders/commits.go | 3 | ||||
-rw-r--r-- | pkg/commands/loaders/files.go | 2 | ||||
-rw-r--r-- | pkg/commands/oscommands/cmd_obj.go | 6 | ||||
-rw-r--r-- | pkg/commands/oscommands/cmd_obj_runner.go | 48 | ||||
-rw-r--r-- | pkg/commands/oscommands/fake_cmd_obj_runner.go | 5 | ||||
-rw-r--r-- | pkg/commands/oscommands/gui_io.go | 3 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 6 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_test.go | 9 |
14 files changed, 86 insertions, 27 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 2176077d4..a28e9afb6 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1,7 +1,6 @@ package commands import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -68,7 +67,7 @@ func NewGitCommand( return nil, err } - dotGitDir, err := findDotGitDir(os.Stat, ioutil.ReadFile) + dotGitDir, err := findDotGitDir(os.Stat, os.ReadFile) if err != nil { return nil, err } diff --git a/pkg/commands/git_cmd_obj_runner.go b/pkg/commands/git_cmd_obj_runner.go index c57c2d2be..96cef3c61 100644 --- a/pkg/commands/git_cmd_obj_runner.go +++ b/pkg/commands/git_cmd_obj_runner.go @@ -21,6 +21,10 @@ func (self *gitCmdObjRunner) RunWithOutput(cmdObj oscommands.ICmdObj) (string, e return self.innerRunner.RunWithOutput(cmdObj) } +func (self *gitCmdObjRunner) RunWithOutputs(cmdObj oscommands.ICmdObj) (string, string, error) { + return self.innerRunner.RunWithOutputs(cmdObj) +} + func (self *gitCmdObjRunner) RunAndProcessLines(cmdObj oscommands.ICmdObj, onLine func(line string) (bool, error)) error { return self.innerRunner.RunAndProcessLines(cmdObj, onLine) } diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 898c26e33..111733bb7 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -1,7 +1,7 @@ package git_commands import ( - "io/ioutil" + "os" "strconv" "github.com/go-errors/errors" @@ -20,7 +20,7 @@ func NewFileCommands(gitCommon *GitCommon) *FileCommands { // Cat obtains the content of a file func (self *FileCommands) Cat(fileName string) (string, error) { - buf, err := ioutil.ReadFile(fileName) + buf, err := os.ReadFile(fileName) if err != nil { return "", nil } diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 48a613e41..11c187f36 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -2,7 +2,7 @@ package git_commands import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strings" @@ -202,7 +202,7 @@ func (self *RebaseCommands) AmendTo(sha string) error { // EditRebaseTodo sets the action at a given index in the git-rebase-todo file func (self *RebaseCommands) EditRebaseTodo(index int, action string) error { fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") - bytes, err := ioutil.ReadFile(fileName) + bytes, err := os.ReadFile(fileName) if err != nil { return err } @@ -217,7 +217,7 @@ func (self *RebaseCommands) EditRebaseTodo(index int, action string) error { content[contentIndex] = action + " " + strings.Join(splitLine[1:], " ") result := strings.Join(content, "\n") - return ioutil.WriteFile(fileName, []byte(result), 0o644) + return os.WriteFile(fileName, []byte(result), 0o644) } func (self *RebaseCommands) getTodoCommitCount(content []string) int { @@ -234,7 +234,7 @@ func (self *RebaseCommands) getTodoCommitCount(content []string) int { // MoveTodoDown moves a rebase todo item down by one position func (self *RebaseCommands) MoveTodoDown(index int) error { fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") - bytes, err := ioutil.ReadFile(fileName) + bytes, err := os.ReadFile(fileName) if err != nil { return err } @@ -247,7 +247,7 @@ func (self *RebaseCommands) MoveTodoDown(index int) error { rearrangedContent = append(rearrangedContent, content[contentIndex+1:]...) result := strings.Join(rearrangedContent, "\n") - return ioutil.WriteFile(fileName, []byte(result), 0o644) + return os.WriteFile(fileName, []byte(result), 0o644) } // SquashAllAboveFixupCommits squashes all fixup! commits above the given one @@ -255,7 +255,7 @@ func (self *RebaseCommands) SquashAllAboveFixupCommits(sha string) error { return self.runSkipEditorCommand( self.cmd.New( fmt.Sprintf( - "git rebase --interactive --autostash --autosquash %s^", + "git rebase --interactive --rebase-merges --autostash --autosquash %s^", sha, ), ), diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go index e4e884ce4..049961541 100644 --- a/pkg/commands/git_commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -2,7 +2,7 @@ package git_commands import ( "fmt" - "io/ioutil" + "os" "regexp" "testing" @@ -432,7 +432,7 @@ func TestWorkingTreeApplyPatch(t *testing.T) { filename := matches[1] - content, err := ioutil.ReadFile(filename) + content, err := os.ReadFile(filename) assert.NoError(t, err) assert.Equal(t, "test", string(content)) diff --git a/pkg/commands/git_config/get_key.go b/pkg/commands/git_config/get_key.go index bd6f59248..c3156a2db 100644 --- a/pkg/commands/git_config/get_key.go +++ b/pkg/commands/git_config/get_key.go @@ -3,7 +3,7 @@ package git_config import ( "bytes" "fmt" - "io/ioutil" + "io" "os/exec" "strings" "syscall" @@ -38,7 +38,7 @@ import ( func runGitConfigCmd(cmd *exec.Cmd) (string, error) { var stdout bytes.Buffer cmd.Stdout = &stdout - cmd.Stderr = ioutil.Discard + cmd.Stderr = io.Discard err := cmd.Run() if exitError, ok := err.(*exec.ExitError); ok { diff --git a/pkg/commands/loaders/commits.go b/pkg/commands/loaders/commits.go index 69c88ccf5..5f33408cd 100644 --- a/pkg/commands/loaders/commits.go +++ b/pkg/commands/loaders/commits.go @@ -3,7 +3,6 @@ package loaders import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -50,7 +49,7 @@ func NewCommitLoader( cmd: cmd, getCurrentBranchName: getCurrentBranchName, getRebaseMode: getRebaseMode, - readFile: ioutil.ReadFile, + readFile: os.ReadFile, walkFiles: filepath.Walk, dotGitDir: dotGitDir, } diff --git a/pkg/commands/loaders/files.go b/pkg/commands/loaders/files.go index db37da935..6d14bb590 100644 --- a/pkg/commands/loaders/files.go +++ b/pkg/commands/loaders/files.go @@ -87,7 +87,7 @@ func (c *FileLoader) GitStatus(opts GitStatusOptions) ([]FileStatus, error) { noRenamesFlag = " --no-renames" } - statusLines, err := c.cmd.New(fmt.Sprintf("git status %s --porcelain -z%s", opts.UntrackedFilesArg, noRenamesFlag)).DontLog().RunWithOutput() + statusLines, _, err := c.cmd.New(fmt.Sprintf("git status %s --porcelain -z%s", opts.UntrackedFilesArg, noRenamesFlag)).DontLog().RunWithOutputs() if err != nil { return []FileStatus{}, err } diff --git a/pkg/commands/oscommands/cmd_obj.go b/pkg/commands/oscommands/cmd_obj.go index 1a801c6fe..47a227f17 100644 --- a/pkg/commands/oscommands/cmd_obj.go +++ b/pkg/commands/oscommands/cmd_obj.go @@ -22,6 +22,8 @@ type ICmdObj interface { Run() error // runs the command and returns the output as a string, and an error if any RunWithOutput() (string, error) + // runs the command and returns stdout and stderr as a string, and an error if any + RunWithOutputs() (string, string, error) // runs the command and runs a callback function on each line of the output. If the callback returns true for the boolean value, we kill the process and return. RunAndProcessLines(onLine func(line string) (bool, error)) error @@ -162,6 +164,10 @@ func (self *CmdObj) RunWithOutput() (string, error) { return self.runner.RunWithOutput(self) } +func (self *CmdObj) RunWithOutputs() (string, string, error) { + return self.runner.RunWithOutputs(self) +} + func (self *CmdObj) RunAndProcessLines(onLine func(line string) (bool, error)) error { return self.runner.RunAndProcessLines(self, onLine) } diff --git a/pkg/commands/oscommands/cmd_obj_runner.go b/pkg/commands/oscommands/cmd_obj_runner.go index ba1489fe6..01cc0f460 100644 --- a/pkg/commands/oscommands/cmd_obj_runner.go +++ b/pkg/commands/oscommands/cmd_obj_runner.go @@ -15,6 +15,7 @@ import ( type ICmdObjRunner interface { Run(cmdObj ICmdObj) error RunWithOutput(cmdObj ICmdObj) (string, error) + RunWithOutputs(cmdObj ICmdObj) (string, string, error) RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error } @@ -76,6 +77,31 @@ func (self *cmdObjRunner) RunWithOutput(cmdObj ICmdObj) (string, error) { return self.RunWithOutputAux(cmdObj) } +func (self *cmdObjRunner) RunWithOutputs(cmdObj ICmdObj) (string, string, error) { + if cmdObj.Mutex() != nil { + cmdObj.Mutex().Lock() + defer cmdObj.Mutex().Unlock() + } + + if cmdObj.GetCredentialStrategy() != NONE { + err := self.runWithCredentialHandling(cmdObj) + // for now we're not capturing output, just because it would take a little more + // effort and there's currently no use case for it. Some commands call RunWithOutputs + // but ignore the output, hence why we've got this check here. + return "", "", err + } + + if cmdObj.ShouldStreamOutput() { + err := self.runAndStream(cmdObj) + // for now we're not capturing output, just because it would take a little more + // effort and there's currently no use case for it. Some commands call RunWithOutputs + // but ignore the output, hence why we've got this check here. + return "", "", err + } + + return self.RunWithOutputsAux(cmdObj) +} + func (self *cmdObjRunner) RunWithOutputAux(cmdObj ICmdObj) (string, error) { self.log.WithField("command", cmdObj.ToString()).Debug("RunCommand") @@ -90,6 +116,28 @@ func (self *cmdObjRunner) RunWithOutputAux(cmdObj ICmdObj) (string, error) { return output, err } +func (self *cmdObjRunner) RunWithOutputsAux(cmdObj ICmdObj) (string, string, error) { + self.log.WithField("command", cmdObj.ToString()).Debug("RunCommand") + + if cmdObj.ShouldLog() { + self.logCmdObj(cmdObj) + } + + var outBuffer, errBuffer bytes.Buffer + cmd := cmdObj.GetCmd() + cmd.Stdout = &outBuffer + cmd.Stderr = &errBuffer + err := cmd.Run() + + stdout := outBuffer.String() + stderr, err := sanitisedCommandOutput(errBuffer.Bytes(), err) + if err != nil { + self.log.WithField("command", cmdObj.ToString()).Error(stderr) + } + + return stdout, stderr, err +} + func (self *cmdObjRunner) RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error { if cmdObj.Mutex() != nil { cmdObj.Mutex().Lock() diff --git a/pkg/commands/oscommands/fake_cmd_obj_runner.go b/pkg/commands/oscommands/fake_cmd_obj_runner.go index d06861251..f553d8c63 100644 --- a/pkg/commands/oscommands/fake_cmd_obj_runner.go +++ b/pkg/commands/oscommands/fake_cmd_obj_runner.go @@ -44,6 +44,11 @@ func (self *FakeCmdObjRunner) RunWithOutput(cmdObj ICmdObj) (string, error) { return output, err } +func (self *FakeCmdObjRunner) RunWithOutputs(cmdObj ICmdObj) (string, string, error) { + output, err := self.RunWithOutput(cmdObj) + return output, "", err +} + func (self *FakeCmdObjRunner) RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error { output, err := self.RunWithOutput(cmdObj) if err != nil { diff --git a/pkg/commands/oscommands/gui_io.go b/pkg/commands/oscommands/gui_io.go index 9540c13e1..10a8b2678 100644 --- a/pkg/commands/oscommands/gui_io.go +++ b/pkg/commands/oscommands/gui_io.go @@ -2,7 +2,6 @@ package oscommands import ( "io" - "io/ioutil" "github.com/sirupsen/logrus" ) @@ -45,7 +44,7 @@ func NewNullGuiIO(log *logrus.Entry) *guiIO { return &guiIO{ log: log, logCommandFn: func(string, bool) {}, - newCmdWriterFn: func() io.Writer { return ioutil.Discard }, + newCmdWriterFn: func() io.Writer { return io.Discard }, promptForCredentialFn: failPromptFn, } } diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 2a7cc1328..39149ce84 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -2,7 +2,7 @@ package oscommands import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -151,7 +151,7 @@ func (c *OSCommand) CreateFileWithContent(path string, content string) error { return err } - if err := ioutil.WriteFile(path, []byte(content), 0o644); err != nil { + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { c.Log.Error(err) return utils.WrapError(err) } @@ -215,7 +215,7 @@ func (c *OSCommand) PipeCommands(commandStrings ...string) error { c.Log.Error(err) } - if b, err := ioutil.ReadAll(stderr); err == nil { + if b, err := io.ReadAll(stderr); err == nil { if len(b) > 0 { finalErrors = append(finalErrors, string(b)) } diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 969224405..e9fc91424 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -1,7 +1,6 @@ package oscommands import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -149,7 +148,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { { filepath.Join(os.TempDir(), "testFile"), func(path string) { - if err := ioutil.WriteFile(path, []byte("hello"), 0o600); err != nil { + if err := os.WriteFile(path, []byte("hello"), 0o600); err != nil { panic(err) } }, @@ -160,7 +159,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { { filepath.Join(os.TempDir(), "emptyTestFile"), func(path string) { - if err := ioutil.WriteFile(path, []byte(""), 0o600); err != nil { + if err := os.WriteFile(path, []byte(""), 0o600); err != nil { panic(err) } }, @@ -171,7 +170,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { { filepath.Join(os.TempDir(), "testFileWithNewline"), func(path string) { - if err := ioutil.WriteFile(path, []byte("hello\n"), 0o600); err != nil { + if err := os.WriteFile(path, []byte("hello\n"), 0o600); err != nil { panic(err) } }, @@ -187,7 +186,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { if err := osCommand.AppendLineToFile(s.path, "world"); err != nil { panic(err) } - f, err := ioutil.ReadFile(s.path) + f, err := os.ReadFile(s.path) if err != nil { panic(err) } |