review/git-review: apply changes from adg's review

From golang.org/cl/1518.
While we are here, fix HasStagedChanges and HasUnstagedChanges
so that git submit reports the changes. Tests to follow in a non-TBR review.

Change-Id: I68fb18f53c1b5e8a71f88e7827b4888e2afd6f36
Reviewed-on: https://go-review.googlesource.com/1585
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/git-review/branch.go b/git-review/branch.go
index 054191f..8c31d8a 100644
--- a/git-review/branch.go
+++ b/git-review/branch.go
@@ -104,7 +104,8 @@
 var stagedRE = regexp.MustCompile(`^[ACDMR]  `)
 
 func HasStagedChanges() bool {
-	for _, s := range getLines("git", "status", "-b", "--porcelain") {
+	// NOTE: Cannot use getLines, because it throws away leading spaces.
+	for _, s := range strings.Split(getOutput("git", "status", "-b", "--porcelain"), "\n") {
 		if stagedRE.MatchString(s) {
 			return true
 		}
@@ -112,10 +113,11 @@
 	return false
 }
 
-var unstagedRE = regexp.MustCompile(`^.[ACDMR] `)
+var unstagedRE = regexp.MustCompile(`^.[ACDMR]`)
 
 func HasUnstagedChanges() bool {
-	for _, s := range getLines("git", "status", "-b", "--porcelain") {
+	// NOTE: Cannot use getLines, because it throws away leading spaces.
+	for _, s := range strings.Split(getOutput("git", "status", "-b", "--porcelain"), "\n") {
 		if unstagedRE.MatchString(s) {
 			return true
 		}
diff --git a/git-review/submit.go b/git-review/submit.go
index 8db32ad..0d7940d 100644
--- a/git-review/submit.go
+++ b/git-review/submit.go
@@ -89,11 +89,14 @@
 		dief("cannot submit: conflicting changes submitted, run 'git sync'")
 	}
 
+	if *noRun {
+		dief("stopped before submit")
+	}
+
 	// Otherwise, try the submit. Sends back updated GerritChange,
 	// but we need extended information and the reply is in the
 	// "SUBMITTED" state anyway, so ignore the GerritChange
 	// in the response and fetch a new one below.
-	ch = new(GerritChange)
 	if err := gerritAPI("/a/changes/"+fullChangeID(b)+"/submit", []byte(`{"wait_for_merge": true}`), nil); err != nil {
 		dief("cannot submit: %v", err)
 	}
@@ -110,7 +113,7 @@
 		time.Sleep(max * (1 << uint(i+1)) / (1 << steps))
 		ch, err = b.GerritChange()
 		if err != nil {
-			dief("%v", err)
+			dief("waiting for merge: %v", err)
 		}
 		if ch.Status != "SUBMITTED" {
 			break
@@ -119,7 +122,7 @@
 
 	switch ch.Status {
 	default:
-		dief("cannot submit: unexpected post-submit Gerrit change status %q", ch.Status)
+		dief("submit error: unexpected post-submit Gerrit change status %q", ch.Status)
 
 	case "MERGED":
 		// good
@@ -140,3 +143,4 @@
 
 	// Done! Change is submitted, branch is up to date, ready for new work.
 }
+
diff --git a/git-review/submit_test.go b/git-review/submit_test.go
index c818e53..66514bd 100644
--- a/git-review/submit_test.go
+++ b/git-review/submit_test.go
@@ -38,6 +38,7 @@
 		"git status", "git stash", "git add", "git-review change")
 	testNoStdout(t)
 	testRan(t)
+	trun(t, gt.client, "git", "add", "file1")
 	trun(t, gt.client, "git", "commit", "--amend", "--no-edit")
 
 	t.Logf("> not found")
@@ -96,7 +97,7 @@
 	srv.setReply("/a/changes/proj~master~I123456789/submit", gerritReply{body: ")]}'\n" + newJSON})
 	testMainDied(t, "submit")
 	testRan(t, "git push -q origin HEAD:refs/for/master")
-	testPrintedStderr(t, "cannot submit: unexpected post-submit Gerrit change status \"NEW\"")
+	testPrintedStderr(t, "submit error: unexpected post-submit Gerrit change status \"NEW\"")
 }
 
 func TestSubmitTimeout(t *testing.T) {