aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorAnthony HAMON <antham@users.noreply.github.com>2017-12-01 16:27:52 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2017-12-01 16:27:52 +0100
commit44c364fe3b7b8cdc0f9623afe870d6781a97ebb4 (patch)
tree03284e910c75da7c896381b439c04498eabe63db /repository.go
parente20d3347d26f0b7193502e2ad7386d7c504b0cde (diff)
downloadgo-git-44c364fe3b7b8cdc0f9623afe870d6781a97ebb4.tar.gz
Fix revision solver for branch and tag (#660)
fix Repository.ResolveRevision for branch and tag
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go43
1 files changed, 15 insertions, 28 deletions
diff --git a/repository.go b/repository.go
index 7cdc0d5..8a966c6 100644
--- a/repository.go
+++ b/repository.go
@@ -891,6 +891,9 @@ func (r *Repository) Worktree() (*Worktree, error) {
}
// ResolveRevision resolves revision to corresponding hash.
+//
+// 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})
func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, error) {
p := revision.NewParserFromString(string(rev))
@@ -905,15 +908,22 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err
for _, item := range items {
switch item.(type) {
case revision.Ref:
- ref, err := storer.ResolveReference(r.Storer, plumbing.ReferenceName(item.(revision.Ref)))
+ revisionRef := item.(revision.Ref)
+ var ref *plumbing.Reference
- if err != nil {
- return &plumbing.ZeroHash, err
+ for _, rule := range append([]string{"%s"}, plumbing.RefRevParseRules...) {
+ ref, err = storer.ResolveReference(r.Storer, plumbing.ReferenceName(fmt.Sprintf(rule, revisionRef)))
+
+ if err == nil {
+ break
+ }
}
- h := ref.Hash()
+ if ref == nil {
+ return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound
+ }
- commit, err = r.CommitObject(h)
+ commit, err = r.CommitObject(ref.Hash())
if err != nil {
return &plumbing.ZeroHash, err
@@ -986,29 +996,6 @@ func (r *Repository) ResolveRevision(rev plumbing.Revision) (*plumbing.Hash, err
}
commit = c
- case revision.AtDate:
- history := object.NewCommitPreorderIter(commit, nil, nil)
-
- date := item.(revision.AtDate).Date
-
- var c *object.Commit
- err := history.ForEach(func(hc *object.Commit) error {
- if date.Equal(hc.Committer.When.UTC()) || hc.Committer.When.UTC().Before(date) {
- c = hc
- return storer.ErrStop
- }
-
- return nil
- })
- if err != nil {
- return &plumbing.ZeroHash, err
- }
-
- if c == nil {
- return &plumbing.ZeroHash, fmt.Errorf(`No commit exists prior to date "%s"`, date.String())
- }
-
- commit = c
}
}