diff options
author | John Cai <jcai@gitlab.com> | 2022-05-05 22:40:55 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-05-06 03:41:14 +0300 |
commit | 3e04a937bb112bb7b7e85667f6eed65b894a3dca (patch) | |
tree | a79715fa2f8709bdd43de52fe55c50e9391c3a1a | |
parent | 3f35f74cbd48f0a1557cd567087f685feede6672 (diff) |
localrepo: Allow Size to pass exclude flag
There are times when we want to exclude certain refs from being included
in the size calculation for a repository. Add the ability to do so.
Changelog: added
-rw-r--r-- | internal/git/localrepo/repo.go | 53 | ||||
-rw-r--r-- | internal/git/localrepo/repo_test.go | 36 |
2 files changed, 81 insertions, 8 deletions
diff --git a/internal/git/localrepo/repo.go b/internal/git/localrepo/repo.go index 7d7f3e409..97525a1fc 100644 --- a/internal/git/localrepo/repo.go +++ b/internal/git/localrepo/repo.go @@ -94,18 +94,55 @@ func errorWithStderr(err error, stderr []byte) error { return fmt.Errorf("%w, stderr: %q", err, stderr) } +// repoSizeConfig can be used to pass in different options to +// git rev-list in determining the size of a repository. +type repoSizeConfig struct { + // Excludes is a list of ref glob patterns to exclude from the size + // calculation. + Excludes []string +} + +// RepoSizeOption is an option which can be passed to Size +type RepoSizeOption func(*repoSizeConfig) + +// WithExcludes is an option for Size that excludes certain refs from the size +// calculation. The format must be a glob pattern. +// see https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt---excludeltglob-patterngt +func WithExcludes(excludes ...string) RepoSizeOption { + return func(cfg *repoSizeConfig) { + cfg.Excludes = excludes + } +} + // Size calculates the size of all reachable objects in bytes -func (repo *Repo) Size(ctx context.Context) (int64, error) { +func (repo *Repo) Size(ctx context.Context, opts ...RepoSizeOption) (int64, error) { var stdout bytes.Buffer + + var cfg repoSizeConfig + + for _, opt := range opts { + opt(&cfg) + } + + var options []git.Option + for _, exclude := range cfg.Excludes { + options = append( + options, + git.Flag{Name: fmt.Sprintf("--exclude=%s", exclude)}, + ) + } + + options = append(options, + git.Flag{Name: "--all"}, + git.Flag{Name: "--objects"}, + git.Flag{Name: "--use-bitmap-index"}, + git.Flag{Name: "--disk-usage"}, + ) + if err := repo.ExecAndWait(ctx, git.SubCmd{ - Name: "rev-list", - Flags: []git.Option{ - git.Flag{Name: "--all"}, - git.Flag{Name: "--objects"}, - git.Flag{Name: "--use-bitmap-index"}, - git.Flag{Name: "--disk-usage"}, - }, + Name: "rev-list", + Flags: options, }, git.WithStdout(&stdout), ); err != nil { diff --git a/internal/git/localrepo/repo_test.go b/internal/git/localrepo/repo_test.go index c305519c8..c9f620731 100644 --- a/internal/git/localrepo/repo_test.go +++ b/internal/git/localrepo/repo_test.go @@ -171,3 +171,39 @@ func TestSize(t *testing.T) { }) } } + +func TestSize_excludes(t *testing.T) { + cfg := testcfg.Build(t) + gitCmdFactory := gittest.NewCommandFactory(t, cfg) + catfileCache := catfile.NewCache(cfg) + t.Cleanup(catfileCache.Stop) + + pbRepo, repoPath := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + blob := bytes.Repeat([]byte("a"), 1000) + blobOID := gittest.WriteBlob(t, cfg, repoPath, blob) + treeOID := gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + { + OID: blobOID, + Mode: "100644", + Path: "1kbblob", + }, + }) + commitOID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTree(treeOID)) + + repo := New(config.NewLocator(cfg), gitCmdFactory, catfileCache, pbRepo) + + ctx := testhelper.Context(t) + sizeBeforeKeepAround, err := repo.Size(ctx) + require.NoError(t, err) + + gittest.WriteRef(t, cfg, repoPath, git.ReferenceName("refs/keep-around/keep1"), commitOID) + + sizeWithKeepAround, err := repo.Size(ctx) + require.NoError(t, err) + assert.Less(t, sizeBeforeKeepAround, sizeWithKeepAround) + + sizeWithoutKeepAround, err := repo.Size(ctx, WithExcludes("refs/keep-around/*")) + require.NoError(t, err) + + assert.Equal(t, sizeBeforeKeepAround, sizeWithoutKeepAround) +} |