Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 01631f0

Browse files
committed
git: Don't return tag object with Tag, adjust docs for Tag and Tags
I've mainly noticed that in using the current Tag function, that there were lots of times that I was ignoring the ref or the object, depending on what I needed. This was evident in the tests as well. As such, I think it just makes more sense for a singular tag fetcher to return just a ref, through which the caller may grab the annotation if they need it, and if it exists. Also, contrary to the docs on Tags, all tags have a ref, even if they are annotated. The difference between a lightweight tag and an annotated tag is the presence of the tag object, which the ref will point to if the tag is annotated. As such I've adjusted the docs with an example as to how one can get the annotation for a tag ref through the iterator. Source: https://git-scm.com/book/en/v2/Git-Internals-Git-References, tags section. Signed-off-by: Chris Marchesi <chrism@vancluevertech.com>
1 parent 119459a commit 01631f0

File tree

2 files changed

+71
-50
lines changed

2 files changed

+71
-50
lines changed

repository.go

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -581,34 +581,52 @@ func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity)
581581
return b.String(), nil
582582
}
583583

584-
// Tag fetches a tag from the repository. The tag is returned as a raw
585-
// reference. If the tag is annotated, a non-nil tag object is returned.
586-
func (r *Repository) Tag(name string) (*plumbing.Reference, *object.Tag, error) {
584+
// Tag fetches a tag from the repository.
585+
//
586+
// If you want to check to see if the tag is an annotated tag, you can call
587+
// TagObject on the hash of the reference in ForEach:
588+
//
589+
// ref, err := r.Tag("v0.1.0")
590+
// if err != nil {
591+
// // Handle error
592+
// }
593+
//
594+
// obj, err := r.TagObject(ref.Hash())
595+
// switch err {
596+
// case nil:
597+
// // Tag object present
598+
// case plumbing.ErrObjectNotFound:
599+
// // Not a tag object
600+
// default:
601+
// // Some other error
602+
// }
603+
//
604+
func (r *Repository) Tag(name string) (*plumbing.Reference, error) {
587605
ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false)
588606
if err != nil {
589607
if err == plumbing.ErrReferenceNotFound {
590608
// Return a friendly error for this one, versus just ReferenceNotFound.
591-
return nil, nil, ErrTagNotFound
609+
return nil, ErrTagNotFound
592610
}
593611

594-
return nil, nil, err
595-
}
596-
597-
obj, err := r.TagObject(ref.Hash())
598-
if err != nil && err != plumbing.ErrObjectNotFound {
599-
return nil, nil, err
612+
return nil, err
600613
}
601614

602-
return ref, obj, nil
615+
return ref, nil
603616
}
604617

605618
// DeleteTag deletes a tag from the repository.
606619
func (r *Repository) DeleteTag(name string) error {
607-
_, obj, err := r.Tag(name)
620+
ref, err := r.Tag(name)
608621
if err != nil {
609622
return err
610623
}
611624

625+
obj, err := r.TagObject(ref.Hash())
626+
if err != nil && err != plumbing.ErrObjectNotFound {
627+
return err
628+
}
629+
612630
if err = r.Storer.RemoveReference(plumbing.ReferenceName(path.Join("refs", "tags", name))); err != nil {
613631
return err
614632
}
@@ -982,9 +1000,31 @@ func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) {
9821000
return nil, fmt.Errorf("invalid Order=%v", o.Order)
9831001
}
9841002

985-
// Tags returns all the References from Tags. This method returns only lightweight
986-
// tags. Note that not all the tags are lightweight ones. To return annotated tags
987-
// too, you need to call TagObjects() method.
1003+
// Tags returns all the tag References in a repository.
1004+
//
1005+
// If you want to check to see if the tag is an annotated tag, you can call
1006+
// TagObject on the hash Reference passed in through ForEach:
1007+
//
1008+
// iter, err := r.Tags()
1009+
// if err != nil {
1010+
// // Handle error
1011+
// }
1012+
//
1013+
// if err := iter.ForEach(func (ref *plumbing.Reference) error {
1014+
// obj, err := r.TagObject(ref.Hash())
1015+
// switch err {
1016+
// case nil:
1017+
// // Tag object present
1018+
// case plumbing.ErrObjectNotFound:
1019+
// // Not a tag object
1020+
// default:
1021+
// // Some other error
1022+
// return err
1023+
// }
1024+
// }); err != nil {
1025+
// // Handle outer iterator error
1026+
// }
1027+
//
9881028
func (r *Repository) Tags() (storer.ReferenceIter, error) {
9891029
refIter, err := r.Storer.IterReferences()
9901030
if err != nil {

repository_test.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,9 +1294,8 @@ func (s *RepositorySuite) TestCreateTagLightweight(c *C) {
12941294
c.Assert(err, IsNil)
12951295
c.Assert(ref, NotNil)
12961296

1297-
actual, obj, err := r.Tag("foobar")
1297+
actual, err := r.Tag("foobar")
12981298
c.Assert(err, IsNil)
1299-
c.Assert(obj, IsNil)
13001299

13011300
c.Assert(expected.Hash(), Equals, actual.Hash())
13021301
}
@@ -1338,9 +1337,11 @@ func (s *RepositorySuite) TestCreateTagAnnotated(c *C) {
13381337
})
13391338
c.Assert(err, IsNil)
13401339

1341-
tag, obj, err := r.Tag("foobar")
1340+
tag, err := r.Tag("foobar")
1341+
c.Assert(err, IsNil)
1342+
1343+
obj, err := r.TagObject(tag.Hash())
13421344
c.Assert(err, IsNil)
1343-
c.Assert(obj, NotNil)
13441345

13451346
c.Assert(ref, DeepEquals, tag)
13461347
c.Assert(obj.Hash, Equals, ref.Hash())
@@ -1412,9 +1413,11 @@ func (s *RepositorySuite) TestCreateTagSigned(c *C) {
14121413
})
14131414
c.Assert(err, IsNil)
14141415

1415-
_, obj, err := r.Tag("foobar")
1416+
tag, err := r.Tag("foobar")
1417+
c.Assert(err, IsNil)
1418+
1419+
obj, err := r.TagObject(tag.Hash())
14161420
c.Assert(err, IsNil)
1417-
c.Assert(obj, NotNil)
14181421

14191422
// Verify the tag.
14201423
pks := new(bytes.Buffer)
@@ -1472,9 +1475,11 @@ func (s *RepositorySuite) TestCreateTagCanonicalize(c *C) {
14721475
})
14731476
c.Assert(err, IsNil)
14741477

1475-
_, obj, err := r.Tag("foobar")
1478+
tag, err := r.Tag("foobar")
1479+
c.Assert(err, IsNil)
1480+
1481+
obj, err := r.TagObject(tag.Hash())
14761482
c.Assert(err, IsNil)
1477-
c.Assert(obj, NotNil)
14781483

14791484
// Assert the new canonicalized message.
14801485
c.Assert(obj.Message, Equals, "foo bar baz qux\n\nsome message here\n")
@@ -1505,9 +1510,8 @@ func (s *RepositorySuite) TestTagLightweight(c *C) {
15051510

15061511
expected := plumbing.NewHash("f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
15071512

1508-
tag, obj, err := r.Tag("lightweight-tag")
1513+
tag, err := r.Tag("lightweight-tag")
15091514
c.Assert(err, IsNil)
1510-
c.Assert(obj, IsNil)
15111515

15121516
actual := tag.Hash()
15131517
c.Assert(expected, Equals, actual)
@@ -1522,34 +1526,11 @@ func (s *RepositorySuite) TestTagLightweightMissingTag(c *C) {
15221526
err := r.clone(context.Background(), &CloneOptions{URL: url})
15231527
c.Assert(err, IsNil)
15241528

1525-
tag, obj, err := r.Tag("lightweight-tag-tag")
1529+
tag, err := r.Tag("lightweight-tag-tag")
15261530
c.Assert(tag, IsNil)
1527-
c.Assert(obj, IsNil)
15281531
c.Assert(err, Equals, ErrTagNotFound)
15291532
}
15301533

1531-
func (s *RepositorySuite) TestTagAnnotated(c *C) {
1532-
url := s.GetLocalRepositoryURL(
1533-
fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
1534-
)
1535-
1536-
r, _ := Init(memory.NewStorage(), nil)
1537-
err := r.clone(context.Background(), &CloneOptions{URL: url})
1538-
c.Assert(err, IsNil)
1539-
1540-
tag, obj, err := r.Tag("annotated-tag")
1541-
c.Assert(err, IsNil)
1542-
c.Assert(obj, NotNil)
1543-
1544-
expectedHash := plumbing.NewHash("b742a2a9fa0afcfa9a6fad080980fbc26b007c69")
1545-
expectedTarget := plumbing.NewHash("f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
1546-
actualHash := tag.Hash()
1547-
c.Assert(expectedHash, Equals, actualHash)
1548-
c.Assert(obj.Hash, Equals, expectedHash)
1549-
c.Assert(obj.Type(), Equals, plumbing.TagObject)
1550-
c.Assert(obj.Target, Equals, expectedTarget)
1551-
}
1552-
15531534
func (s *RepositorySuite) TestDeleteTag(c *C) {
15541535
url := s.GetLocalRepositoryURL(
15551536
fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
@@ -1562,7 +1543,7 @@ func (s *RepositorySuite) TestDeleteTag(c *C) {
15621543
err = r.DeleteTag("lightweight-tag")
15631544
c.Assert(err, IsNil)
15641545

1565-
_, _, err = r.Tag("lightweight-tag")
1546+
_, err = r.Tag("lightweight-tag")
15661547
c.Assert(err, Equals, ErrTagNotFound)
15671548
}
15681549

0 commit comments

Comments
 (0)