aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>2016-03-01 13:21:33 -0300
committerAndrew Gerrand <adg@golang.org>2016-05-28 03:12:11 +0000
commit13ec236026ca7434479884eab6a8e4ea7047aaa5 (patch)
tree20791aa4c1eac13dd495412dd127ce67e2984a79
parentb7f20f28bc89a6599ce258f2705ac8e1351ab28d (diff)
downloadgo-x-review-13ec236026ca7434479884eab6a8e4ea7047aaa5.tar.xz
git-codereview: make change fail with multiple pending changes
When using 'git change' to amend the current change, it will fail if there are multiple pending changes. The previous behavior (amending the latest) wasn't always what the user intended. $ git change git-codereview: cannot amend change: multiple changes pending: fc60fd5 test: dummy change 6440cdc git-codereview: make change fail with multiple pending changes $ Unlike mail and submit, change doesn't support explicitly telling which commit to work on. Either 'git commit --amend' (for updating the topmost change) or a combination of 'git commit' and 'git rebase' must be used. Fixes golang/go#10443. Change-Id: I2f6ea1b0e302b66b627761d72d031a3b6aa0fba2 Reviewed-on: https://go-review.googlesource.com/20049 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
-rw-r--r--git-codereview/branch.go12
-rw-r--r--git-codereview/change.go4
-rw-r--r--git-codereview/change_test.go26
-rw-r--r--git-codereview/mail.go2
-rw-r--r--git-codereview/submit.go2
5 files changed, 38 insertions, 8 deletions
diff --git a/git-codereview/branch.go b/git-codereview/branch.go
index 8cf1566..c96bce0 100644
--- a/git-codereview/branch.go
+++ b/git-codereview/branch.go
@@ -335,8 +335,9 @@ func (b *Branch) CommitByRev(action, rev string) *Commit {
// DefaultCommit returns the default pending commit for this branch.
// It dies if there is not exactly one pending commit,
-// using the action ("mail", "submit") in the failure message.
-func (b *Branch) DefaultCommit(action string) *Commit {
+// using the action (e.g. "mail", "submit") and optional extra instructions
+// in the failure message.
+func (b *Branch) DefaultCommit(action, extra string) *Commit {
work := b.Pending()
if len(work) == 0 {
dief("cannot %s: no changes pending", action)
@@ -346,11 +347,10 @@ func (b *Branch) DefaultCommit(action string) *Commit {
for _, c := range work {
fmt.Fprintf(&buf, "\n\t%s %s", c.ShortHash, c.Subject)
}
- extra := ""
- if action == "submit" {
- extra = " or use submit -i"
+ if extra != "" {
+ extra = "; " + extra
}
- dief("cannot %s: multiple changes pending; must specify commit on command line%s:%s", action, extra, buf.String())
+ dief("cannot %s: multiple changes pending%s:%s", action, extra, buf.String())
}
return work[0]
}
diff --git a/git-codereview/change.go b/git-codereview/change.go
index 4c0e252..0d47857 100644
--- a/git-codereview/change.go
+++ b/git-codereview/change.go
@@ -43,6 +43,10 @@ func cmdChange(args []string) {
}
amend := b.HasPendingCommit()
+ if amend {
+ // Dies if there is not exactly one commit.
+ b.DefaultCommit("amend change", "")
+ }
commitChanges(amend)
b.loadedPending = false // force reload after commitChanges
b.check()
diff --git a/git-codereview/change_test.go b/git-codereview/change_test.go
index 524c021..05e5c82 100644
--- a/git-codereview/change_test.go
+++ b/git-codereview/change_test.go
@@ -76,3 +76,29 @@ func TestMessageRE(t *testing.T) {
}
}
}
+
+func TestChangeAmendCommit(t *testing.T) {
+ gt := newGitTest(t)
+ defer gt.done()
+
+ testCommitMsg = "foo: amended commit message"
+ gt.work(t)
+
+ write(t, gt.client+"/file", "new content in work to be amend")
+ trun(t, gt.client, "git", "add", "file")
+ testMain(t, "change")
+}
+
+func TestChangeFailAmendWithMultiplePending(t *testing.T) {
+ gt := newGitTest(t)
+ defer gt.done()
+
+ testCommitMsg = "foo: amended commit message"
+ gt.work(t)
+ gt.work(t)
+
+ write(t, gt.client+"/file", "new content in work to be amend")
+ trun(t, gt.client, "git", "add", "file")
+ testMainDied(t, "change")
+ testPrintedStderr(t, "multiple changes pending")
+}
diff --git a/git-codereview/mail.go b/git-codereview/mail.go
index 4c40968..040ef19 100644
--- a/git-codereview/mail.go
+++ b/git-codereview/mail.go
@@ -39,7 +39,7 @@ func cmdMail(args []string) {
if len(flags.Args()) == 1 {
c = b.CommitByRev("mail", flags.Arg(0))
} else {
- c = b.DefaultCommit("mail")
+ c = b.DefaultCommit("mail", "must specify commit on command line")
}
if *diff {
diff --git a/git-codereview/submit.go b/git-codereview/submit.go
index d163914..78c6339 100644
--- a/git-codereview/submit.go
+++ b/git-codereview/submit.go
@@ -42,7 +42,7 @@ func cmdSubmit(args []string) {
cs = append(cs, b.CommitByRev("submit", arg))
}
} else {
- cs = append(cs, b.DefaultCommit("submit"))
+ cs = append(cs, b.DefaultCommit("submit", "must specify commit on command line or use submit -i"))
}
// No staged changes.