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 84fcdfb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,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

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 728 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: ErrInvalidTagName
}

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

_, err := r.Storer.Reference(rname)
Expand Down Expand Up @@ -757,6 +761,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

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (v2.11.0, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / test (master, ubuntu-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.21.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, windows-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: ErrInvalidTagName

Check failure on line 765 in repository.go

View workflow job for this annotation

GitHub Actions / version-matrix (1.20.x, macos-latest)

undefined: 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 84fcdfb

Please sign in to comment.