aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-07 08:29:52 +0000
committerGravatar Runxi Yu2026-06-07 08:31:39 +0000
commit2885c5b4d27d913c04fa680d46ce5d9febe5643d (patch)
treedd4fe91749e450bc46ed4ec23c727b6a0d4d91ba /internal/testgit
parentinternal/testgit: I don't want exhaustruct:ignore here actually (diff)
signatureNo signature
internal/testgit: CommitTree should accept identities
Diffstat (limited to 'internal/testgit')
-rw-r--r--internal/testgit/command.go16
-rw-r--r--internal/testgit/commit.go49
2 files changed, 61 insertions, 4 deletions
diff --git a/internal/testgit/command.go b/internal/testgit/command.go
index a7d8651a..dc960877 100644
--- a/internal/testgit/command.go
+++ b/internal/testgit/command.go
@@ -3,6 +3,8 @@ package testgit
import (
"io"
"os/exec"
+ "slices"
+ "strings"
"testing"
)
@@ -34,3 +36,17 @@ func (repo *Repo) Run(
return cmd.Output() //nolint:wrapcheck
}
+
+func setEnv(env []string, key string, value string) []string {
+ prefix := key + "="
+ i := slices.IndexFunc(env, func(entry string) bool {
+ return strings.HasPrefix(entry, prefix)
+ })
+ if i >= 0 {
+ env[i] = prefix + value
+
+ return env
+ }
+
+ return append(env, prefix+value)
+}
diff --git a/internal/testgit/commit.go b/internal/testgit/commit.go
index 09a5438d..b33b8904 100644
--- a/internal/testgit/commit.go
+++ b/internal/testgit/commit.go
@@ -8,19 +8,60 @@ import (
"lindenii.org/go/furgit/object/id"
)
+// CommitTreeIdentity configures an author or committer identity
+// for [Repo.CommitTree].
+type CommitTreeIdentity struct {
+ Name string
+ Email string
+}
+
+// CommitTreeOptions configures [Repo.CommitTree].
+type CommitTreeOptions struct {
+ Message string
+ Author CommitTreeIdentity
+ Committer CommitTreeIdentity
+ AuthorDate string
+ CommitterDate string
+}
+
// CommitTree creates a commit object from a tree and optional parents,
// and returns its object ID.
-func (repo *Repo) CommitTree(tb testing.TB, tree id.ObjectID, message string, parents ...id.ObjectID) (id.ObjectID, error) {
+func (repo *Repo) CommitTree(
+ tb testing.TB,
+ tree id.ObjectID,
+ opts CommitTreeOptions,
+ parents ...id.ObjectID,
+) (id.ObjectID, error) {
tb.Helper()
- args := []string{"commit-tree", tree.String()}
+ args := []string{"commit-tree"}
for _, parent := range parents {
args = append(args, "-p", parent.String())
}
- args = append(args, "-m", message)
+ args = append(args, "-m", opts.Message, "--end-of-options", tree.String())
+
+ cmd := repo.Command(tb, "git", args...)
+ if opts.Author.Name != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_AUTHOR_NAME", opts.Author.Name)
+ }
+ if opts.Author.Email != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_AUTHOR_EMAIL", opts.Author.Email)
+ }
+ if opts.AuthorDate != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_AUTHOR_DATE", opts.AuthorDate)
+ }
+ if opts.Committer.Name != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_NAME", opts.Committer.Name)
+ }
+ if opts.Committer.Email != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_EMAIL", opts.Committer.Email)
+ }
+ if opts.CommitterDate != "" {
+ cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_DATE", opts.CommitterDate)
+ }
- stdout, err := repo.Run(tb, nil, "git", args...)
+ stdout, err := cmd.Output()
if err != nil {
return id.ObjectID{}, fmt.Errorf("commit-tree: %w", err)
}