From 5893630f4aecf5365f953a161d5133f128374aff Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 7 Jun 2026 07:05:48 +0000 Subject: internal/testgit: Add helpers to prepare for commit testing --- internal/testgit/commit.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 internal/testgit/commit.go (limited to 'internal/testgit/commit.go') 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 +} -- cgit v1.3.1-10-gc9f91