Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testhelper.go « testhelper « gitaly-git2go-v15 « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ba1e65cd3d06e15f91a4f80cb95ca071a59cce4 (plain)
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
//go:build static && system_libgit2
// +build static,system_libgit2

package testhelper

import (
	"testing"
	"time"

	git "github.com/libgit2/git2go/v33"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v15/cmd/gitaly-git2go-v15/git2goutil"
)

// DefaultAuthor is the author used by BuildCommit
var DefaultAuthor = git.Signature{
	Name:  "Foo",
	Email: "foo@example.com",
	When:  time.Date(2020, 1, 1, 1, 1, 1, 0, time.FixedZone("", 2*60*60)),
}

//nolint: revive,stylecheck // This is unintentionally missing documentation.
func BuildCommit(t testing.TB, repoPath string, parents []*git.Oid, fileContents map[string]string) *git.Oid {
	repo, err := git2goutil.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)
		require.NoError(t, treeBuilder.Insert(file, oid, git.FilemodeBlob))
	}

	tree, err := treeBuilder.Write()
	require.NoError(t, err)

	var commit *git.Oid
	commit, err = repo.CreateCommitFromIds("", &DefaultAuthor, &DefaultAuthor, "Message", tree, parents...)
	require.NoError(t, err)

	return commit
}