@@ -266,11 +266,12 @@ func (d *DotGit) SetRef(r *plumbing.Reference) error {
266266// Symbolic references are resolved and included in the output.
267267func (d * DotGit ) Refs () ([]* plumbing.Reference , error ) {
268268 var refs []* plumbing.Reference
269- if err := d .addRefsFromPackedRefs (& refs ); err != nil {
269+ var seen = make (map [plumbing.ReferenceName ]bool )
270+ if err := d .addRefsFromRefDir (& refs , seen ); err != nil {
270271 return nil , err
271272 }
272273
273- if err := d .addRefsFromRefDir (& refs ); err != nil {
274+ if err := d .addRefsFromPackedRefs (& refs , seen ); err != nil {
274275 return nil , err
275276 }
276277
@@ -359,13 +360,16 @@ func (d *DotGit) RemoveRef(name plumbing.ReferenceName) error {
359360 return d .rewritePackedRefsWithoutRef (name )
360361}
361362
362- func (d * DotGit ) addRefsFromPackedRefs (refs * []* plumbing.Reference ) (err error ) {
363+ func (d * DotGit ) addRefsFromPackedRefs (refs * []* plumbing.Reference , seen map [plumbing. ReferenceName ] bool ) (err error ) {
363364 if err := d .syncPackedRefs (); err != nil {
364365 return err
365366 }
366367
367- for _ , ref := range d .cachedPackedRefs {
368- * refs = append (* refs , ref )
368+ for name , ref := range d .cachedPackedRefs {
369+ if ! seen [name ] {
370+ * refs = append (* refs , ref )
371+ seen [name ] = true
372+ }
369373 }
370374
371375 return nil
@@ -448,11 +452,11 @@ func (d *DotGit) processLine(line string) (*plumbing.Reference, error) {
448452 }
449453}
450454
451- func (d * DotGit ) addRefsFromRefDir (refs * []* plumbing.Reference ) error {
452- return d .walkReferencesTree (refs , []string {refsPath })
455+ func (d * DotGit ) addRefsFromRefDir (refs * []* plumbing.Reference , seen map [plumbing. ReferenceName ] bool ) error {
456+ return d .walkReferencesTree (refs , []string {refsPath }, seen )
453457}
454458
455- func (d * DotGit ) walkReferencesTree (refs * []* plumbing.Reference , relPath []string ) error {
459+ func (d * DotGit ) walkReferencesTree (refs * []* plumbing.Reference , relPath []string , seen map [plumbing. ReferenceName ] bool ) error {
456460 files , err := d .fs .ReadDir (d .fs .Join (relPath ... ))
457461 if err != nil {
458462 if os .IsNotExist (err ) {
@@ -465,7 +469,7 @@ func (d *DotGit) walkReferencesTree(refs *[]*plumbing.Reference, relPath []strin
465469 for _ , f := range files {
466470 newRelPath := append (append ([]string (nil ), relPath ... ), f .Name ())
467471 if f .IsDir () {
468- if err = d .walkReferencesTree (refs , newRelPath ); err != nil {
472+ if err = d .walkReferencesTree (refs , newRelPath , seen ); err != nil {
469473 return err
470474 }
471475
@@ -477,8 +481,9 @@ func (d *DotGit) walkReferencesTree(refs *[]*plumbing.Reference, relPath []strin
477481 return err
478482 }
479483
480- if ref != nil {
484+ if ref != nil && ! seen [ ref . Name ()] {
481485 * refs = append (* refs , ref )
486+ seen [ref .Name ()] = true
482487 }
483488 }
484489
0 commit comments