Skip to content

repo_tag: able to create annotated tag #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func (c *Command) AddEnvs(envs ...string) *Command {
return c
}

// AddCommitter appends given committer to the command.
func (c *Command) AddCommitter(committer *Signature) *Command {
c.AddEnvs("GIT_COMMITTER_NAME="+committer.Name, "GIT_COMMITTER_EMAIL="+committer.Email)
return c
}

// DefaultTimeout is the default timeout duration for all commands.
const DefaultTimeout = time.Minute

Expand Down
2 changes: 1 addition & 1 deletion repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func CreateCommit(repoPath string, committer *Signature, message string, opts ..
}

cmd := NewCommand("commit")
cmd.AddEnvs("GIT_COMMITTER_NAME="+committer.Name, "GIT_COMMITTER_EMAIL="+committer.Email)
cmd.AddCommitter(committer)

if opt.Author == nil {
opt.Author = committer
Expand Down
21 changes: 20 additions & 1 deletion repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ func (r *Repository) Tags(opts ...TagsOptions) ([]string, error) {
//
// Docs: https://git-scm.com/docs/git-tag
type CreateTagOptions struct {
// Annotated marks a tag as annotated rather than lightweight.
Annotated bool
// Message specifies a tagging message for the annotated tag. It is ignored when tag is not annotated.
Message string
// Author is the author of the tag. It is ignored when tag is not annotated.
Author *Signature
// The timeout duration before giving up for each shell command execution. The
// default timeout duration will be used when not supplied.
Timeout time.Duration
Expand All @@ -207,7 +213,20 @@ func (r *Repository) CreateTag(name, rev string, opts ...CreateTagOptions) error
opt = opts[0]
}

_, err := NewCommand("tag", name, rev).RunInDirWithTimeout(opt.Timeout, r.path)
cmd := NewCommand("tag")
if opt.Annotated {
cmd.AddArgs("-a", name)
cmd.AddArgs("--message", opt.Message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this actually works without error.

if opt.Author != nil {
cmd.AddCommitter(opt.Author)
}
} else {
cmd.AddArgs(name)
}

cmd.AddArgs(rev)

_, err := cmd.RunInDirWithTimeout(opt.Timeout, r.path)
return err
}

Expand Down
32 changes: 32 additions & 0 deletions repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ func TestRepository_CreateTag(t *testing.T) {
assert.True(t, r.HasReference(RefsTags+"v2.0.0"))
}

func TestRepository_CreateAnnotatedTag(t *testing.T) {
r, cleanup, err := setupTempRepo()
if err != nil {
t.Fatal(err)
}
defer cleanup()

assert.False(t, r.HasReference(RefsTags+"v2.0.0"))

err = r.CreateTag("v2.0.0", "master", CreateTagOptions{
Annotated: true,
Author: &Signature{
Name: "alice",
Email: "[email protected]",
},
})
if err != nil {
t.Fatal(err)
}

assert.True(t, r.HasReference(RefsTags+"v2.0.0"))

tag, err := r.Tag("v2.0.0")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "alice", tag.tagger.Name)
assert.Equal(t, "[email protected]", tag.tagger.Email)
assert.False(t, tag.tagger.When.IsZero())
}

func TestRepository_DeleteTag(t *testing.T) {
r, cleanup, err := setupTempRepo()
if err != nil {
Expand Down