git-codereview: change upstream fallback to try main before master
Will help when we start using origin/main in Go repos.
(Tested using git-codereview against github.com/google/licensecheck.)
Change-Id: I564a0f6863938a3196dea3df9e8af240faba91fc
Reviewed-on: https://go-review.googlesource.com/c/review/+/254420
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/git-codereview/branch.go b/git-codereview/branch.go
index a332256..2bd9fe9 100644
--- a/git-codereview/branch.go
+++ b/git-codereview/branch.go
@@ -89,6 +89,18 @@
// Have seen both "No upstream configured" and "no upstream configured".
if strings.Contains(string(out), "upstream configured") {
// Assume branch was created before we set upstream correctly.
+ // See if origin/main exists; if so, use it.
+ // Otherwise, fall back to origin/master.
+ argv := []string{"git", "rev-parse", "--abbrev-ref", "origin/main"}
+ cmd := exec.Command(argv[0], argv[1:]...)
+ setEnglishLocale(cmd)
+ if out, err := cmd.CombinedOutput(); err == nil {
+ b.originBranch = string(bytes.TrimSpace(out))
+ // Best effort attempt to correct setting for next time,
+ // and for "git status".
+ exec.Command("git", "branch", "-u", "origin/main").Run()
+ return b.originBranch
+ }
b.originBranch = "origin/master"
return b.originBranch
}