| 1 | package git |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // Remote struct is simply a collection of remote branches and wraps it with the |
| 6 | // name of the remote and fetch/push urls. It also holds the *selected* remote |
| 7 | // branch |
| 8 | type Remote struct { |
| 9 | Name string |
| 10 | URL []string |
| 11 | RefSpecs []string |
| 12 | Branches []*RemoteBranch |
| 13 | } |
| 14 | |
| 15 | // search for remotes in go-git way. It is the short way to get remotes but it |
| 16 | // does not give any insight about remote branches |
| 17 | func (r *Repository) initRemotes() error { |
| 18 | rp := r.Repo |
| 19 | r.Remotes = make([]*Remote, 0) |
| 20 | |
| 21 | rms, err := rp.Remotes() |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | for _, rm := range rms { |
| 26 | rfs := make([]string, 0) |
| 27 | for _, rf := range rm.Config().Fetch { |
| 28 | rfs = append(rfs, string(rf)) |
| 29 | } |
| 30 | remote := &Remote{ |
| 31 | Name: rm.Config().Name, |
| 32 | URL: rm.Config().URLs, |
| 33 | RefSpecs: rfs, |
| 34 | } |
| 35 | if err := remote.loadRemoteBranches(r); err != nil { |
| 36 | continue |
| 37 | } |
| 38 | r.Remotes = append(r.Remotes, remote) |
| 39 | } |
| 40 | |
| 41 | if len(r.Remotes) <= 0 { |
| 42 | return fmt.Errorf("no remote for repository: %s", r.Name) |
| 43 | } |
| 44 | r.State.Remote = r.Remotes[0] |
| 45 | return err |
| 46 | } |