forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_repository.go
216 lines (170 loc) · 4.47 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
package main
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/motemen/ghq/utils"
)
// A RemoteRepository represents a remote repository.
type RemoteRepository interface {
// The repository URL.
URL() *url.URL
// Checks if the URL is valid.
IsValid() bool
// The VCS backend that hosts the repository.
VCS() *VCSBackend
}
// A GitHubRepository represents a GitHub repository. Impliments RemoteRepository.
type GitHubRepository struct {
url *url.URL
}
func (repo *GitHubRepository) URL() *url.URL {
return repo.url
}
func (repo *GitHubRepository) IsValid() bool {
if strings.HasPrefix(repo.url.Path, "/blog/") {
return false
}
// must be /{user}/{project}/?
pathComponents := strings.Split(strings.TrimRight(repo.url.Path, "/"), "/")
if len(pathComponents) != 3 {
return false
}
return true
}
func (repo *GitHubRepository) VCS() *VCSBackend {
return GitBackend
}
// A GitHubGistRepository represents a GitHub Gist repository.
type GitHubGistRepository struct {
url *url.URL
}
func (repo *GitHubGistRepository) URL() *url.URL {
return repo.url
}
func (repo *GitHubGistRepository) IsValid() bool {
return true
}
func (repo *GitHubGistRepository) VCS() *VCSBackend {
return GitBackend
}
type GoogleCodeRepository struct {
url *url.URL
}
func (repo *GoogleCodeRepository) URL() *url.URL {
return repo.url
}
var validGoogleCodePathPattern = regexp.MustCompile(`^/p/[^/]+/?$`)
func (repo *GoogleCodeRepository) IsValid() bool {
return validGoogleCodePathPattern.MatchString(repo.url.Path)
}
func (repo *GoogleCodeRepository) VCS() *VCSBackend {
if utils.RunSilently("hg", "identify", repo.url.String()) == nil {
return MercurialBackend
} else if utils.RunSilently("git", "ls-remote", repo.url.String()) == nil {
return GitBackend
} else {
return nil
}
}
type DarksHubRepository struct {
url *url.URL
}
func (repo *DarksHubRepository) URL() *url.URL {
return repo.url
}
func (repo *DarksHubRepository) IsValid() bool {
return strings.Count(repo.url.Path, "/") == 2
}
func (repo *DarksHubRepository) VCS() *VCSBackend {
return DarcsBackend
}
type BluemixRepository struct {
url *url.URL
}
func (repo *BluemixRepository) URL() *url.URL {
return repo.url
}
var validBluemixPathPattern = regexp.MustCompile(`^/git/[^/]+/[^/]+$`)
func (repo *BluemixRepository) IsValid() bool {
return validBluemixPathPattern.MatchString(repo.url.Path)
}
func (repo *BluemixRepository) VCS() *VCSBackend {
return GitBackend
}
type OtherRepository struct {
url *url.URL
}
func (repo *OtherRepository) URL() *url.URL {
return repo.url
}
func (repo *OtherRepository) IsValid() bool {
return true
}
func (repo *OtherRepository) VCS() *VCSBackend {
if GitHasFeatureConfigURLMatch() {
// Respect 'ghq.url.https://ghe.example.com/.vcs' config variable
// (in gitconfig:)
// [ghq "https://ghe.example.com/"]
// vcs = github
vcs, err := GitConfig("--get-urlmatch", "ghq.vcs", repo.URL().String())
if err != nil {
utils.Log("error", err.Error())
}
if vcs == "git" || vcs == "github" {
return GitBackend
}
if vcs == "svn" || vcs == "subversion" {
return SubversionBackend
}
if vcs == "git-svn" {
return GitsvnBackend
}
if vcs == "hg" || vcs == "mercurial" {
return MercurialBackend
}
if vcs == "darcs" {
return DarcsBackend
}
} else {
utils.Log("warning", "This version of Git does not support `config --get-urlmatch`; per-URL settings are not available")
}
// Detect VCS backend automatically
if utils.RunSilently("git", "ls-remote", repo.url.String()) == nil {
return GitBackend
} else if utils.RunSilently("hg", "identify", repo.url.String()) == nil {
return MercurialBackend
} else if utils.RunSilently("svn", "info", repo.url.String()) == nil {
return SubversionBackend
} else {
return nil
}
}
func NewRemoteRepository(url *url.URL) (RemoteRepository, error) {
if url.Host == "github.com" {
return &GitHubRepository{url}, nil
}
if url.Host == "gist.github.com" {
return &GitHubGistRepository{url}, nil
}
if url.Host == "code.google.com" {
return &GoogleCodeRepository{url}, nil
}
if url.Host == "hub.darcs.net" {
return &DarksHubRepository{url}, nil
}
if url.Host == "hub.jazz.net" {
return &BluemixRepository{url}, nil
}
gheHosts, err := GitConfigAll("ghq.ghe.host")
if err != nil {
return nil, fmt.Errorf("failed to retrieve GH:E hostname from .gitconfig: %s", err)
}
for _, host := range gheHosts {
if url.Host == host {
return &GitHubRepository{url}, nil
}
}
return &OtherRepository{url}, nil
}