aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/revparse.go
blob: ffa5b7025e86731a00b0cc22c64e551eadef77d1 (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
package testgit

import (
	"fmt"
	"strings"
	"testing"

	"lindenii.org/go/furgit/object/id"
)

// RevParse resolves one revision spec to an object ID.
func (repo *Repo) RevParse(tb testing.TB, spec string) (id.ObjectID, error) {
	tb.Helper()

	stdout, err := repo.run(tb, nil, "git", "rev-parse", "--verify", "--end-of-options", spec)
	if err != nil {
		return id.ObjectID{}, fmt.Errorf("rev-parse %q: %w", spec, err)
	}

	objectID, err := repo.objectFormat.FromString(strings.TrimSuffix(string(stdout), "\n"))
	if err != nil {
		return id.ObjectID{}, fmt.Errorf("parse git rev-parse output %q: %w", string(stdout), err)
	}

	return objectID, nil
}