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

Commit

Permalink
handle detached git on restore
Browse files Browse the repository at this point in the history
If the git repo is detached, we need to determine the default remote
branch before attempting to donwload (can't pull into a repo in detached
state).

This attempts to use "git remote show origin" to have the origin tell us
what the default branch is.

PS: this may not work with old (~3 year old git servers).
  • Loading branch information
Edward Muller committed May 10, 2016
1 parent 4d9a4c3 commit 0e7242e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.vcs.Name == "Git" {
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
}
Expand Down
31 changes: 31 additions & 0 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -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)
}

0 comments on commit 0e7242e

Please sign in to comment.