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

Commit 662e2c2

Browse files
authored
Merge pull request #870 from mcuadros/fetch-error
Remote.Fetch: error on missing remote reference
2 parents b11eaab + b53ba8d commit 662e2c2

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

remote.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,29 @@ func calculateRefs(
630630
spec = append(spec, refspecTag)
631631
}
632632

633+
refs := make(memory.ReferenceStorage)
634+
for _, s := range spec {
635+
if err := doCalculateRefs(s, remoteRefs, refs); err != nil {
636+
return nil, err
637+
}
638+
}
639+
640+
return refs, nil
641+
}
642+
643+
func doCalculateRefs(
644+
s config.RefSpec,
645+
remoteRefs storer.ReferenceStorer,
646+
refs memory.ReferenceStorage,
647+
) error {
633648
iter, err := remoteRefs.IterReferences()
634649
if err != nil {
635-
return nil, err
650+
return err
636651
}
637652

638-
refs := make(memory.ReferenceStorage)
639-
return refs, iter.ForEach(func(ref *plumbing.Reference) error {
640-
if !config.MatchAny(spec, ref.Name()) {
653+
var matched bool
654+
err = iter.ForEach(func(ref *plumbing.Reference) error {
655+
if !s.Match(ref.Name()) {
641656
return nil
642657
}
643658

@@ -654,8 +669,23 @@ func calculateRefs(
654669
return nil
655670
}
656671

657-
return refs.SetReference(ref)
672+
matched = true
673+
if err := refs.SetReference(ref); err != nil {
674+
return err
675+
}
676+
677+
if !s.IsWildcard() {
678+
return storer.ErrStop
679+
}
680+
681+
return nil
658682
})
683+
684+
if !matched && !s.IsWildcard() {
685+
return fmt.Errorf("couldn't find remote ref %q", s.Src())
686+
}
687+
688+
return err
659689
}
660690

661691
func getWants(localStorer storage.Storer, refs memory.ReferenceStorage) ([]plumbing.Hash, error) {

remote_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ func (s *RemoteSuite) TestFetch(c *C) {
100100
})
101101
}
102102

103+
func (s *RemoteSuite) TestFetchNonExistantReference(c *C) {
104+
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
105+
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},
106+
})
107+
108+
err := r.Fetch(&FetchOptions{
109+
RefSpecs: []config.RefSpec{
110+
config.RefSpec("+refs/heads/foo:refs/remotes/origin/foo"),
111+
},
112+
})
113+
114+
c.Assert(err, ErrorMatches, "couldn't find remote ref.*")
115+
}
116+
103117
func (s *RemoteSuite) TestFetchContext(c *C) {
104118
r := newRemote(memory.NewStorage(), &config.RemoteConfig{
105119
URLs: []string{s.GetLocalRepositoryURL(fixtures.ByTag("tags").One())},

0 commit comments

Comments
 (0)