aboutsummaryrefslogtreecommitdiff
path: root/internal/testgit/tag.go
blob: 139edf772d9c879e23e9811a0867cbc0630fbea5 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package testgit

import (
	"fmt"
	"testing"

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

// TagAnnotatedOptions configures [Repo.TagAnnotated].
type TagAnnotatedOptions struct {
	Message    string
	Tagger     Identity
	TaggerDate string

	// Sign requests a signed tag via git tag -s,
	// using the gpg.format and user.signingkey configured on the repo.
	Sign bool
}

// TagAnnotated creates an annotated tag object and returns its object ID.
func (repo *Repo) TagAnnotated(
	tb testing.TB,
	name string,
	target id.ObjectID,
	opts TagAnnotatedOptions,
) (id.ObjectID, error) {
	tb.Helper()

	args := []string{"tag", "-a"}
	if opts.Sign {
		args = append(args, "-s")
	}

	args = append(args, "-m", opts.Message, "--end-of-options", name, target.String())

	cmd := repo.command(tb, "git", args...)

	if opts.Tagger.Name != "" {
		cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_NAME", opts.Tagger.Name)
	}

	if opts.Tagger.Email != "" {
		cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_EMAIL", opts.Tagger.Email)
	}

	if opts.TaggerDate != "" {
		cmd.Env = setEnv(cmd.Env, "GIT_COMMITTER_DATE", opts.TaggerDate)
	}

	err := cmd.Run()
	if err != nil {
		return id.ObjectID{}, fmt.Errorf("tag -a %q %s: %w", name, target, err)
	}

	return repo.RevParse(tb, "refs/tags/"+name)
}