diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-12-01 16:24:02 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-12-01 17:23:46 +0300 |
commit | aad93be2bdf83fda999e1519e485d9b1860d454c (patch) | |
tree | 5e38b8beb0231ad2cd58b6fb4e64b275bd96c434 | |
parent | f84f57d37117c3027431452251bd07785842bd45 (diff) |
objectpool: Move logic to create pools into a single place
The logic to create object pools is split up between "pool.go" and
"clone.go". Merge it into a single "create.go" to make it easier to
find.
-rw-r--r-- | internal/git/objectpool/create.go (renamed from internal/git/objectpool/clone.go) | 14 | ||||
-rw-r--r-- | internal/git/objectpool/create_test.go (renamed from internal/git/objectpool/clone_test.go) | 46 | ||||
-rw-r--r-- | internal/git/objectpool/pool.go | 14 | ||||
-rw-r--r-- | internal/git/objectpool/pool_test.go | 42 |
4 files changed, 60 insertions, 56 deletions
diff --git a/internal/git/objectpool/clone.go b/internal/git/objectpool/create.go index 45a48f864..41ede286b 100644 --- a/internal/git/objectpool/clone.go +++ b/internal/git/objectpool/create.go @@ -11,6 +11,20 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo" ) +// Create will create a pool for a repository and pull the required data to this +// pool. `repo` that is passed also joins the repository. +func (o *ObjectPool) Create(ctx context.Context, repo *localrepo.Repo) (err error) { + if err := o.clone(ctx, repo); err != nil { + return fmt.Errorf("clone: %v", err) + } + + if err := o.removeHooksDir(); err != nil { + return fmt.Errorf("remove hooks: %v", err) + } + + return nil +} + // clone a repository to a pool, without setting the alternates, is not the // responsibility of this function. func (o *ObjectPool) clone(ctx context.Context, repo *localrepo.Repo) error { diff --git a/internal/git/objectpool/clone_test.go b/internal/git/objectpool/create_test.go index c2eed627c..01cba8efb 100644 --- a/internal/git/objectpool/clone_test.go +++ b/internal/git/objectpool/create_test.go @@ -10,9 +10,55 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" + "gitlab.com/gitlab-org/gitaly/v15/internal/git/localrepo" + "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" ) +func TestCreate(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + + cfg, pool, repoProto := setupObjectPool(t, ctx) + + repo := localrepo.NewTestRepo(t, cfg, repoProto) + repoPath, err := repo.Path() + require.NoError(t, err) + + commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master")) + + require.NoError(t, pool.Create(ctx, repo)) + require.True(t, pool.IsValid()) + + // There should not be a "hooks" directory in the pool. + require.NoDirExists(t, filepath.Join(pool.FullPath(), "hooks")) + // The "origin" remote of the pool points to the pool member. + require.Equal(t, repoPath, text.ChompBytes(gittest.Exec(t, cfg, "-C", pool.FullPath(), "remote", "get-url", "origin"))) + // The "master" branch points to the same commit as in the pool member. + require.Equal(t, commitID, gittest.ResolveRevision(t, cfg, pool.FullPath(), "refs/heads/master")) + // Objects exist in the pool repository. + gittest.RequireObjectExists(t, cfg, pool.FullPath(), commitID) +} + +func TestCreate_subdirsExist(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + + cfg, pool, repoProto := setupObjectPool(t, ctx) + repo := localrepo.NewTestRepo(t, cfg, repoProto) + + err := pool.Create(ctx, repo) + require.NoError(t, err) + + require.NoError(t, pool.Remove(ctx)) + + // Recreate pool so the subdirs exist already + err = pool.Create(ctx, repo) + require.NoError(t, err) +} + func TestClone_successful(t *testing.T) { t.Parallel() diff --git a/internal/git/objectpool/pool.go b/internal/git/objectpool/pool.go index fb140416a..76ad62276 100644 --- a/internal/git/objectpool/pool.go +++ b/internal/git/objectpool/pool.go @@ -106,20 +106,6 @@ func (o *ObjectPool) IsValid() bool { return storage.IsGitDirectory(o.FullPath()) } -// Create will create a pool for a repository and pull the required data to this -// pool. `repo` that is passed also joins the repository. -func (o *ObjectPool) Create(ctx context.Context, repo *localrepo.Repo) (err error) { - if err := o.clone(ctx, repo); err != nil { - return fmt.Errorf("clone: %v", err) - } - - if err := o.removeHooksDir(); err != nil { - return fmt.Errorf("remove hooks: %v", err) - } - - return nil -} - // Remove will remove the pool, and all its contents without preparing and/or // updating the repositories depending on this object pool // Subdirectories will remain to exist, and will never be cleaned up, even when diff --git a/internal/git/objectpool/pool_test.go b/internal/git/objectpool/pool_test.go index 92f39ddda..e6053b946 100644 --- a/internal/git/objectpool/pool_test.go +++ b/internal/git/objectpool/pool_test.go @@ -11,7 +11,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v15/internal/helper" - "gitlab.com/gitlab-org/gitaly/v15/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" ) @@ -104,47 +103,6 @@ func TestFromRepo_failures(t *testing.T) { } } -func TestCreate(t *testing.T) { - t.Parallel() - - ctx := testhelper.Context(t) - - cfg, pool, repo := setupObjectPool(t, ctx) - repoPath, err := repo.Path() - require.NoError(t, err) - - commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master")) - - require.NoError(t, pool.Create(ctx, repo)) - require.True(t, pool.IsValid()) - - // There should not be a "hooks" directory in the pool. - require.NoDirExists(t, filepath.Join(pool.FullPath(), "hooks")) - // The "origin" remote of the pool points to the pool member. - require.Equal(t, repoPath, text.ChompBytes(gittest.Exec(t, cfg, "-C", pool.FullPath(), "remote", "get-url", "origin"))) - // The "master" branch points to the same commit as in the pool member. - require.Equal(t, commitID, gittest.ResolveRevision(t, cfg, pool.FullPath(), "refs/heads/master")) - // Objects exist in the pool repository. - gittest.RequireObjectExists(t, cfg, pool.FullPath(), commitID) -} - -func TestCreate_subdirsExist(t *testing.T) { - t.Parallel() - - ctx := testhelper.Context(t) - - _, pool, repo := setupObjectPool(t, ctx) - - err := pool.Create(ctx, repo) - require.NoError(t, err) - - require.NoError(t, pool.Remove(ctx)) - - // Recreate pool so the subdirs exist already - err = pool.Create(ctx, repo) - require.NoError(t, err) -} - func TestRemove(t *testing.T) { t.Parallel() |