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
|
// +build static,system_libgit2
package testhelper
import (
"testing"
"time"
git "github.com/libgit2/git2go/v30"
"github.com/stretchr/testify/require"
)
func BuildCommit(t testing.TB, repoPath string, parent *git.Oid, fileContents map[string]string) *git.Oid {
repo, err := git.OpenRepository(repoPath)
require.NoError(t, err)
defer repo.Free()
odb, err := repo.Odb()
require.NoError(t, err)
treeBuilder, err := repo.TreeBuilder()
require.NoError(t, err)
for file, contents := range fileContents {
oid, err := odb.Write([]byte(contents), git.ObjectBlob)
require.NoError(t, err)
treeBuilder.Insert(file, oid, git.FilemodeBlob)
}
tree, err := treeBuilder.Write()
require.NoError(t, err)
committer := git.Signature{
Name: "Foo",
Email: "foo@example.com",
When: time.Date(2020, 1, 1, 1, 1, 1, 1, time.FixedZone("UTC+2", 2*60*60)),
}
var commit *git.Oid
if parent != nil {
commit, err = repo.CreateCommitFromIds("", &committer, &committer, "Message", tree, parent)
} else {
commit, err = repo.CreateCommitFromIds("", &committer, &committer, "Message", tree)
}
require.NoError(t, err)
return commit
}
|