Fast implementation of Git in pure Go
1package testgit
2
3import (
4 "slices"
5 "strings"
6 "testing"
7
8 "codeberg.org/lindenii/furgit/objectid"
9)
10
11// CommitTreeWithEnv creates one commit from a tree and message, optionally with
12// parents, using additional environment variables for the git subprocess.
13func (testRepo *TestRepo) CommitTreeWithEnv(
14 tb testing.TB,
15 extraEnv []string,
16 tree objectid.ObjectID,
17 message string,
18 parents ...objectid.ObjectID,
19) objectid.ObjectID {
20 tb.Helper()
21
22 args := make([]string, 0, 2+2*len(parents)+2)
23
24 args = append(args, "commit-tree", tree.String())
25 for _, parent := range parents {
26 args = append(args, "-p", parent.String())
27 }
28
29 args = append(args, "-m", message)
30 hex := testRepo.runWithExtraEnv(tb, extraEnv, args...)
31
32 id, err := objectid.ParseHex(testRepo.algo, hex)
33 if err != nil {
34 tb.Fatalf("parse commit-tree output %q: %v", hex, err)
35 }
36
37 return id
38}
39
40func (testRepo *TestRepo) runWithExtraEnv(tb testing.TB, extraEnv []string, args ...string) string {
41 tb.Helper()
42
43 env := slices.Concat(testRepo.env, extraEnv)
44
45 out, err := testRepo.runBytesWithEnv(tb, nil, testRepo.dir, env, args...)
46 if err != nil {
47 tb.Fatalf("git %v failed: %v\n%s", args, err, out)
48 }
49
50 return strings.TrimSpace(string(out))
51}