Skip to content

Fix non-master branches and tags #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func updateSymlink(gitRoot, link, newDir string) error {
func addWorktreeAndSwap(gitRoot, dest, branch, rev, hash string) error {
log.V(0).Infof("syncing to %s (%s)", rev, hash)

// Update from the remote.
if _, err := runCommand(gitRoot, "git", "fetch", "--tags", "origin", branch); err != nil {
return err
}

// Make a worktree for this exact git hash.
worktreePath := path.Join(gitRoot, "rev-"+hash)
_, err := runCommand(gitRoot, "git", "worktree", "add", worktreePath, "origin/"+branch)
Expand Down Expand Up @@ -343,7 +348,7 @@ func syncRepo(repo, branch, rev string, depth int, gitRoot, dest string) error {
case err != nil:
return fmt.Errorf("error checking if repo exists %q: %v", gitRepoPath, err)
default:
local, remote, err := getRevs(target, rev)
local, remote, err := getRevs(target, branch, rev)
if err != nil {
return err
}
Expand All @@ -362,28 +367,39 @@ func syncRepo(repo, branch, rev string, depth int, gitRoot, dest string) error {
}

// getRevs returns the local and upstream hashes for rev.
func getRevs(localDir, rev string) (string, string, error) {
func getRevs(localDir, branch, rev string) (string, string, error) {
// Ask git what the exact hash is for rev.
local, err := hashForRev(rev, localDir)
if err != nil {
return "", "", err
}

// Fetch rev from upstream.
_, err = runCommand(localDir, "git", "fetch", "--tags", "origin", rev)
if err != nil {
return "", "", err
// Build a ref string, depending on whether the user asked to track HEAD or a tag.
ref := ""
if rev == "HEAD" {
ref = "refs/heads/" + branch
} else {
ref = "refs/tags/" + rev + "^{}"
}

// Ask git what the exact hash is for upstream rev.
remote, err := hashForRev("FETCH_HEAD", localDir)
// Figure out what hash the remote resolves ref to.
remote, err := remoteHashForRef(ref, localDir)
if err != nil {
return "", "", err
}

return local, remote, nil
}

func remoteHashForRef(ref, gitRoot string) (string, error) {
output, err := runCommand(gitRoot, "git", "ls-remote", "-q", "origin", ref)
if err != nil {
return "", err
}
parts := strings.Split(string(output), "\t")
return parts[0], nil
}

func cmdForLog(command string, args ...string) string {
if strings.ContainsAny(command, " \t\n") {
command = fmt.Sprintf("%q", command)
Expand Down