Skip to content

Commit

Permalink
git: error on empty tag name (go-git#929)
Browse files Browse the repository at this point in the history
An empty tagname is an invalid tag.

Fixes: go-git#929
  • Loading branch information
aymanbagabas committed Nov 28, 2023
1 parent a3b3d53 commit e3cbe0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var (
// ErrFetching is returned when the packfile could not be downloaded
ErrFetching = errors.New("unable to fetch packfile")

// ErrInvalidTagName is returned when the tag name is invalid.
ErrInvalidTagName = errors.New("invalid tag name")

ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch")
ErrRepositoryNotExists = errors.New("repository does not exist")
ErrRepositoryIncomplete = errors.New("repository's commondir path does not exist")
Expand Down Expand Up @@ -724,6 +727,10 @@ func (r *Repository) DeleteBranch(name string) error {
// CreateTag creates a tag. If opts is included, the tag is an annotated tag,
// otherwise a lightweight tag is created.
func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *CreateTagOptions) (*plumbing.Reference, error) {
if name == "" {
return nil, ErrInvalidTagName
}

rname := plumbing.ReferenceName(path.Join("refs", "tags", name))

_, err := r.Storer.Reference(rname)
Expand Down Expand Up @@ -757,6 +764,10 @@ func (r *Repository) CreateTag(name string, hash plumbing.Hash, opts *CreateTagO
}

func (r *Repository) createTagObject(name string, hash plumbing.Hash, opts *CreateTagOptions) (plumbing.Hash, error) {
if name == "" {
return plumbing.ZeroHash, ErrInvalidTagName
}

if err := opts.Validate(r, hash); err != nil {
return plumbing.ZeroHash, err
}
Expand Down
8 changes: 8 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3265,6 +3265,14 @@ func (s *RepositorySuite) TestIssue674(c *C) {
c.Check(h.IsZero(), Equals, true)
}

func (s *RepositorySuite) TestEmptyTagName(c *C) {
r, _ := Init(memory.NewStorage(), nil)
_, err := r.CreateTag("", plumbing.ZeroHash, nil)

c.Assert(err, NotNil)
c.Assert(err, ErrorMatches, "invalid tag name")
}

func BenchmarkObjects(b *testing.B) {
defer fixtures.Clean()

Expand Down

0 comments on commit e3cbe0e

Please sign in to comment.