Skip to content
This repository has been archived by the owner on Feb 26, 2019. It is now read-only.

Commit

Permalink
Improve handling of git remote show origin
Browse files Browse the repository at this point in the history
This should help in cases where remote HEAD is ambiguous.
See the new tests. If we run across any other/different output we can
add it to the tests.
  • Loading branch information
Edward Muller committed May 27, 2016
1 parent c02e668 commit e532e14
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 7 deletions.
29 changes: 22 additions & 7 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,36 @@ func gitDetached(r string) (bool, error) {
}

func gitDefaultBranch(r string) (string, error) {
e := "Unable to determine default branch: "
hb := []byte("HEAD branch: ")
o, err := vcsGit.runOutput(r, "remote show origin")
if err != nil {
return "", errors.New(e + err.Error())
return "", errors.New("Running git remote show origin errored with: " + err.Error())
}
s := bytes.Index(o, hb)
return gitDetermineDefaultBranch(r, string(o))
}

func gitDetermineDefaultBranch(r, o string) (string, error) {
e := "Unable to determine HEAD branch: "
hb := "HEAD branch:"
lbcfgp := "Local branch configured for 'git pull':"
s := strings.Index(o, hb)
if s < 0 {
return "", errors.New(e + "git output missing '" + string(hb) + "'")
b := strings.Index(o, lbcfgp)
if b < 0 {
return "", errors.New(e + "Remote HEAD is ambiguous. Before godep can pull new commits you will need to:" + `
cd ` + r + `
git checkout <a HEAD branch>
Here is what was reported:
` + o)
}
s = b + len(lbcfgp)
} else {
s += len(hb)
}
f := bytes.Fields(o[s:])
f := strings.Fields(o[s:])
if len(f) < 3 {
return "", errors.New(e + "git output too short")
}
return string(f[2]), nil
return f[0], nil
}

func gitCheckout(r, b string) error {
Expand Down
94 changes: 94 additions & 0 deletions vcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import "testing"

func TestGitDetermineDefaultBranch(t *testing.T) {
cases := []struct {
r, o string
v string
err bool
}{
{"test",
`* remote origin
Fetch URL: https://gopkg.in/mgo.v2
Push URL: https://gopkg.in/mgo.v2
HEAD branch: v2
Remote branches:
master tracked
v2 tracked
v2-unstable tracked
Local branches configured for 'git pull':
master merges with remote master
v2 merges with remote v2
Local refs configured for 'git push':
master pushes to master (up to date)
v2 pushes to v2 (local out of date)
`, "v2", false},
{"test",
`* remote origin
Fetch URL: https://gopkg.in/bluesuncorp/validator.v5
Push URL: https://gopkg.in/bluesuncorp/validator.v5
HEAD branch (remote HEAD is ambiguous, may be one of the following):
master
v5
Remote branches:
krhubert tracked
master tracked
v4 tracked
v5 tracked
v5-development tracked
v6 tracked
v6-development tracked
v7 tracked
v7-development tracked
v8 tracked
v8-development tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
`, "master", false},
{"test",
`* remote origin
Fetch URL: https://github.com/gin-gonic/gin
Push URL: https://github.com/gin-gonic/gin
HEAD branch: develop
Remote branches:
benchmarks tracked
better-bind-errors tracked
develop tracked
fasthttp tracked
fix-binding tracked
fix-tests tracked
gh-pages tracked
honteng-bind_test tracked
master tracked
new-binding-validator tracked
new-catch-all tracked
performance tracked
routes-list tracked
Local branch configured for 'git pull':
develop merges with remote develop
Local ref configured for 'git push':
develop pushes to develop (local out of date)
`, "develop", false},
{"test", "", "", true},
}

for i, test := range cases {
v, e := gitDetermineDefaultBranch(test.r, test.o)
if v != test.v {
t.Errorf("%d Unexpected value returned: %s, wanted %s", i, v, test.v)
}
if e != nil {
t.Log("Err", e.Error())
}
if test.err && e == nil {
t.Errorf("%d Test should err, but didn't", i)
}

if !test.err && e != nil {
t.Errorf("%d Test shouldn't err, but did with: %s", i, e)
}
}
}

0 comments on commit e532e14

Please sign in to comment.