| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/go-git/go-git/v5/plumbing" |
| 7 | "github.com/go-git/go-git/v5/plumbing/storer" |
| 8 | ) |
| 9 | |
| 10 | // RemoteBranch is the wrapper of go-git's Reference struct. In addition to |
| 11 | // that, it also holds name of the remote branch |
| 12 | type RemoteBranch struct { |
| 13 | Name string |
| 14 | Reference *plumbing.Reference |
| 15 | } |
| 16 | |
| 17 | // search for the remote branches of the remote. It takes the go-git's repo |
| 18 | // pointer in order to get storer struct |
| 19 | func (rm *Remote) loadRemoteBranches(r *Repository) error { |
| 20 | rm.Branches = make([]*RemoteBranch, 0) |
| 21 | bs, err := remoteBranchesIter(r.Repo.Storer) |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | defer bs.Close() |
| 26 | err = bs.ForEach(func(b *plumbing.Reference) error { |
| 27 | if strings.Split(b.Name().Short(), "/")[0] == rm.Name { |
| 28 | rm.Branches = append(rm.Branches, &RemoteBranch{ |
| 29 | Name: b.Name().Short(), |
| 30 | Reference: b, |
| 31 | }) |
| 32 | } |
| 33 | return nil |
| 34 | }) |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // create an iterator for the references. it checks if the reference is a hash |
| 39 | // reference |
| 40 | func remoteBranchesIter(s storer.ReferenceStorer) (storer.ReferenceIter, error) { |
| 41 | refs, err := s.IterReferences() |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | return storer.NewReferenceFilteredIter(func(ref *plumbing.Reference) bool { |
| 47 | if ref.Type() == plumbing.HashReference { |
| 48 | return ref.Name().IsRemote() |
| 49 | } |
| 50 | return false |
| 51 | }, refs), nil |
| 52 | } |