From cbedbc0ca5d27296b93b89ba2dad3ab16bd50f46 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sat, 18 Feb 2023 16:26:18 -0800 Subject: [PATCH] Add test for repo size and change default GC --git-gc=always seems to be the right tradeoff. --- cmd/git-sync/main.go | 15 +++++--- test_e2e.sh | 87 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 0982aa9..75d8aa9 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -136,7 +136,7 @@ var flGitCmd = pflag.String("git", envString("GIT_SYNC_GIT", "git"), "the git command to run (subject to PATH search, mostly for testing)") var flGitConfig = pflag.String("git-config", envString("GIT_SYNC_GIT_CONFIG", ""), "additional git config options in 'section.var1:val1,\"section.sub.var2\":\"val2\"' format") -var flGitGC = pflag.String("git-gc", envString("GIT_SYNC_GIT_GC", "auto"), +var flGitGC = pflag.String("git-gc", envString("GIT_SYNC_GIT_GC", "always"), "git garbage collection behavior: one of 'auto', 'always', 'aggressive', or 'off'") var flHTTPBind = pflag.String("http-bind", envString("GIT_SYNC_HTTP_BIND", ""), @@ -1371,6 +1371,11 @@ func (git *repoSync) cleanup(ctx context.Context, oldHash string) error { } } + // Expire old refs. + if _, err := git.run.Run(ctx, git.root, nil, git.cmd, "reflog", "expire", "--expire-unreachable=all", "--all"); err != nil { + cleanupErrs = append(cleanupErrs, err) + } + // Run GC if needed. if git.gc != gcOff { args := []string{"gc"} @@ -1547,11 +1552,9 @@ func (git *repoSync) SyncRepo(ctx context.Context, refreshCreds func(context.Con // Mark ourselves as "ready". setRepoReady() - if oldHash != "" { - // Clean up the old worktree(s). - if err := git.cleanup(ctx, oldHash); err != nil { - git.log.Error(err, "git cleanup failed", "oldHash", oldHash) - } + // Clean up the old worktree(s) and run GC. + if err := git.cleanup(ctx, oldHash); err != nil { + git.log.Error(err, "git cleanup failed", "oldHash", oldHash) } return changed, remote, nil diff --git a/test_e2e.sh b/test_e2e.sh index 6fc25a8..095bfc8 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -2571,6 +2571,93 @@ function e2e::github_https() { assert_file_exists "$ROOT"/link/LICENSE } +############################################## +# Test git-gc default +############################################## +function e2e::gc_default() { + SHA1=$(git -C "$REPO" rev-parse HEAD) + dd if=/dev/urandom of="$REPO"/big1 bs=1024 count=4096 >/dev/null + git -C "$REPO" add . + git -C "$REPO" commit -qam "$FUNCNAME 1" + SHA2=$(git -C "$REPO" rev-parse HEAD) + dd if=/dev/urandom of="$REPO"/big2 bs=1024 count=4096 >/dev/null + git -C "$REPO" add . + git -C "$REPO" commit -qam "$FUNCNAME 2" + SHA3=$(git -C "$REPO" rev-parse HEAD) + + GIT_SYNC \ + --one-time \ + --repo="file://$REPO" \ + --root="$ROOT" \ + --link="link" \ + --ref="$SHA3" \ + --depth=0 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/big1 + assert_file_exists "$ROOT"/link/big2 + SIZE=$(du -s "$ROOT" | cut -f1) + if [ "$SIZE" -lt 14000 ]; then + fail "repo is impossibly small: $SIZE" + fi + if [ "$SIZE" -gt 18000 ]; then + fail "repo is too big: $SIZE" + fi + + GIT_SYNC \ + --one-time \ + --repo="file://$REPO" \ + --root="$ROOT" \ + --link="link" \ + --ref="$SHA3" \ + --depth=1 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/big1 + assert_file_exists "$ROOT"/link/big2 + SIZE=$(du -s "$ROOT" | cut -f1) + if [ "$SIZE" -lt 14000 ]; then + fail "repo is impossibly small: $SIZE" + fi + if [ "$SIZE" -gt 18000 ]; then + fail "repo is too big: $SIZE" + fi + + GIT_SYNC \ + --one-time \ + --repo="file://$REPO" \ + --root="$ROOT" \ + --link="link" \ + --ref="$SHA2" \ + --depth=1 + assert_link_exists "$ROOT"/link + assert_file_exists "$ROOT"/link/big1 + assert_file_absent "$ROOT"/link/big2 + SIZE=$(du -s "$ROOT" | cut -f1) + if [ "$SIZE" -lt 7000 ]; then + fail "repo is impossibly small: $SIZE" + fi + if [ "$SIZE" -gt 9000 ]; then + fail "repo is too big: $SIZE" + fi + + GIT_SYNC \ + --one-time \ + --repo="file://$REPO" \ + --root="$ROOT" \ + --link="link" \ + --ref="$SHA1" \ + --depth=1 + assert_link_exists "$ROOT"/link + assert_file_absent "$ROOT"/link/big1 + assert_file_absent "$ROOT"/link/big2 + SIZE=$(du -s "$ROOT" | cut -f1) + if [ "$SIZE" -lt 100 ]; then + fail "repo is impossibly small: $SIZE" + fi + if [ "$SIZE" -gt 1000 ]; then + fail "repo is too big: $SIZE" + fi +} + ############################################## # Test git-gc=auto ##############################################