diff options
author | Toon Claes <toon@gitlab.com> | 2022-06-03 17:41:50 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2022-07-08 11:24:17 +0300 |
commit | 228c7f63daf73362e2ec8daa18026d63b8161324 (patch) | |
tree | 1a67636cc745febd8bde6dd36a17ee6ca2616aaf | |
parent | 5da3e9a2322ba44be76aaba8d3eae06747620dcd (diff) |
localrepo: Add method to Repo to get temp path
In some cases you might only have access to a localrepo.Repo instance,
and directly to the locator. To make it more convenient to find the
TempDir for the storage where the repo is on, add a helper method to
Repo that will use it's storage.Locator to determine the temporary dir
for the storage.
It also ensures this temp dir exists.
-rw-r--r-- | internal/git/localrepo/repo.go | 15 | ||||
-rw-r--r-- | internal/git/localrepo/repo_test.go | 20 |
2 files changed, 35 insertions, 0 deletions
diff --git a/internal/git/localrepo/repo.go b/internal/git/localrepo/repo.go index 6d6ac6da3..730176543 100644 --- a/internal/git/localrepo/repo.go +++ b/internal/git/localrepo/repo.go @@ -205,3 +205,18 @@ func (repo *Repo) Size(ctx context.Context, opts ...RepoSizeOption) (int64, erro return size, nil } + +// StorageTempDir returns the temporary dir for the storage where the repo is on. +// When this directory does not exist yet, it's being created. +func (repo *Repo) StorageTempDir() (string, error) { + tempPath, err := repo.locator.TempDir(repo.GetStorageName()) + if err != nil { + return "", err + } + + if err := os.MkdirAll(tempPath, 0o755); err != nil { + return "", err + } + + return tempPath, nil +} diff --git a/internal/git/localrepo/repo_test.go b/internal/git/localrepo/repo_test.go index bc986eddf..f90e5377e 100644 --- a/internal/git/localrepo/repo_test.go +++ b/internal/git/localrepo/repo_test.go @@ -342,3 +342,23 @@ func TestSize(t *testing.T) { }) } } + +func TestRepo_StorageTempDir(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, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0]) + repo := New(locator, gitCmdFactory, catfileCache, pbRepo) + + expected, err := locator.TempDir(cfg.Storages[0].Name) + require.NoError(t, err) + require.NoDirExists(t, expected) + + tempPath, err := repo.StorageTempDir() + require.NoError(t, err) + require.DirExists(t, expected) + require.Equal(t, expected, tempPath) +} |