diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-10-20 08:52:16 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-10-28 07:49:54 +0300 |
commit | 9a815299173d016a57228a4726a1e1a2f0f705aa (patch) | |
tree | 8c4e6453ac02ff2bb98eb76e84ae96276380b082 | |
parent | 672962358ad5b12ed698a0cab8da1c4be7bffd82 (diff) |
housekeeping: Split up policy and mechanism to pack references
The policy to determine when we need to pack references is tightly
coupled with the mechanism to run git-pack-refs(1). Split up these two
concerns into standalone functions so that we can easily move the
policy-part into the new optimization strategy infrastructure.
-rw-r--r-- | internal/git/housekeeping/optimize_repository.go | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/internal/git/housekeeping/optimize_repository.go b/internal/git/housekeeping/optimize_repository.go index 92df972a6..0bc6fbd7d 100644 --- a/internal/git/housekeeping/optimize_repository.go +++ b/internal/git/housekeeping/optimize_repository.go @@ -566,6 +566,29 @@ func countLooseAndPackedRefs(ctx context.Context, repo *localrepo.Repo) (int64, } func packRefsIfNeeded(ctx context.Context, repo *localrepo.Repo) (bool, error) { + shouldPackRefs, err := needsPackRefs(ctx, repo) + if err != nil { + return false, fmt.Errorf("determining whether repo needs to pack refs: %w", err) + } + + if !shouldPackRefs { + return false, nil + } + + var stderr bytes.Buffer + if err := repo.ExecAndWait(ctx, git.SubCmd{ + Name: "pack-refs", + Flags: []git.Option{ + git.Flag{Name: "--all"}, + }, + }, git.WithStderr(&stderr)); err != nil { + return false, fmt.Errorf("packing refs: %w, stderr: %q", err, stderr.String()) + } + + return true, nil +} + +func needsPackRefs(ctx context.Context, repo *localrepo.Repo) (bool, error) { looseRefs, packedRefsSize, err := countLooseAndPackedRefs(ctx, repo) if err != nil { return false, fmt.Errorf("counting refs: %w", err) @@ -601,15 +624,5 @@ func packRefsIfNeeded(ctx context.Context, repo *localrepo.Repo) (bool, error) { return false, nil } - var stderr bytes.Buffer - if err := repo.ExecAndWait(ctx, git.SubCmd{ - Name: "pack-refs", - Flags: []git.Option{ - git.Flag{Name: "--all"}, - }, - }, git.WithStderr(&stderr)); err != nil { - return false, fmt.Errorf("packing refs: %w, stderr: %q", err, stderr.String()) - } - return true, nil } |