diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-08 11:59:09 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-15 16:15:09 +0300 |
commit | 925e65cdc845c78ed4467d83fb905b5be8ef0825 (patch) | |
tree | 6da9a6418f21fa8e7e89fe4efc1401bb44a3c352 | |
parent | f23495c24f2d10642f5f0ca50e7d34faf277b5d0 (diff) |
housekeeping: Extract function to count references
We're about to add a second callsite that requires us to count loose and
packed references in a repository as `packRefsIfNeeded()` already knows
to do. Extract the logic into its own function to make it reusable.
-rw-r--r-- | internal/git/housekeeping/optimize_repository.go | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/internal/git/housekeeping/optimize_repository.go b/internal/git/housekeeping/optimize_repository.go index ef74f8f0d..2837cd065 100644 --- a/internal/git/housekeeping/optimize_repository.go +++ b/internal/git/housekeeping/optimize_repository.go @@ -412,10 +412,12 @@ func pruneIfNeeded(ctx context.Context, repo *localrepo.Repo) (bool, error) { return true, nil } -func packRefsIfNeeded(ctx context.Context, repo *localrepo.Repo) (bool, error) { +// countLooseAndPackedRefs counts the number of loose references that exist in the repository and +// returns the size of the packed-refs file. +func countLooseAndPackedRefs(ctx context.Context, repo *localrepo.Repo) (int64, int64, error) { repoPath, err := repo.Path() if err != nil { - return false, fmt.Errorf("getting repository path: %w", err) + return 0, 0, fmt.Errorf("getting repository path: %w", err) } refsPath := filepath.Join(repoPath, "refs") @@ -431,23 +433,32 @@ func packRefsIfNeeded(ctx context.Context, repo *localrepo.Repo) (bool, error) { return nil }); err != nil { - return false, fmt.Errorf("counting loose refs: %w", err) - } - - // If there aren't any loose refs then there is nothing we need to do. - if looseRefs == 0 { - return false, nil + return 0, 0, fmt.Errorf("counting loose refs: %w", err) } packedRefsSize := int64(0) if stat, err := os.Stat(filepath.Join(repoPath, "packed-refs")); err != nil { if !errors.Is(err, os.ErrNotExist) { - return false, fmt.Errorf("getting packed-refs size: %w", err) + return 0, 0, fmt.Errorf("getting packed-refs size: %w", err) } } else { packedRefsSize = stat.Size() } + return looseRefs, packedRefsSize, nil +} + +func packRefsIfNeeded(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) + } + + // If there aren't any loose refs then there is nothing we need to do. + if looseRefs == 0 { + return false, nil + } + // Packing loose references into the packed-refs file scales with the number of references // we're about to write. We thus decide whether we repack refs by weighing the current size // of the packed-refs file against the number of loose references. This is done such that we |