-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
remote_repository.go
230 lines (194 loc) · 6.13 KB
/
remote_repository.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/Songmu/gitconfig"
"github.com/x-motemen/ghq/cmdutil"
"github.com/x-motemen/ghq/logger"
)
// A RemoteRepository represents a remote repository.
type RemoteRepository interface {
// URL returns the repository URL.
URL() *url.URL
// IsValid checks if the URL is valid.
IsValid() bool
// VCS returns the VCS backend that hosts the repository.
VCS() (*VCSBackend, *url.URL, error)
}
// A GitHubRepository represents a GitHub repository. Implements RemoteRepository.
type GitHubRepository struct {
url *url.URL
}
// URL returns URL of the repository
func (repo *GitHubRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the repository is valid or not
func (repo *GitHubRepository) IsValid() bool {
if strings.HasPrefix(repo.url.Path, "/blog/") {
logger.Log("github", `the user or organization named "blog" is invalid on github, "https://github.com/blog" is redirected to "https://github.blog".`)
return false
}
pathComponents := strings.Split(strings.Trim(repo.url.Path, "/"), "/")
return len(pathComponents) >= 2
}
// VCS returns VCSBackend of the repository
func (repo *GitHubRepository) VCS() (*VCSBackend, *url.URL, error) {
u := *repo.url
pathComponents := strings.Split(strings.Trim(strings.TrimSuffix(u.Path, ".git"), "/"), "/")
path := "/" + strings.Join(pathComponents[0:2], "/")
if strings.HasSuffix(u.String(), ".git") {
path += ".git"
}
u.Path = path
return GitBackend, &u, nil
}
// A GitHubGistRepository represents a GitHub Gist repository.
type GitHubGistRepository struct {
url *url.URL
}
// URL returns URL of the GistRepository
func (repo *GitHubGistRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the gist repository is valid or not
func (repo *GitHubGistRepository) IsValid() bool {
return true
}
// VCS returns VCSBackend of the gist
func (repo *GitHubGistRepository) VCS() (*VCSBackend, *url.URL, error) {
return GitBackend, repo.URL(), nil
}
// DarksHubRepository represents DarcsHub Repository
type DarksHubRepository struct {
url *url.URL
}
// URL returns URL of darks repository
func (repo *DarksHubRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the DarcsHub repository is valid or not
func (repo *DarksHubRepository) IsValid() bool {
return strings.Count(repo.url.Path, "/") == 2
}
// VCS returns VCSBackend of the DarcsHub repository
func (repo *DarksHubRepository) VCS() (*VCSBackend, *url.URL, error) {
return DarcsBackend, repo.URL(), nil
}
// NestPijulRepository represents the Nest repository
type NestPijulRepository struct {
url *url.URL
}
// URL returns URL of the Nest repository
func (repo *NestPijulRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the Nest repository is valid or not
func (repo *NestPijulRepository) IsValid() bool {
return strings.Count(repo.url.Path, "/") == 2
}
// VCS returns VCSBackend of the Nest repository
func (repo *NestPijulRepository) VCS() (*VCSBackend, *url.URL, error) {
return PijulBackend, repo.URL(), nil
}
// A CodeCommitRepository represents a CodeCommit repository. Implements RemoteRepository.
type CodeCommitRepository struct {
url *url.URL
}
// URL returns URL of the repository
func (repo *CodeCommitRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the repository is valid or not
func (repo *CodeCommitRepository) IsValid() bool {
return true
}
// VCS returns VCSBackend of the repository
func (repo *CodeCommitRepository) VCS() (*VCSBackend, *url.URL, error) {
u := *repo.url
return GitBackend, &u, nil
}
// OtherRepository represents other repository
type OtherRepository struct {
url *url.URL
}
// URL returns URL of the repository
func (repo *OtherRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the repository is valid or not
func (repo *OtherRepository) IsValid() bool {
return true
}
var (
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr|codecommit)(?:\+|$)`)
scheme2vcs = map[string]*VCSBackend{
"git": GitBackend,
"codecommit": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
}
)
// VCS detects VCSBackend of the OtherRepository
func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL, error) {
// Respect 'ghq.url.https://ghe.example.com/.vcs' config variable
// (in gitconfig:)
// [ghq "https://ghe.example.com/"]
// vcs = github
vcs, err := gitconfig.Do("--path", "--get-urlmatch", "ghq.vcs", repo.URL().String())
if err != nil && !gitconfig.IsNotFound(err) {
logger.Log("error", err.Error())
}
if backend, ok := vcsRegistry[vcs]; ok {
return backend, repo.URL(), nil
}
if m := vcsSchemeReg.FindStringSubmatch(repo.url.Scheme); len(m) > 1 {
return scheme2vcs[m[1]], repo.URL(), nil
}
mayBeSvn := strings.HasPrefix(repo.url.Host, "svn.")
if mayBeSvn && cmdutil.RunSilently("svn", "info", repo.url.String()) == nil {
return SubversionBackend, repo.URL(), nil
}
// Detect VCS backend automatically
if cmdutil.RunSilently("git", "ls-remote", repo.url.String()) == nil {
return GitBackend, repo.URL(), nil
}
vcs, repoURL, err := detectGoImport(repo.url)
if err == nil {
// vcs == "mod" (modproxy) not supported yet
return vcsRegistry[vcs], repoURL, nil
}
if cmdutil.RunSilently("hg", "identify", repo.url.String()) == nil {
return MercurialBackend, repo.URL(), nil
}
if !mayBeSvn && cmdutil.RunSilently("svn", "info", repo.url.String()) == nil {
return SubversionBackend, repo.URL(), nil
}
return nil, nil, fmt.Errorf("unsupported VCS, url=%s: %w", repo.URL(), err)
}
// NewRemoteRepository returns new RemoteRepository object from URL
func NewRemoteRepository(u *url.URL) (RemoteRepository, error) {
repo := func() RemoteRepository {
if u.Scheme == "codecommit" {
return &CodeCommitRepository{u}
}
switch u.Host {
case "github.com":
return &GitHubRepository{u}
case "gist.github.com":
return &GitHubGistRepository{u}
case "hub.darcs.net":
return &DarksHubRepository{u}
case "nest.pijul.com":
return &NestPijulRepository{u}
default:
return &OtherRepository{u}
}
}()
if !repo.IsValid() {
return nil, fmt.Errorf("not a valid repository: %s", u)
}
return repo, nil
}