git-codereview: deflake TestSyncRebase

In order to guarantee different hashes on "client" and "server" we need
to generate different commit histories.

Fixes golang/go#10048, updates golang/go#9602.

Change-Id: I42a50dc890598dd3a3b6c626c6db70ffe06c14d3
Reviewed-on: https://go-review.googlesource.com/9256
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/git-codereview/sync_test.go b/git-codereview/sync_test.go
index 2f7318d..88461b3 100644
--- a/git-codereview/sync_test.go
+++ b/git-codereview/sync_test.go
@@ -73,8 +73,10 @@
 		t.Fatalf("CL hashes changed during no-op sync")
 	}
 
-	// submit first two CLs - gt.serverWork does same thing gt.work does, but on client
+	// submit first two CLs - gt.serverWork does same thing gt.work does, but on server
+
 	gt.serverWork(t)
+	gt.serverWorkUnrelated(t) // wedge in unrelated work to get different hashes
 	gt.serverWork(t)
 
 	testMain(t, "sync")
diff --git a/git-codereview/util_test.go b/git-codereview/util_test.go
index 034d052..ea7cf8f 100644
--- a/git-codereview/util_test.go
+++ b/git-codereview/util_test.go
@@ -6,6 +6,7 @@
 
 import (
 	"bytes"
+	"encoding/hex"
 	"fmt"
 	"io/ioutil"
 	"net"
@@ -26,6 +27,7 @@
 	client      string // client repo root
 	nwork       int    // number of calls to work method
 	nworkServer int    // number of calls to serverWork method
+	nworkOther  int    // number of calls to serverWorkUnrelated method
 }
 
 // resetReadOnlyFlagAll resets windows read-only flag
@@ -61,6 +63,18 @@
 	os.RemoveAll(gt.tmpdir)
 }
 
+// doWork simulates commit 'n' touching 'file' in 'dir'
+func doWork(t *testing.T, n int, dir, file string) {
+	write(t, dir+"/"+file, fmt.Sprintf("new content %d", n))
+	trun(t, dir, "git", "add", file)
+	suffix := ""
+	if n > 1 {
+		suffix = fmt.Sprintf(" #%d", n)
+	}
+	changeid := hex.EncodeToString([]byte(file))
+	trun(t, dir, "git", "commit", "-m", fmt.Sprintf("msg%s\n\nChange-Id: I%d%s\n", suffix, n, changeid))
+}
+
 func (gt *gitTest) work(t *testing.T) {
 	if gt.nwork == 0 {
 		trun(t, gt.client, "git", "checkout", "-b", "work")
@@ -70,29 +84,23 @@
 
 	// make local change on client
 	gt.nwork++
-	write(t, gt.client+"/file", fmt.Sprintf("new content %d", gt.nwork))
-	trun(t, gt.client, "git", "add", "file")
-	suffix := ""
-	if gt.nwork > 1 {
-		suffix = fmt.Sprintf(" #%d", gt.nwork)
-	}
-	trun(t, gt.client, "git", "commit", "-m", fmt.Sprintf("msg%s\n\nChange-Id: I%d23456789\n", suffix, gt.nwork))
+	doWork(t, gt.nwork, gt.client, "file")
 }
 
 func (gt *gitTest) serverWork(t *testing.T) {
 	// make change on server
-	// duplicating the changes of gt.work to simulate them
-	// having gone through Gerrit and submitted with
-	// different times and commit hashes but the same content.
+	// duplicating the sequence of changes in gt.work to simulate them
+	// having gone through Gerrit and submitted with possibly
+	// different commit hashes but the same content.
 	gt.nworkServer++
-	write(t, gt.server+"/file", fmt.Sprintf("new content %d", gt.nworkServer))
-	trun(t, gt.server, "git", "add", "file")
-	suffix := ""
-	if gt.nworkServer > 1 {
-		suffix = fmt.Sprintf(" #%d", gt.nworkServer)
-	}
-	trun(t, gt.server, "git", "commit", "-m", fmt.Sprintf("msg%s\n\nChange-Id: I%d23456789\n", suffix, gt.nworkServer))
+	doWork(t, gt.nworkServer, gt.server, "file")
+}
 
+func (gt *gitTest) serverWorkUnrelated(t *testing.T) {
+	// make unrelated change on server
+	// this makes history different on client and server
+	gt.nworkOther++
+	doWork(t, gt.nworkOther, gt.server, "otherfile")
 }
 
 func newGitTest(t *testing.T) (gt *gitTest) {