diff --git a/pkg/commands/git.go b/pkg/commands/git.go index c200a688b..8756a0483 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -337,38 +337,33 @@ func (c *GitCommand) SquashPreviousTwoCommits(message string) error { // SquashFixupCommit squashes a 'FIXUP' commit into the commit beneath it, // retaining the commit message of the lower commit func (c *GitCommand) SquashFixupCommit(branchName string, shaValue string) error { - var err error commands := []string{ - "git checkout -q " + shaValue, - "git reset --soft " + shaValue + "^", - "git commit --amend -C " + shaValue + "^", - "git rebase --onto HEAD " + shaValue + " " + branchName, + fmt.Sprintf("git checkout -q %s", shaValue), + fmt.Sprintf("git reset --soft %s^", shaValue), + fmt.Sprintf("git commit --amend -C %s^", shaValue), + fmt.Sprintf("git rebase --onto HEAD %s %s", shaValue, branchName), } - ret := "" for _, command := range commands { c.Log.Info(command) - output, err := c.OSCommand.RunCommandWithOutput(command) - ret += output - if err != nil { + + if output, err := c.OSCommand.RunCommandWithOutput(command); err != nil { + ret := output + // We are already in an error state here so we're just going to append + // the output of these commands + output, _ := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git branch -d %s", shaValue)) + ret += output + output, _ = c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git checkout %s", branchName)) + ret += output + c.Log.Info(ret) - break + return errors.New(ret) } } - if err != nil { - // We are already in an error state here so we're just going to append - // the output of these commands - output, _ := c.OSCommand.RunCommandWithOutput("git branch -d " + shaValue) - ret += output - output, _ = c.OSCommand.RunCommandWithOutput("git checkout " + branchName) - ret += output - } - if err != nil { - return errors.New(ret) - } + return nil } -// CatFile obtain the contents of a file +// CatFile obtains the content of a file func (c *GitCommand) CatFile(fileName string) (string, error) { return c.OSCommand.RunCommandWithOutput("cat " + c.OSCommand.Quote(fileName)) } diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 0bcbdb7dd..bc0202df9 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1016,6 +1016,69 @@ func TestGitCommandSquashPreviousTwoCommits(t *testing.T) { } } +func TestGitCommandSquashFixupCommit(t *testing.T) { + type scenario struct { + testName string + command func() (func(string, ...string) *exec.Cmd, *[][]string) + test func(*[][]string, error) + } + + scenarios := []scenario{ + { + "An error occurred with one of the sub git command", + func() (func(string, ...string) *exec.Cmd, *[][]string) { + cmdsCalled := [][]string{} + return func(cmd string, args ...string) *exec.Cmd { + cmdsCalled = append(cmdsCalled, args) + if len(args) > 0 && args[0] == "checkout" { + return exec.Command("exit", "1") + } + + return exec.Command("echo") + }, &cmdsCalled + }, + func(cmdsCalled *[][]string, err error) { + assert.NotNil(t, err) + assert.Len(t, *cmdsCalled, 3) + assert.EqualValues(t, *cmdsCalled, [][]string{ + {"checkout", "-q", "6789abcd"}, + {"branch", "-d", "6789abcd"}, + {"checkout", "test"}, + }) + }, + }, + { + "Squash fixup succeeded", + func() (func(string, ...string) *exec.Cmd, *[][]string) { + cmdsCalled := [][]string{} + return func(cmd string, args ...string) *exec.Cmd { + cmdsCalled = append(cmdsCalled, args) + return exec.Command("echo") + }, &cmdsCalled + }, + func(cmdsCalled *[][]string, err error) { + assert.Nil(t, err) + assert.Len(t, *cmdsCalled, 4) + assert.EqualValues(t, *cmdsCalled, [][]string{ + {"checkout", "-q", "6789abcd"}, + {"reset", "--soft", "6789abcd^"}, + {"commit", "--amend", "-C", "6789abcd^"}, + {"rebase", "--onto", "HEAD", "6789abcd", "test"}, + }) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + var cmdsCalled *[][]string + gitCmd := newDummyGitCommand() + gitCmd.OSCommand.command, cmdsCalled = s.command() + s.test(cmdsCalled, gitCmd.SquashFixupCommit("test", "6789abcd")) + }) + } +} + func TestGitCommandDiff(t *testing.T) { gitCommand := newDummyGitCommand() assert.NoError(t, test.GenerateRepo("lots_of_diffs.sh"))