aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/repo_commit_tree_env.go
blob: fbddf26f19d147c4225c433f08694c08eb6b5121 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package testgit

import (
	"slices"
	"strings"
	"testing"

	objectid "codeberg.org/lindenii/furgit/object/id"
)

// CommitTreeWithEnv creates one commit from a tree and message, optionally with
// parents, using additional environment variables for the git subprocess.
func (testRepo *TestRepo) CommitTreeWithEnv(
	tb testing.TB,
	extraEnv []string,
	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 _, parent := range parents {
		args = append(args, "-p", parent.String())
	}

	args = append(args, "-m", message)
	hex := testRepo.runWithExtraEnv(tb, extraEnv, args...)

	id, err := objectid.ParseHex(testRepo.algo, hex)
	if err != nil {
		tb.Fatalf("parse commit-tree output %q: %v", hex, err)
	}

	return id
}

func (testRepo *TestRepo) runWithExtraEnv(tb testing.TB, extraEnv []string, args ...string) string {
	tb.Helper()

	env := slices.Concat(testRepo.env, extraEnv)

	out, err := testRepo.runBytesWithEnv(tb, nil, testRepo.dir, env, args...)
	if err != nil {
		tb.Fatalf("git %v failed: %v\n%s", args, err, out)
	}

	return strings.TrimSpace(string(out))
}