diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 46331e6..eb148df 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -403,11 +403,22 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) error { } // addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree -func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev, hash string) error { +func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string, depth int, hash string) error { log.V(0).Infof("syncing to %s (%s)", rev, hash) + args := []string{"fetch", "--tags"} + if depth != 0 { + args = append(args, "--depth", strconv.Itoa(depth)) + } + args = append(args, "origin", branch) + // Update from the remote. - if _, err := runCommand(ctx, gitRoot, *flGitCmd, "fetch", "--tags", "origin", branch); err != nil { + if _, err := runCommand(ctx, gitRoot, *flGitCmd, args...); err != nil { + return err + } + + // GC clone + if _, err := runCommand(ctx, gitRoot, *flGitCmd, "gc", "--prune=all"); err != nil { return err } @@ -533,7 +544,7 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, } } - return true, addWorktreeAndSwap(ctx, gitRoot, dest, branch, rev, hash) + return true, addWorktreeAndSwap(ctx, gitRoot, dest, branch, rev, depth, hash) } // getRevs returns the local and upstream hashes for rev. diff --git a/test_e2e.sh b/test_e2e.sh index a6d744a..22b2807 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -536,6 +536,56 @@ assert_link_exists "$ROOT"/link assert_file_exists "$ROOT"/link/file assert_file_eq "$ROOT"/link/file "$TESTCASE 2" # Wrap up +remove_sync_container +wait +pass + +# Test depth syncing +testcase "depth" +# First sync +echo "$TESTCASE 1" > "$REPO"/file +expected_depth="1" +git -C "$REPO" commit -qam "$TESTCASE 1" +GIT_SYNC \ + --logtostderr \ + --v=5 \ + --wait=0.1 \ + --repo="$REPO" \ + --depth="$expected_depth" \ + --root="$ROOT" \ + --dest="link" > "$DIR"/log."$TESTCASE" 2>&1 & +sleep 3 +assert_link_exists "$ROOT"/link +assert_file_exists "$ROOT"/link/file +assert_file_eq "$ROOT"/link/file "$TESTCASE 1" +depth=$(GIT_DIR="$ROOT"/link/.git git log | grep commit | wc -l) +if [ $expected_depth != $depth ]; then + fail "initial depth mismatch expected=$expected_depth actual=$depth" +fi +# Move forward +echo "$TESTCASE 2" > "$REPO"/file +git -C "$REPO" commit -qam "$TESTCASE 2" +sleep 3 +assert_link_exists "$ROOT"/link +assert_file_exists "$ROOT"/link/file +assert_file_eq "$ROOT"/link/file "$TESTCASE 2" +depth=$(GIT_DIR="$ROOT"/link/.git git log | grep commit | wc -l) +if [ $expected_depth != $depth ]; then + fail "forward depth mismatch expected=$expected_depth actual=$depth" +fi +# Move backward +git -C "$REPO" reset -q --hard HEAD^ +sleep 3 +assert_link_exists "$ROOT"/link +assert_file_exists "$ROOT"/link/file +assert_file_eq "$ROOT"/link/file "$TESTCASE 1" +depth=$(GIT_DIR="$ROOT"/link/.git git log | grep commit | wc -l) +if [ $expected_depth != $depth ]; then + fail "backward depth mismatch expected=$expected_depth actual=$depth" +fi +# Wrap up +remove_sync_container +wait pass echo "cleaning up $DIR"