diff options
Diffstat (limited to 'cmd/gitaly-git2go/testhelper/testhelper.go')
-rw-r--r-- | cmd/gitaly-git2go/testhelper/testhelper.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cmd/gitaly-git2go/testhelper/testhelper.go b/cmd/gitaly-git2go/testhelper/testhelper.go new file mode 100644 index 000000000..3a7a24b0d --- /dev/null +++ b/cmd/gitaly-git2go/testhelper/testhelper.go @@ -0,0 +1,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 +} |