diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-25 13:38:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-27 16:14:05 +0300 |
commit | cd3a53b3013603be14c09a6e257b9eed3f8a377b (patch) | |
tree | bd89fe5a69a2511c8faf04e6ce0aee05ba2e9b4a | |
parent | f6114dab48951ac98469da02769c04a9c9125f89 (diff) |
gittest: Allow writing arbitrary references in WriteCommit
The WriteCommit helper function has an option to write the newly written
commit to a specific branch. It's not possible though to write to an
arbitrary reference that is not a branch.
Add another option `WithReference()` that allows callers to chose custom
reference prefixes so that they can write non-branch-references.
-rw-r--r-- | internal/git/gittest/commit.go | 20 | ||||
-rw-r--r-- | internal/git/gittest/commit_test.go | 14 |
2 files changed, 27 insertions, 7 deletions
diff --git a/internal/git/gittest/commit.go b/internal/git/gittest/commit.go index 526e73545..3ffc1035a 100644 --- a/internal/git/gittest/commit.go +++ b/internal/git/gittest/commit.go @@ -32,7 +32,7 @@ var ( ) type writeCommitConfig struct { - branch string + reference string parents []git.ObjectID authorDate time.Time authorName string @@ -47,14 +47,20 @@ type writeCommitConfig struct { // WriteCommitOption is an option which can be passed to WriteCommit. type WriteCommitOption func(*writeCommitConfig) -// WithBranch is an option for WriteCommit which will cause it to update the update the given branch -// name to the new commit. -func WithBranch(branch string) WriteCommitOption { +// WithReference is an option for WriteCommit which will cause it to update the given reference to +// point to the new commit. This function requires the fully-qualified reference name. +func WithReference(reference string) WriteCommitOption { return func(cfg *writeCommitConfig) { - cfg.branch = branch + cfg.reference = reference } } +// WithBranch is an option for WriteCommit which will cause it to update the given branch name to +// the new commit. +func WithBranch(branch string) WriteCommitOption { + return WithReference("refs/heads/" + branch) +} + // WithMessage is an option for WriteCommit which will set the commit message. func WithMessage(message string) WriteCommitOption { return func(cfg *writeCommitConfig) { @@ -218,10 +224,10 @@ func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCom oid, err := DefaultObjectHash.FromHex(text.ChompBytes(stdout)) require.NoError(t, err) - if writeCommitConfig.branch != "" { + if writeCommitConfig.reference != "" { ExecOpts(t, cfg, ExecConfig{ Env: env, - }, "-C", repoPath, "update-ref", "refs/heads/"+writeCommitConfig.branch, oid.String()) + }, "-C", repoPath, "update-ref", writeCommitConfig.reference, oid.String()) } return oid diff --git a/internal/git/gittest/commit_test.go b/internal/git/gittest/commit_test.go index d1f279554..b4fa49a17 100644 --- a/internal/git/gittest/commit_test.go +++ b/internal/git/gittest/commit_test.go @@ -125,6 +125,20 @@ func TestWriteCommit(t *testing.T) { expectedRevUpdate: "refs/heads/foo", }, { + desc: "with reference", + opts: []WriteCommitOption{ + WithReference("refs/custom/namespace"), + }, + expectedCommit: strings.Join([]string{ + "tree " + DefaultObjectHash.EmptyTreeOID.String(), + "author " + DefaultCommitterSignature, + "committer " + DefaultCommitterSignature, + "", + "message", + }, "\n"), + expectedRevUpdate: "refs/custom/namespace", + }, + { desc: "with tree entry", opts: []WriteCommitOption{ WithTreeEntries(treeEntryA), |