aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit
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
parentUnify rules around errors.go or not (diff)
signatureNo signature
internal/testgit: Add helpers to prepare for commit testing
Diffstat (limited to 'internal/testgit')
-rw-r--r--internal/testgit/commit.go34
-rw-r--r--internal/testgit/object.go13
-rw-r--r--internal/testgit/tree.go41
3 files changed, 88 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
+}
diff --git a/internal/testgit/object.go b/internal/testgit/object.go
index a5f7163b..cbdfce76 100644
--- a/internal/testgit/object.go
+++ b/internal/testgit/object.go
@@ -27,3 +27,16 @@ func (repo *Repo) HashObject(tb testing.TB, ty typ.Type, body io.Reader) (id.Obj
return objectID, nil
}
+
+// CatFile returns the raw content of an object
+// (without the "type size\x00" header).
+func (repo *Repo) CatFile(tb testing.TB, ty typ.Type, oid id.ObjectID) ([]byte, error) {
+ tb.Helper()
+
+ stdout, err := repo.Run(tb, nil, "git", "cat-file", ty.Name(), oid.String())
+ if err != nil {
+ return nil, fmt.Errorf("cat-file: %w", err)
+ }
+
+ return stdout, nil
+}
diff --git a/internal/testgit/tree.go b/internal/testgit/tree.go
new file mode 100644
index 00000000..3f80d1c3
--- /dev/null
+++ b/internal/testgit/tree.go
@@ -0,0 +1,41 @@
+package testgit
+
+import (
+ "bytes"
+ "fmt"
+ "strings"
+ "testing"
+
+ "lindenii.org/go/furgit/object/id"
+ "lindenii.org/go/furgit/object/typ"
+)
+
+// MkTreeEntry is one entry of a tree built by [Repo.MkTree].
+type MkTreeEntry struct {
+ Mode string
+ Type typ.Type
+ OID id.ObjectID
+ Name string
+}
+
+// MkTree builds a tree object from entries and returns its object ID.
+func (repo *Repo) MkTree(tb testing.TB, entries []MkTreeEntry) (id.ObjectID, error) {
+ tb.Helper()
+
+ var stdin bytes.Buffer
+ for _, entry := range entries {
+ fmt.Fprintf(&stdin, "%s %s %s\t%s\n", entry.Mode, entry.Type.Name(), entry.OID.String(), entry.Name)
+ }
+
+ stdout, err := repo.Run(tb, &stdin, "git", "mktree")
+ if err != nil {
+ return id.ObjectID{}, fmt.Errorf("mktree: %w", err)
+ }
+
+ treeID, err := repo.objectFormat.FromString(strings.TrimSuffix(string(stdout), "\n"))
+ if err != nil {
+ return id.ObjectID{}, fmt.Errorf("parse git mktree output %q: %w", string(stdout), err)
+ }
+
+ return treeID, nil
+}