Skip to content

Commit

Permalink
git-sync: Fix .git path for worktrees
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Lyons committed Aug 11, 2016
1 parent caa9989 commit a107212
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions git-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -204,6 +205,17 @@ func addWorktreeAndSwap(dest, branch, rev string) error {

log.Printf("add worktree origin/%q: %v", branch, string(output))

// .git file in worktree directory holds a reference to /git/.git/worktrees/<worktree-dir-name>
// Replace it with a reference using relative paths, so that other containers can use a different volume mount name
worktreePathRelative, err := filepath.Rel(volMount, worktreePath)
if err != nil {
return err
}
gitDirRef := []byte(path.Join("gitdir: ../.git/worktrees", worktreePathRelative) + "\n")
if err = ioutil.WriteFile(path.Join(worktreePath, ".git"), gitDirRef, 0644); err != nil {
return err
}

// reset working copy
output, err = runCommand("git", worktreePath, []string{"reset", "--hard", rev})
if err != nil {
Expand Down

0 comments on commit a107212

Please sign in to comment.