diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-10-10 17:50:55 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-10 17:50:55 +1100 |
commit | d5f64602a8e10ab5c8162140d426ce677fe884ee (patch) | |
tree | aab5bee3d464089603a8117a1206b79933f8e8f7 /pkg/commands | |
parent | b8b59baa2700a6fc20f7f7077d494323fef851fa (diff) | |
parent | 4287f8ae905237abf3de181461255406c6d0b51d (diff) |
Merge pull request #283 from kristijanhusak/feature/commit-amendv0.4
Add action for amending a commit
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 14 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 81 |
2 files changed, 84 insertions, 11 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 402ec3378..0e974b567 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -221,11 +221,11 @@ func (c *GitCommand) ResetHard() error { // UpstreamDifferenceCount checks how many pushables/pullables there are for the // current branch func (c *GitCommand) UpstreamDifferenceCount() (string, string) { - pushableCount, err := c.OSCommand.RunCommandWithOutput("git rev-list @{u}..head --count") + pushableCount, err := c.OSCommand.RunCommandWithOutput("git rev-list @{u}..HEAD --count") if err != nil { return "?", "?" } - pullableCount, err := c.OSCommand.RunCommandWithOutput("git rev-list head..@{u} --count") + pullableCount, err := c.OSCommand.RunCommandWithOutput("git rev-list HEAD..@{u} --count") if err != nil { return "?", "?" } @@ -236,7 +236,7 @@ func (c *GitCommand) UpstreamDifferenceCount() (string, string) { // to the remote branch of the current branch, a map is returned to ease look up func (c *GitCommand) GetCommitsToPush() map[string]bool { pushables := map[string]bool{} - o, err := c.OSCommand.RunCommandWithOutput("git rev-list @{u}..head --abbrev-commit") + o, err := c.OSCommand.RunCommandWithOutput("git rev-list @{u}..HEAD --abbrev-commit") if err != nil { return pushables } @@ -314,8 +314,12 @@ func (c *GitCommand) usingGpg() bool { } // Commit commits to git -func (c *GitCommand) Commit(message string) (*exec.Cmd, error) { - command := fmt.Sprintf("git commit -m %s", c.OSCommand.Quote(message)) +func (c *GitCommand) Commit(message string, amend bool) (*exec.Cmd, error) { + amendParam := "" + if amend { + amendParam = " --amend" + } + command := fmt.Sprintf("git commit%s -m %s", amendParam, c.OSCommand.Quote(message)) if c.usingGpg() { return c.OSCommand.PrepareSubProcess(c.OSCommand.Platform.shell, c.OSCommand.Platform.shellArg, command), nil } diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 25820558a..ebb9de136 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -561,7 +561,7 @@ func TestGitCommandUpstreamDifferentCount(t *testing.T) { { "Can't retrieve pullable count", func(cmd string, args ...string) *exec.Cmd { - if args[1] == "head..@{u}" { + if args[1] == "HEAD..@{u}" { return exec.Command("test") } @@ -575,7 +575,7 @@ func TestGitCommandUpstreamDifferentCount(t *testing.T) { { "Retrieve pullable and pushable count", func(cmd string, args ...string) *exec.Cmd { - if args[1] == "head..@{u}" { + if args[1] == "HEAD..@{u}" { return exec.Command("echo", "10") } @@ -889,7 +889,76 @@ func TestGitCommandCommit(t *testing.T) { gitCmd := newDummyGitCommand() gitCmd.getGlobalGitConfig = s.getGlobalGitConfig gitCmd.OSCommand.command = s.command - s.test(gitCmd.Commit("test")) + s.test(gitCmd.Commit("test", false)) + }) + } +} + +func TestGitCommandCommitAmendFromFiles(t *testing.T) { + type scenario struct { + testName string + command func(string, ...string) *exec.Cmd + getGlobalGitConfig func(string) (string, error) + test func(*exec.Cmd, error) + } + + scenarios := []scenario{ + { + "Amend commit using gpg", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "bash", cmd) + assert.EqualValues(t, []string{"-c", `git commit --amend -m 'test'`}, args) + + return exec.Command("echo") + }, + func(string) (string, error) { + return "true", nil + }, + func(cmd *exec.Cmd, err error) { + assert.NotNil(t, cmd) + assert.Nil(t, err) + }, + }, + { + "Amend commit without using gpg", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args) + + return exec.Command("echo") + }, + func(string) (string, error) { + return "false", nil + }, + func(cmd *exec.Cmd, err error) { + assert.Nil(t, cmd) + assert.Nil(t, err) + }, + }, + { + "Amend commit without using gpg with an error", + func(cmd string, args ...string) *exec.Cmd { + assert.EqualValues(t, "git", cmd) + assert.EqualValues(t, []string{"commit", "--amend", "-m", "test"}, args) + + return exec.Command("test") + }, + func(string) (string, error) { + return "false", nil + }, + func(cmd *exec.Cmd, err error) { + assert.Nil(t, cmd) + assert.Error(t, err) + }, + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + gitCmd := newDummyGitCommand() + gitCmd.getGlobalGitConfig = s.getGlobalGitConfig + gitCmd.OSCommand.command = s.command + s.test(gitCmd.Commit("test", true)) }) } } @@ -1507,7 +1576,7 @@ func TestGitCommandGetCommits(t *testing.T) { switch args[0] { case "rev-list": - assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args) + assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo") case "log": assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args) @@ -1534,7 +1603,7 @@ func TestGitCommandGetCommits(t *testing.T) { switch args[0] { case "rev-list": - assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args) + assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo", "8a2bb0e") case "log": assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args) @@ -1577,7 +1646,7 @@ func TestGitCommandGetCommits(t *testing.T) { switch args[0] { case "rev-list": - assert.EqualValues(t, []string{"rev-list", "@{u}..head", "--abbrev-commit"}, args) + assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo", "8a2bb0e") case "log": assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args) |