git-review: make 'git change topic' with no staged changes not commit

I expect that many people (myself included) will sometimes have in
mind what they're going to do and run 'git change topic' before making
any edits. Before this CL, what that does is create a new branch and
then print

	no staged changes. Did you forget to 'git add'?

and then put you in an editor to write a commit message for an empty
commit. You're going to have to run 'git change' later, after making
the edits and running git add, so might as well delay editing of the
commit message until then. It will be easier to write the commit
message when there are actual concrete changes to describe. The
current editor pop-up is annoying.

We already avoid the editor pop-up when changing to a branch that has
a pending commit. This CL avoids it also for changing to a branch when
there's nothing staged.

Fixes golang/go#9278

Change-Id: I3f913a88527dc1a4f6a237cbb9e0f3b58f823eec
Reviewed-on: https://go-review.googlesource.com/1427
Reviewed-by: Chris Manghane <cmang@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/git-review/change.go b/git-review/change.go
index 0990d58..9a7d7e0 100644
--- a/git-review/change.go
+++ b/git-review/change.go
@@ -26,6 +26,9 @@
 	target := flags.Arg(0)
 	if target != "" {
 		checkoutOrCreate(target)
+		if !HasStagedChanges() {
+			return
+		}
 	}
 
 	// Create or amend change commit.
@@ -39,7 +42,7 @@
 	}
 
 	if b.ChangeID() == "" {
-		// No change commit on this branch, create one.
+		// No change commit on this branch, create one if there are staged changes.
 		commitChanges(false)
 		return
 	}
diff --git a/git-review/change_test.go b/git-review/change_test.go
index ad33e7a..34c7a87 100644
--- a/git-review/change_test.go
+++ b/git-review/change_test.go
@@ -18,13 +18,19 @@
 	t.Logf("master -> work")
 	testMain(t, "change", "work")
 	testRan(t, "git checkout -q -b work",
-		"git branch -q --set-upstream-to origin/master",
-		"git commit -q --allow-empty -m foo: my commit msg")
+		"git branch -q --set-upstream-to origin/master")
 
 	t.Logf("work -> master")
 	testMain(t, "change", "master")
 	testRan(t, "git checkout -q master")
 
+	t.Logf("master -> work with staged changes")
+	write(t, gt.client+"/file", "new content")
+	trun(t, gt.client, "git", "add", "file")
+	testMain(t, "change", "work")
+	testRan(t, "git checkout -q work",
+		"git commit -q --allow-empty -m my commit msg")
+
 	t.Logf("master -> dev.branch")
 	testMain(t, "change", "dev.branch")
 	testRan(t, "git checkout -q -t -b dev.branch origin/dev.branch")