aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-03-02 20:01:04 +0000
committerPaulo Gomes <pjbgf@linux.com>2023-03-02 21:17:11 +0000
commit3486338715d0c1385992c6ca8db6bd04fd0df135 (patch)
tree7e50a6e47300559e2fafee41d31fe8744608901d
parent3389b571c09bdc919d2acf31055996534ef96c69 (diff)
downloadgo-git-3486338715d0c1385992c6ca8db6bd04fd0df135.tar.gz
*: Fix panic for empty revisions
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
-rw-r--r--repository.go76
-rw-r--r--repository_test.go9
2 files changed, 50 insertions, 35 deletions
diff --git a/repository.go b/repository.go
index 7292df6..2a06f8b 100644
--- a/repository.go
+++ b/repository.go
@@ -750,21 +750,20 @@ func (r *Repository) buildTagSignature(tag *object.Tag, signKey *openpgp.Entity)
// If you want to check to see if the tag is an annotated tag, you can call
// TagObject on the hash of the reference in ForEach:
//
-// ref, err := r.Tag("v0.1.0")
-// if err != nil {
-// // Handle error
-// }
-//
-// obj, err := r.TagObject(ref.Hash())
-// switch err {
-// case nil:
-// // Tag object present
-// case plumbing.ErrObjectNotFound:
-// // Not a tag object
-// default:
-// // Some other error
-// }
+// ref, err := r.Tag("v0.1.0")
+// if err != nil {
+// // Handle error
+// }
//
+// obj, err := r.TagObject(ref.Hash())
+// switch err {
+// case nil:
+// // Tag object present
+// case plumbing.ErrObjectNotFound:
+// // Not a tag object
+// default:
+// // Some other error
+// }
func (r *Repository) Tag(name string) (*plumbing.Reference, error) {
ref, err := r.Reference(plumbing.ReferenceName(path.Join("refs", "tags", name)), false)
if err != nil {
@@ -1241,26 +1240,25 @@ func commitIterFunc(order LogOrder) func(c *object.Commit) object.CommitIter {
// If you want to check to see if the tag is an annotated tag, you can call
// TagObject on the hash Reference passed in through ForEach:
//
-// iter, err := r.Tags()
-// if err != nil {
-// // Handle error
-// }
-//
-// if err := iter.ForEach(func (ref *plumbing.Reference) error {
-// obj, err := r.TagObject(ref.Hash())
-// switch err {
-// case nil:
-// // Tag object present
-// case plumbing.ErrObjectNotFound:
-// // Not a tag object
-// default:
-// // Some other error
-// return err
-// }
-// }); err != nil {
-// // Handle outer iterator error
-// }
+// iter, err := r.Tags()
+// if err != nil {
+// // Handle error
+// }
//
+// if err := iter.ForEach(func (ref *plumbing.Reference) error {
+// obj, err := r.TagObject(ref.Hash())
+// switch err {
+// case nil:
+// // Tag object present
+// case plumbing.ErrObjectNotFound:
+// // Not a tag object
+// default:
+// // Some other error
+// return err
+// }
+// }); err != nil {
+// // Handle outer iterator error
+// }
func (r *Repository) Tags() (storer.ReferenceIter, error) {
refIter, err := r.Storer.IterReferences()
if err != nil {
@@ -1424,9 +1422,13 @@ func (r *Repository) Worktree() (*Worktree, error) {
//
// Implemented resolvers : HEAD, branch, tag, heads/branch, refs/heads/branch,
// refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, master~^, tag~2, ref/heads/master~1, ...), selection by text (HEAD^{/fix nasty bug}), hash (prefix and full)
-func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, error) {
- p := revision.NewParserFromString(string(rev))
+func (r *Repository) ResolveRevision(in plumbing.Revision) (*plumbing.Hash, error) {
+ rev := in.String()
+ if rev == "" {
+ return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound
+ }
+ p := revision.NewParserFromString(rev)
items, err := p.Parse()
if err != nil {
@@ -1557,6 +1559,10 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err
}
}
+ if commit == nil {
+ return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound
+ }
+
return &commit.Hash, nil
}
diff --git a/repository_test.go b/repository_test.go
index 7a9db15..468ce33 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -2953,6 +2953,15 @@ func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) {
c.Assert(err, NotNil)
}
+func (s *RepositorySuite) TestIssue674(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+ h, err := r.ResolveRevision(plumbing.Revision(""))
+
+ c.Assert(err, NotNil)
+ c.Assert(h, NotNil)
+ c.Check(h.IsZero(), Equals, true)
+}
+
func BenchmarkObjects(b *testing.B) {
defer fixtures.Clean()