From f1a1a1b61eb0d2739698f23a7e2112bb99980821 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Tue, 22 Jan 2019 16:22:54 -0800 Subject: [PATCH 1/3] Use depth option when doing fetches Previously the `depth` flag was only used for the initial clone-- so although you might start with a depth=10 as more commits show up you are always >10. With this diff we enforce that depth on each fetch, this way old-commits can get GCd off to reduce the size of the local checkout required. --- cmd/git-sync/main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index d83fcec..71bc0fa 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -339,11 +339,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 } @@ -469,7 +480,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. From b0c123478779cdb1be6ff51c21d38ace39d1d3a9 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Tue, 22 Jan 2019 16:59:53 -0800 Subject: [PATCH 2/3] Add e2e tests for depth flag --- test_e2e.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test_e2e.sh b/test_e2e.sh index a6d744a..82ec371 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -538,5 +538,53 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE 2" # Wrap up 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" rm -rf "$DIR" From 07f0e6dde2486b0b063f13f70631a6c8e040c4d8 Mon Sep 17 00:00:00 2001 From: Thomas Jackson Date: Tue, 22 Jan 2019 17:15:03 -0800 Subject: [PATCH 3/3] Cleanup container from sync-loop-timeout Otherwise any test running afterwards fails because it left the container running. --- test_e2e.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_e2e.sh b/test_e2e.sh index 82ec371..22b2807 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -536,6 +536,8 @@ 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