Commit 731d8b32 authored by Edward Muller's avatar Edward Muller
Browse files

Checkout default git branch

When a git repository is in detached HEAD state use `git show remote
origin` to determine the default branch and check it out before
downloading updates.

This is necessary for `godep restore` to work since
https://github.com/golang/go/commit/42206598671a44111c8f726ad33dc7b265bdf669
parents 4d9a4c3d 196d0fae
#v66 (2016/06/10)
* Use `git remote show origin` to find the default branch when restoring a git based package repository that is in detached head state
#v65 (2016/06/09)
* Rewrite update so that it considers new transitive dependencies, both in the same repo and outside of it.
......
......@@ -146,6 +146,22 @@ func download(dep *Dependency) error {
if !dep.vcs.exists(dep.root, dep.Rev) {
debugln("Updating existing", dep.root)
if dep.vcs == vcsGit {
detached, err := gitDetached(dep.root)
if err != nil {
return err
}
if detached {
db, err := gitDefaultBranch(dep.root)
if err != nil {
return err
}
if err := gitCheckout(dep.root, db); err != nil {
return err
}
}
}
dep.vcs.vcs.Download(dep.root)
downloaded[rr.Repo] = true
}
......
......@@ -2,6 +2,7 @@ package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
......@@ -274,3 +275,33 @@ func hgLink(dir, remote, url string) error {
fmt.Fprintf(f, "[paths]\n%s = %s\n", remote, url)
return f.Close()
}
func gitDetached(r string) (bool, error) {
o, err := vcsGit.runOutput(r, "status")
if err != nil {
return false, errors.New("unable to determine git status " + err.Error())
}
return bytes.Contains(o, []byte("HEAD detached at")), nil
}
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())
}
s := bytes.Index(o, hb)
if s < 0 {
return "", errors.New(e + "git output missing '" + string(hb) + "'")
}
f := bytes.Fields(o[s:])
if len(f) < 3 {
return "", errors.New(e + "git output too short")
}
return string(f[2]), nil
}
func gitCheckout(r, b string) error {
return vcsGit.run(r, "checkout "+b)
}
......@@ -8,7 +8,7 @@ import (
"strings"
)
const version = 65
const version = 66
var cmdVersion = &Command{
Name: "version",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment