aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/testgit/object.go27
-rw-r--r--internal/testgit/repo.go53
2 files changed, 80 insertions, 0 deletions
diff --git a/internal/testgit/object.go b/internal/testgit/object.go
new file mode 100644
index 00000000..0ea4d4d8
--- /dev/null
+++ b/internal/testgit/object.go
@@ -0,0 +1,27 @@
+package testgit
+
+import (
+ "io"
+ "testing"
+
+ "codeberg.org/lindenii/furgit/object/id"
+ "codeberg.org/lindenii/furgit/object/typ"
+)
+
+// HashObject hashes and writes an object and returns its object ID.
+func (repo *Repo) HashObject(tb testing.TB, ty typ.Type, body io.Reader) id.ObjectID {
+ tb.Helper()
+ cmd := repo.Command(tb, "git", "hash-object", "-t", ty.Name(), "-w", "--stdin", "--literally")
+
+ hex, err := cmd.CombinedOutput()
+ if err != nil {
+ tb.Fatalf("hash-object: %v", hex)
+ }
+
+ id, err := id.FromHex(repo.algo, string(hex))
+ if err != nil {
+ tb.Fatalf("parse git hash-object output %q: %v", hex, err)
+ }
+
+ return id
+}
diff --git a/internal/testgit/repo.go b/internal/testgit/repo.go
new file mode 100644
index 00000000..f18f199c
--- /dev/null
+++ b/internal/testgit/repo.go
@@ -0,0 +1,53 @@
+package testgit
+
+import (
+ "os"
+ "os/exec"
+ "testing"
+
+ "codeberg.org/lindenii/furgit/object/id"
+)
+
+type Repo struct {
+ path string
+ algo id.Algorithm
+ env []string
+}
+
+func (repo *Repo) Command(
+ tb testing.TB,
+ command string,
+ args ...string,
+) *exec.Cmd {
+ //nolint:noctx
+ cmd := exec.Command(command, args...)
+ cmd.Dir = repo.path
+ cmd.Env = repo.env
+
+ return cmd
+}
+
+type RepoOptions struct {
+ ObjectFormat id.Algorithm
+}
+
+func NewRepo(tb testing.TB, opts RepoOptions) (*Repo, error) {
+ tb.Helper()
+
+ repo := &Repo{
+ path: tb.TempDir(),
+ algo: opts.ObjectFormat,
+ env: append(os.Environ(),
+ "GIT_CONFIG_GLOBAL=/dev/null",
+ "GIT_CONFIG_SYSTEM=/dev/null",
+ "GIT_AUTHOR_NAME=Test Author",
+ "GIT_AUTHOR_EMAIL=test@example.org",
+ "GIT_COMMITTER_NAME=Test Committer",
+ "GIT_COMMITTER_EMAIL=committer@example.org",
+ "GIT_AUTHOR_DATE=1234567890 +0000",
+ "GIT_COMMITTER_DATE=1234567890 +0000",
+ ),
+ }
+
+ return repo, repo.Command(tb, "git", "init", "--object-format="+repo.algo.String(), "--", repo.path).Run()
+}