diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-15 14:17:21 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-15 16:34:33 +0300 |
commit | 0abd8d93ab7f70f444cd10a65ad9d3f616adaf57 (patch) | |
tree | 2741c0ad123998f503cb8195ea98e77075059690 | |
parent | 1e54a6d7d7e64f60963040468408f418ab66d6a7 (diff) |
gittest: Add commit option to write objects into alternate ODB
We have several tests which write commit objects into the alternate
object database. While some hand-code this, most use the helper function
`CreateCommitInAlternateObjectDirectory()`. This helper requires manual
assemlby of the command and is thus unnecessarily verbose to be used.
Add a new option to the `WriteCommit()` helper which allows callers to
specify an alternate ODB such that it can be used instead of the old
helper.
-rw-r--r-- | internal/git/gittest/commit.go | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/internal/git/gittest/commit.go b/internal/git/gittest/commit.go index 1e49149d6..b7e83ee8e 100644 --- a/internal/git/gittest/commit.go +++ b/internal/git/gittest/commit.go @@ -21,11 +21,12 @@ const ( ) type writeCommitConfig struct { - branch string - parents []git.ObjectID - committerName string - message string - treeEntries []TreeEntry + branch string + parents []git.ObjectID + committerName string + message string + treeEntries []TreeEntry + alternateObjectDir string } // WriteCommitOption is an option which can be passed to WriteCommit. @@ -74,6 +75,15 @@ func WithCommitterName(name string) WriteCommitOption { } } +// WithAlternateObjectDirectory will cause the commit to be written into the given alternate object +// directory. This can either be an absolute path or a relative path. In the latter case the path +// is considered to be relative to the repository path. +func WithAlternateObjectDirectory(alternateObjectDir string) WriteCommitOption { + return func(cfg *writeCommitConfig) { + cfg.alternateObjectDir = alternateObjectDir + } +} + // WriteCommit writes a new commit into the target repository. func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCommitOption) git.ObjectID { t.Helper() @@ -119,16 +129,33 @@ func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCom "commit-tree", "-F", "-", tree, } + var env []string + if writeCommitConfig.alternateObjectDir != "" { + require.True(t, filepath.IsAbs(writeCommitConfig.alternateObjectDir), + "alternate object directory must be an absolute path") + require.NoError(t, os.MkdirAll(writeCommitConfig.alternateObjectDir, 0o755)) + + env = append(env, + fmt.Sprintf("GIT_OBJECT_DIRECTORY=%s", writeCommitConfig.alternateObjectDir), + fmt.Sprintf("GIT_ALTERNATE_OBJECT_DIRECTORIES=%s", filepath.Join(repoPath, "objects")), + ) + } + for _, parent := range parents { commitArgs = append(commitArgs, "-p", parent.String()) } - stdout := ExecOpts(t, cfg, ExecConfig{Stdin: stdin}, commitArgs...) + stdout := ExecOpts(t, cfg, ExecConfig{ + Stdin: stdin, + Env: env, + }, commitArgs...) oid, err := git.NewObjectIDFromHex(text.ChompBytes(stdout)) require.NoError(t, err) if writeCommitConfig.branch != "" { - Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/"+writeCommitConfig.branch, oid.String()) + ExecOpts(t, cfg, ExecConfig{ + Env: env, + }, "-C", repoPath, "update-ref", "refs/heads/"+writeCommitConfig.branch, oid.String()) } return oid |