diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index cf220ab..1777ce3 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -684,11 +684,6 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string, return nil } - // GC clone - if _, err := cmdRunner.Run(ctx, gitRoot, *flGitCmd, "gc", "--prune=all"); err != nil { - return err - } - // Make a worktree for this exact git hash. worktreePath := filepath.Join(gitRoot, hash) @@ -702,12 +697,17 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string, return err } - _, err := cmdRunner.Run(ctx, gitRoot, *flGitCmd, "worktree", "add", worktreePath, "origin/"+branch, "--no-checkout") + _, err := cmdRunner.Run(ctx, gitRoot, *flGitCmd, "worktree", "add", "--detach", worktreePath, hash, "--no-checkout") log.V(0).Info("adding worktree", "path", worktreePath, "branch", fmt.Sprintf("origin/%s", branch)) if err != nil { return err } + // GC clone + if _, err := cmdRunner.Run(ctx, gitRoot, *flGitCmd, "gc", "--prune=all"); err != nil { + return err + } + // The .git file in the worktree directory holds a reference to // /git/.git/worktrees/. Replace it with a reference // using relative paths, so that other containers can use a different volume diff --git a/test_e2e.sh b/test_e2e.sh index fb0f1cd..e49c962 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -426,6 +426,43 @@ 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 \ + --rev=HEAD \ + --depth=1 \ + --root="$ROOT" \ + --dest="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" \ + --dest="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 ##############################################