use branch revision during worktree add (v4)

This is a port of the v3 commit 2c3bb035f6
This commit is contained in:
Tim Hockin 2021-10-04 16:28:27 -07:00
parent 4d286fc44f
commit a91777288a
2 changed files with 42 additions and 6 deletions

View File

@ -896,11 +896,6 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
return nil
}
// GC clone
if _, err := git.run.Run(ctx, git.root, git.cmd, "gc", "--prune=all"); err != nil {
return err
}
// Make a worktree for this exact git hash.
worktreePath := filepath.Join(git.root, hash)
@ -912,12 +907,17 @@ func (git *repoSync) AddWorktreeAndSwap(ctx context.Context, hash string) error
return err
}
_, err := git.run.Run(ctx, git.root, git.cmd, "worktree", "add", worktreePath, "origin/"+git.branch, "--no-checkout")
_, err := git.run.Run(ctx, git.root, git.cmd, "worktree", "add", "--detach", worktreePath, hash, "--no-checkout")
git.log.V(0).Info("adding worktree", "path", worktreePath, "branch", fmt.Sprintf("origin/%s", git.branch))
if err != nil {
return err
}
// GC clone
if _, err := git.run.Run(ctx, git.root, git.cmd, "gc", "--prune=all"); err != nil {
return err
}
// The .git file in the 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

View File

@ -553,6 +553,42 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
# Wrap up
pass
##############################################
# Test switching branch after depth=1 checkout
##############################################
testcase "branch-switch"
# First sync
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=e2e-branch \
--depth=1 \
--root="$ROOT" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
BRANCH="${TESTCASE}2"
git -C "$REPO" checkout -q -b $BRANCH
echo "$TESTCASE 2" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE 2"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=$BRANCH \
--rev=HEAD \
--root="$ROOT" \
--link="link" \
>> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE 2"
# Wrap up
pass
##############################################
# Test tag syncing
##############################################