diff options
author | John Cai <jcai@gitlab.com> | 2022-05-13 17:02:14 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-05-16 15:32:46 +0300 |
commit | c4d7176839a7cc46c3631b59cf53052ebf987367 (patch) | |
tree | 705da15b994381cd0be381e6bced3d0d061dd338 | |
parent | 42fab8fc526215f9426bc9f459f9e6da0951c574 (diff) |
localrepo: Add WithoutAlternates option to repository size
There are times when we need to exclude the alternates directory from
the repository size calculation. Provide an option to do so.
-rw-r--r-- | internal/git/localrepo/repo.go | 18 | ||||
-rw-r--r-- | internal/git/localrepo/repo_test.go | 31 |
2 files changed, 49 insertions, 0 deletions
diff --git a/internal/git/localrepo/repo.go b/internal/git/localrepo/repo.go index 97525a1fc..3fef482e8 100644 --- a/internal/git/localrepo/repo.go +++ b/internal/git/localrepo/repo.go @@ -100,6 +100,9 @@ type repoSizeConfig struct { // Excludes is a list of ref glob patterns to exclude from the size // calculation. Excludes []string + // ExcludeAlternates will exclude objects in the alternates directory + // from being counted towards the total size of the repository. + ExcludeAlternates bool } // RepoSizeOption is an option which can be passed to Size @@ -114,6 +117,13 @@ func WithExcludes(excludes ...string) RepoSizeOption { } } +// WithoutAlternates will exclude any objects in the alternate objects directory +func WithoutAlternates() RepoSizeOption { + return func(cfg *repoSizeConfig) { + cfg.ExcludeAlternates = true + } +} + // Size calculates the size of all reachable objects in bytes func (repo *Repo) Size(ctx context.Context, opts ...RepoSizeOption) (int64, error) { var stdout bytes.Buffer @@ -132,6 +142,14 @@ func (repo *Repo) Size(ctx context.Context, opts ...RepoSizeOption) (int64, erro ) } + if cfg.ExcludeAlternates { + options = append(options, + git.Flag{Name: "--not"}, + git.Flag{Name: "--alternate-refs"}, + git.Flag{Name: "--not"}, + ) + } + options = append(options, git.Flag{Name: "--all"}, git.Flag{Name: "--objects"}, diff --git a/internal/git/localrepo/repo_test.go b/internal/git/localrepo/repo_test.go index c9f620731..10f87514c 100644 --- a/internal/git/localrepo/repo_test.go +++ b/internal/git/localrepo/repo_test.go @@ -207,3 +207,34 @@ func TestSize_excludes(t *testing.T) { assert.Equal(t, sizeBeforeKeepAround, sizeWithoutKeepAround) } + +func TestSize_excludeAlternates(t *testing.T) { + cfg := testcfg.Build(t) + gitCmdFactory := gittest.NewCommandFactory(t, cfg) + catfileCache := catfile.NewCache(cfg) + t.Cleanup(catfileCache.Stop) + locator := config.NewLocator(cfg) + + pbRepo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + _, altRepoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + + require.NoError(t, os.WriteFile( + filepath.Join(repoPath, "objects", "info", "alternates"), + []byte(filepath.Join(altRepoPath, "objects")), + os.ModePerm, + )) + + repo := New(locator, gitCmdFactory, catfileCache, pbRepo) + + ctx := testhelper.Context(t) + + gittest.Exec(t, cfg, "-C", repoPath, "gc") + + sizeIncludingAlternates, err := repo.Size(ctx) + require.NoError(t, err) + assert.Greater(t, sizeIncludingAlternates, int64(0)) + + sizeExcludingAlternates, err := repo.Size(ctx, WithoutAlternates()) + require.NoError(t, err) + assert.Equal(t, int64(0), sizeExcludingAlternates) +} |