aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/commit.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-07 07:05:48 +0000
committerGravatar Runxi Yu2026-06-07 07:05:48 +0000
commit5893630f4aecf5365f953a161d5133f128374aff (patch)
treeb26a52ad56406b8ef088cd24634f64ed599fea21 /internal/testgit/commit.go
parentUnify rules around errors.go or not (diff)
internal/testgit: Add helpers to prepare for commit testing
Diffstat (limited to 'internal/testgit/commit.go')
-rw-r--r--internal/testgit/commit.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/testgit/commit.go b/internal/testgit/commit.go
new file mode 100644
index 00000000..09a5438d
--- /dev/null
+++ b/internal/testgit/commit.go
@@ -0,0 +1,34 @@
+package testgit
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ "lindenii.org/go/furgit/object/id"
+)
+
+// 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) {
+ tb.Helper()
+
+ args := []string{"commit-tree", tree.String()}
+ for _, parent := range parents {
+ args = append(args, "-p", parent.String())
+ }
+
+ args = append(args, "-m", message)
+
+ stdout, err := repo.Run(tb, nil, "git", args...)
+ if err != nil {
+ return id.ObjectID{}, fmt.Errorf("commit-tree: %w", err)
+ }
+
+ commitID, err := repo.objectFormat.FromString(strings.TrimSuffix(string(stdout), "\n"))
+ if err != nil {
+ return id.ObjectID{}, fmt.Errorf("parse git commit-tree output %q: %w", string(stdout), err)
+ }
+
+ return commitID, nil
+}