diff options
| author | Russ Cox <rsc@golang.org> | 2014-12-22 13:44:36 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2014-12-23 17:04:01 +0000 |
| commit | 7bfb5ee3c2402269776c1d835036ac35d1e7e868 (patch) | |
| tree | 42bb2f21a2ca0b2b5327d33d3c60263221b9ca9a /git-codereview/branch.go | |
| parent | ce23a4d596e8659c3c76931fd00b2d7a092f3fc5 (diff) | |
| download | go-x-review-7bfb5ee3c2402269776c1d835036ac35d1e7e868.tar.xz | |
git-codereview: fix a few corner case failures
- make it clearer what the random git command at the end of failures means
- avoid some problems with detached HEAD mode in hooks run during git rebase
- redirect all stdout/stderr into test buffers
Change-Id: I102f22fc870f69c884728eaa46ecc95792d5b795
Reviewed-on: https://go-review.googlesource.com/2011
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'git-codereview/branch.go')
| -rw-r--r-- | git-codereview/branch.go | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/git-codereview/branch.go b/git-codereview/branch.go index 9c9d8e7..40288c7 100644 --- a/git-codereview/branch.go +++ b/git-codereview/branch.go @@ -7,7 +7,6 @@ package main import ( "bytes" "fmt" - "os" "os/exec" "regexp" "strings" @@ -34,10 +33,24 @@ func CurrentBranch() *Branch { return &Branch{Name: name} } +// DetachedHead reports whether branch b corresponds to a detached HEAD +// (does not have a real branch name). +func (b *Branch) DetachedHead() bool { + return b.Name == "HEAD" +} + // OriginBranch returns the name of the origin branch that branch b tracks. // The returned name is like "origin/master" or "origin/dev.garbage" or // "origin/release-branch.go1.4". func (b *Branch) OriginBranch() string { + if b.DetachedHead() { + // Detached head mode. + // "origin/HEAD" is clearly false, but it should be easy to find when it + // appears in other commands. Really any caller of OriginBranch + // should check for detached head mode. + return "origin/HEAD" + } + if b.originBranch != "" { return b.originBranch } @@ -52,7 +65,7 @@ func (b *Branch) OriginBranch() string { b.originBranch = "origin/master" return b.originBranch } - fmt.Fprintf(os.Stderr, "%v\n%s\n", commandString(argv[0], argv[1:]), out) + fmt.Fprintf(stderr(), "%v\n%s\n", commandString(argv[0], argv[1:]), out) dief("%v", err) panic("not reached") } @@ -99,6 +112,10 @@ func (b *Branch) loadPending() { } b.loadedPending = true + if b.DetachedHead() { + return + } + const numField = 5 all := getOutput("git", "log", "--format=format:%H%x00%h%x00%P%x00%s%x00%B%x00", b.OriginBranch()+".."+b.Name) fields := strings.Split(all, "\x00") @@ -192,8 +209,20 @@ func LocalChanges() (staged, unstaged, untracked []string) { func LocalBranches() []*Branch { var branches []*Branch + current := CurrentBranch() for _, s := range getLines("git", "branch", "-q") { - s = strings.TrimPrefix(strings.TrimSpace(s), "* ") + s = strings.TrimSpace(s) + if strings.HasPrefix(s, "* ") { + // * marks current branch in output. + // Normally the current branch has a name like any other, + // but in detached HEAD mode the branch listing shows + // a localized (translated) textual description instead of + // a branch name. Avoid language-specific differences + // by using CurrentBranch().Name for the current branch. + // It detects detached HEAD mode in a more portable way. + // (git rev-parse --abbrev-ref HEAD returns 'HEAD'). + s = current.Name + } branches = append(branches, &Branch{Name: s}) } return branches |
