aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/commit.go
blob: 09a5438d4f89ff7e1931ac210d6586eefbf407be (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
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
}