This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
111 lines (98 loc) · 2.59 KB
/
remote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package context
import (
"fmt"
"net/url"
"strings"
"github.com/botwayorg/gh/core/ghrepo"
"github.com/botwayorg/gh/git"
)
// Remotes represents a set of git remotes
type Remotes []*Remote
// FindByName returns the first Remote whose name matches the list
func (r Remotes) FindByName(names ...string) (*Remote, error) {
for _, name := range names {
for _, rem := range r {
if rem.Name == name || name == "*" {
return rem, nil
}
}
}
return nil, fmt.Errorf("no GitHub remotes found")
}
// FindByRepo returns the first Remote that points to a specific GitHub repository
func (r Remotes) FindByRepo(owner, name string) (*Remote, error) {
for _, rem := range r {
if strings.EqualFold(rem.RepoOwner(), owner) && strings.EqualFold(rem.RepoName(), name) {
return rem, nil
}
}
return nil, fmt.Errorf("no matching remote found")
}
func remoteNameSortScore(name string) int {
switch strings.ToLower(name) {
case "upstream":
return 3
case "github":
return 2
case "origin":
return 1
default:
return 0
}
}
// https://golang.org/pkg/sort/#Interface
func (r Remotes) Len() int { return len(r) }
func (r Remotes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Remotes) Less(i, j int) bool {
return remoteNameSortScore(r[i].Name) > remoteNameSortScore(r[j].Name)
}
// Filter remotes by given hostnames, maintains original order
func (r Remotes) FilterByHosts(hosts []string) Remotes {
filtered := make(Remotes, 0)
for _, rr := range r {
for _, host := range hosts {
if strings.EqualFold(rr.RepoHost(), host) {
filtered = append(filtered, rr)
break
}
}
}
return filtered
}
// Remote represents a git remote mapped to a GitHub repository
type Remote struct {
*git.Remote
Repo ghrepo.Interface
}
// RepoName is the name of the GitHub repository
func (r Remote) RepoName() string {
return r.Repo.RepoName()
}
// RepoOwner is the name of the GitHub account that owns the repo
func (r Remote) RepoOwner() string {
return r.Repo.RepoOwner()
}
// RepoHost is the GitHub hostname that the remote points to
func (r Remote) RepoHost() string {
return r.Repo.RepoHost()
}
// TODO: accept an interface instead of git.RemoteSet
func TranslateRemotes(gitRemotes git.RemoteSet, urlTranslate func(*url.URL) *url.URL) (remotes Remotes) {
for _, r := range gitRemotes {
var repo ghrepo.Interface
if r.FetchURL != nil {
repo, _ = ghrepo.FromURL(urlTranslate(r.FetchURL))
}
if r.PushURL != nil && repo == nil {
repo, _ = ghrepo.FromURL(urlTranslate(r.PushURL))
}
if repo == nil {
continue
}
remotes = append(remotes, &Remote{
Remote: r,
Repo: repo,
})
}
return
}