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

import (
	"io"
	"testing"

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

// HashObject hashes and writes an object,
// and returns its object ID.
func (repo *Repo) HashObject(tb testing.TB, ty typ.Type, body io.Reader) id.ObjectID {
	tb.Helper()
	cmd := repo.Command(tb, "git", "hash-object", "-t", ty.Name(), "-w", "--stdin", "--literally")

	hex, err := cmd.CombinedOutput()
	if err != nil {
		tb.Fatalf("hash-object: %v", hex)
	}

	id, err := id.FromHex(repo.objectFormat, string(hex))
	if err != nil {
		tb.Fatalf("parse git hash-object output %q: %v", hex, err)
	}

	return id
}