aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit
diff options
context:
space:
mode:
Diffstat (limited to 'internal/testgit')
-rw-r--r--internal/testgit/algorithms.go1
-rw-r--r--internal/testgit/repo_cat_file.go1
-rw-r--r--internal/testgit/repo_commit_tree.go5
-rw-r--r--internal/testgit/repo_hash_object.go2
-rw-r--r--internal/testgit/repo_make_commit.go1
-rw-r--r--internal/testgit/repo_make_single_file_tree.go1
-rw-r--r--internal/testgit/repo_mktree.go2
-rw-r--r--internal/testgit/repo_new.go4
-rw-r--r--internal/testgit/repo_refs.go4
-rw-r--r--internal/testgit/repo_repack.go1
-rw-r--r--internal/testgit/repo_rev_parse.go2
-rw-r--r--internal/testgit/repo_run.go7
-rw-r--r--internal/testgit/repo_tag_annotated.go1
13 files changed, 32 insertions, 0 deletions
diff --git a/internal/testgit/algorithms.go b/internal/testgit/algorithms.go
index 81af4f75..5534aad0 100644
--- a/internal/testgit/algorithms.go
+++ b/internal/testgit/algorithms.go
@@ -9,6 +9,7 @@ import (
// ForEachAlgorithm runs a subtest for every supported algorithm.
func ForEachAlgorithm(t *testing.T, fn func(t *testing.T, algo objectid.Algorithm)) {
t.Helper()
+
for _, algo := range objectid.SupportedAlgorithms() {
t.Run(algo.String(), func(t *testing.T) {
fn(t, algo)
diff --git a/internal/testgit/repo_cat_file.go b/internal/testgit/repo_cat_file.go
index 9cc56db6..1325cf6f 100644
--- a/internal/testgit/repo_cat_file.go
+++ b/internal/testgit/repo_cat_file.go
@@ -9,5 +9,6 @@ import (
// CatFile returns raw output from git cat-file.
func (testRepo *TestRepo) CatFile(tb testing.TB, mode string, id objectid.ObjectID) []byte {
tb.Helper()
+
return testRepo.RunBytes(tb, "cat-file", mode, id.String())
}
diff --git a/internal/testgit/repo_commit_tree.go b/internal/testgit/repo_commit_tree.go
index 763474c2..5eee21ba 100644
--- a/internal/testgit/repo_commit_tree.go
+++ b/internal/testgit/repo_commit_tree.go
@@ -9,16 +9,21 @@ import (
// CommitTree creates a commit from a tree and message, optionally with parents.
func (testRepo *TestRepo) CommitTree(tb testing.TB, tree objectid.ObjectID, message string, parents ...objectid.ObjectID) objectid.ObjectID {
tb.Helper()
+
args := make([]string, 0, 2+2*len(parents)+2)
+
args = append(args, "commit-tree", tree.String())
for _, p := range parents {
args = append(args, "-p", p.String())
}
+
args = append(args, "-m", message)
hex := testRepo.Run(tb, args...)
+
id, err := objectid.ParseHex(testRepo.algo, hex)
if err != nil {
tb.Fatalf("parse commit-tree output %q: %v", hex, err)
}
+
return id
}
diff --git a/internal/testgit/repo_hash_object.go b/internal/testgit/repo_hash_object.go
index 10a05381..bc2def72 100644
--- a/internal/testgit/repo_hash_object.go
+++ b/internal/testgit/repo_hash_object.go
@@ -10,9 +10,11 @@ import (
func (testRepo *TestRepo) HashObject(tb testing.TB, objType string, body []byte) objectid.ObjectID {
tb.Helper()
hex := testRepo.RunInput(tb, body, "hash-object", "-t", objType, "-w", "--stdin")
+
id, err := objectid.ParseHex(testRepo.algo, hex)
if err != nil {
tb.Fatalf("parse git hash-object output %q: %v", hex, err)
}
+
return id
}
diff --git a/internal/testgit/repo_make_commit.go b/internal/testgit/repo_make_commit.go
index a569dfb1..c8bdc428 100644
--- a/internal/testgit/repo_make_commit.go
+++ b/internal/testgit/repo_make_commit.go
@@ -11,5 +11,6 @@ func (testRepo *TestRepo) MakeCommit(tb testing.TB, message string) (objectid.Ob
tb.Helper()
blobID, treeID := testRepo.MakeSingleFileTree(tb, "file.txt", []byte("commit-body\n"))
commitID := testRepo.CommitTree(tb, treeID, message)
+
return blobID, treeID, commitID
}
diff --git a/internal/testgit/repo_make_single_file_tree.go b/internal/testgit/repo_make_single_file_tree.go
index 7c53c658..e7a235a7 100644
--- a/internal/testgit/repo_make_single_file_tree.go
+++ b/internal/testgit/repo_make_single_file_tree.go
@@ -13,5 +13,6 @@ func (testRepo *TestRepo) MakeSingleFileTree(tb testing.TB, fileName string, fil
blobID := testRepo.HashObject(tb, "blob", fileContent)
treeInput := fmt.Sprintf("100644 blob %s\t%s\n", blobID.String(), fileName)
treeID := testRepo.Mktree(tb, treeInput)
+
return blobID, treeID
}
diff --git a/internal/testgit/repo_mktree.go b/internal/testgit/repo_mktree.go
index 34e6388d..565a0083 100644
--- a/internal/testgit/repo_mktree.go
+++ b/internal/testgit/repo_mktree.go
@@ -10,9 +10,11 @@ import (
func (testRepo *TestRepo) Mktree(tb testing.TB, input string) objectid.ObjectID {
tb.Helper()
hex := testRepo.RunInput(tb, []byte(input), "mktree")
+
id, err := objectid.ParseHex(testRepo.algo, hex)
if err != nil {
tb.Fatalf("parse mktree output %q: %v", hex, err)
}
+
return id
}
diff --git a/internal/testgit/repo_new.go b/internal/testgit/repo_new.go
index 8120a9a2..8a71e406 100644
--- a/internal/testgit/repo_new.go
+++ b/internal/testgit/repo_new.go
@@ -21,6 +21,7 @@ type RepoOptions struct {
// NewRepo creates a temporary repository initialized with the requested options.
func NewRepo(tb testing.TB, opts RepoOptions) *TestRepo {
tb.Helper()
+
algo := opts.ObjectFormat
if algo.Size() == 0 {
tb.Fatalf("invalid algorithm: %v", algo)
@@ -47,10 +48,13 @@ func NewRepo(tb testing.TB, opts RepoOptions) *TestRepo {
if opts.Bare {
args = append(args, "--bare")
}
+
if opts.RefFormat != "" {
args = append(args, "--ref-format="+opts.RefFormat)
}
+
args = append(args, dir)
testRepo.runBytes(tb, nil, "", args...)
+
return testRepo
}
diff --git a/internal/testgit/repo_refs.go b/internal/testgit/repo_refs.go
index eb09a78b..66e08561 100644
--- a/internal/testgit/repo_refs.go
+++ b/internal/testgit/repo_refs.go
@@ -28,6 +28,7 @@ func (testRepo *TestRepo) SymbolicRef(tb testing.TB, name, target string) {
// PackRefs runs git pack-refs with args.
func (testRepo *TestRepo) PackRefs(tb testing.TB, args ...string) {
tb.Helper()
+
cmd := append([]string{"pack-refs"}, args...)
testRepo.Run(tb, cmd...)
}
@@ -35,10 +36,13 @@ func (testRepo *TestRepo) PackRefs(tb testing.TB, args ...string) {
// ShowRef returns lines from git show-ref output.
func (testRepo *TestRepo) ShowRef(tb testing.TB, args ...string) []string {
tb.Helper()
+
cmd := append([]string{"show-ref"}, args...)
+
out := testRepo.Run(tb, cmd...)
if strings.TrimSpace(out) == "" {
return nil
}
+
return strings.Split(strings.TrimSpace(out), "\n")
}
diff --git a/internal/testgit/repo_repack.go b/internal/testgit/repo_repack.go
index 29fa8a4f..7773ac13 100644
--- a/internal/testgit/repo_repack.go
+++ b/internal/testgit/repo_repack.go
@@ -5,6 +5,7 @@ import "testing"
// Repack runs "git repack" with args in the repository.
func (testRepo *TestRepo) Repack(tb testing.TB, args ...string) {
tb.Helper()
+
cmdArgs := make([]string, 0, len(args)+1)
cmdArgs = append(cmdArgs, "repack")
cmdArgs = append(cmdArgs, args...)
diff --git a/internal/testgit/repo_rev_parse.go b/internal/testgit/repo_rev_parse.go
index bebdfa8e..3bee6108 100644
--- a/internal/testgit/repo_rev_parse.go
+++ b/internal/testgit/repo_rev_parse.go
@@ -10,9 +10,11 @@ import (
func (testRepo *TestRepo) RevParse(tb testing.TB, spec string) objectid.ObjectID {
tb.Helper()
hex := testRepo.Run(tb, "rev-parse", spec)
+
id, err := objectid.ParseHex(testRepo.algo, hex)
if err != nil {
tb.Fatalf("parse rev-parse output %q: %v", hex, err)
}
+
return id
}
diff --git a/internal/testgit/repo_run.go b/internal/testgit/repo_run.go
index 8022835e..162a0d72 100644
--- a/internal/testgit/repo_run.go
+++ b/internal/testgit/repo_run.go
@@ -11,12 +11,14 @@ import (
func (testRepo *TestRepo) Run(tb testing.TB, args ...string) string {
tb.Helper()
out := testRepo.runBytes(tb, nil, testRepo.dir, args...)
+
return strings.TrimSpace(string(out))
}
// RunBytes executes git and returns raw output bytes.
func (testRepo *TestRepo) RunBytes(tb testing.TB, args ...string) []byte {
tb.Helper()
+
return testRepo.runBytes(tb, nil, testRepo.dir, args...)
}
@@ -24,12 +26,14 @@ func (testRepo *TestRepo) RunBytes(tb testing.TB, args ...string) []byte {
func (testRepo *TestRepo) RunInput(tb testing.TB, stdin []byte, args ...string) string {
tb.Helper()
out := testRepo.runBytes(tb, stdin, testRepo.dir, args...)
+
return strings.TrimSpace(string(out))
}
// RunInputBytes executes git with stdin and returns raw output bytes.
func (testRepo *TestRepo) RunInputBytes(tb testing.TB, stdin []byte, args ...string) []byte {
tb.Helper()
+
return testRepo.runBytes(tb, stdin, testRepo.dir, args...)
}
@@ -38,13 +42,16 @@ func (testRepo *TestRepo) runBytes(tb testing.TB, stdin []byte, dir string, args
//nolint:noctx
cmd := exec.Command("git", args...) //#nosec G204
cmd.Dir = dir
+
cmd.Env = testRepo.env
if stdin != nil {
cmd.Stdin = bytes.NewReader(stdin)
}
+
out, err := cmd.CombinedOutput()
if err != nil {
tb.Fatalf("git %v failed: %v\n%s", args, err, out)
}
+
return out
}
diff --git a/internal/testgit/repo_tag_annotated.go b/internal/testgit/repo_tag_annotated.go
index a3ffafa6..7e9bfbf5 100644
--- a/internal/testgit/repo_tag_annotated.go
+++ b/internal/testgit/repo_tag_annotated.go
@@ -11,5 +11,6 @@ import (
func (testRepo *TestRepo) TagAnnotated(tb testing.TB, name string, target objectid.ObjectID, message string) objectid.ObjectID {
tb.Helper()
testRepo.Run(tb, "tag", "-a", name, target.String(), "-m", message)
+
return testRepo.RevParse(tb, fmt.Sprintf("refs/tags/%s", name))
}