aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorArieh Schneier <15041913+AriehSchneier@users.noreply.github.com>2023-05-25 08:56:17 +1000
committerArieh Schneier <15041913+AriehSchneier@users.noreply.github.com>2023-05-25 08:56:17 +1000
commit65a5c716353f81b20c70f4a2c6560590d9472b6e (patch)
treeb5a0c837eb3afe02cb8f24d85846c6464616f655 /repository.go
parentb98b813a17d32f4fa29a3ef2e9f4c38c5a97b440 (diff)
downloadgo-git-65a5c716353f81b20c70f4a2c6560590d9472b6e.tar.gz
git: enable fetch with unqualified references
Signed-off-by: Arieh Schneier <15041913+AriehSchneier@users.noreply.github.com>
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go43
1 files changed, 22 insertions, 21 deletions
diff --git a/repository.go b/repository.go
index f3540c6..17fa5dd 100644
--- a/repository.go
+++ b/repository.go
@@ -1029,21 +1029,9 @@ func (r *Repository) fetchAndUpdateReferences(
return nil, err
}
- var resolvedRef *plumbing.Reference
- // return error from checking the raw ref passed in
- var rawRefError error
- for _, rule := range append([]string{"%s"}, plumbing.RefRevParseRules...) {
- resolvedRef, err = storer.ResolveReference(remoteRefs, plumbing.ReferenceName(fmt.Sprintf(rule, ref)))
-
- if err == nil {
- break
- } else if rawRefError == nil {
- rawRefError = err
- }
- }
-
+ resolvedRef, err := expand_ref(remoteRefs, ref)
if err != nil {
- return nil, rawRefError
+ return nil, err
}
refsUpdated, err := r.updateReferences(remote.c.Fetch, resolvedRef)
@@ -1489,6 +1477,23 @@ func (r *Repository) Worktree() (*Worktree, error) {
return &Worktree{r: r, Filesystem: r.wt}, nil
}
+func expand_ref(s storer.ReferenceStorer, ref plumbing.ReferenceName) (*plumbing.Reference, error) {
+ // For improving troubleshooting, this preserves the error for the provided `ref`,
+ // and returns the error for that specific ref in case all parse rules fails.
+ var ret error
+ for _, rule := range plumbing.RefRevParseRules {
+ resolvedRef, err := storer.ResolveReference(s, plumbing.ReferenceName(fmt.Sprintf(rule, ref)))
+
+ if err == nil {
+ return resolvedRef, nil
+ } else if ret == nil {
+ ret = err
+ }
+ }
+
+ return nil, ret
+}
+
// ResolveRevision resolves revision to corresponding hash. It will always
// resolve to a commit hash, not a tree or annotated tag.
//
@@ -1518,13 +1523,9 @@ func (r *Repository) ResolveRevision(in plumbing.Revision) (*plumbing.Hash, erro
tryHashes = append(tryHashes, r.resolveHashPrefix(string(revisionRef))...)
- for _, rule := range append([]string{"%s"}, plumbing.RefRevParseRules...) {
- ref, err := storer.ResolveReference(r.Storer, plumbing.ReferenceName(fmt.Sprintf(rule, revisionRef)))
-
- if err == nil {
- tryHashes = append(tryHashes, ref.Hash())
- break
- }
+ ref, err := expand_ref(r.Storer, plumbing.ReferenceName(revisionRef))
+ if err == nil {
+ tryHashes = append(tryHashes, ref.Hash())
}
// in ambiguous cases, `git rev-parse` will emit a warning, but