git-codereview: fix sync-branch to work in worktrees

Change-Id: I8a3bcad53eae3273dc498ba94d53fe2d50b58520
Reviewed-on: https://go-review.googlesource.com/c/review/+/290212
Trust: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Filippo Valsorda <filippo@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/git-codereview/sync.go b/git-codereview/sync.go
index f8233f1..0555d67 100644
--- a/git-codereview/sync.go
+++ b/git-codereview/sync.go
@@ -10,7 +10,6 @@
 	"fmt"
 	"io/ioutil"
 	"os"
-	"path/filepath"
 	"strings"
 )
 
@@ -104,7 +103,7 @@
 }
 
 func syncBranchStatusFile() string {
-	return filepath.Join(repoRoot(), ".git/codereview-sync-branch-status")
+	return gitPath("codereview-sync-branch-status")
 }
 
 func readSyncBranchStatus() *syncBranchStatus {
diff --git a/git-codereview/sync_test.go b/git-codereview/sync_test.go
index 884168c..1dbd7bf 100644
--- a/git-codereview/sync_test.go
+++ b/git-codereview/sync_test.go
@@ -7,6 +7,7 @@
 import (
 	"bytes"
 	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strings"
 	"testing"
@@ -238,6 +239,29 @@
 		"Run 'git codereview mail' to send for review.")
 }
 
+func TestSyncBranchWorktree(t *testing.T) {
+	gt := newGitTest(t)
+	defer gt.done()
+
+	gt.serverWork(t)
+	gt.serverWork(t)
+	trun(t, gt.server, "git", "checkout", "dev.branch")
+	gt.serverWorkUnrelated(t, "")
+	gt.serverWorkUnrelated(t, "")
+	gt.serverWorkUnrelated(t, "")
+	trun(t, gt.server, "git", "checkout", "main")
+
+	wt := filepath.Join(gt.tmpdir, "git-worktree")
+	trun(t, gt.client, "git", "worktree", "add", "-b", "dev.branch", wt, "origin/dev.branch")
+	if err := os.Chdir(wt); err != nil {
+		t.Fatal(err)
+	}
+
+	testMain(t, "sync-branch")
+	testHideRevHashes(t)
+	testPrintedStdout(t, "[dev.branch] all: merge main (REVHASH) into dev.branch")
+}
+
 func TestSyncBranchMergeBack(t *testing.T) {
 	gt := newGitTest(t)
 	defer gt.done()