-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathremote_repository.go
159 lines (126 loc) · 3.39 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
package main
import (
"fmt"
"net/url"
"strings"
"github.com/motemen/ghq/cmdutil"
"github.com/motemen/ghq/logger"
)
// 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, *url.URL)
}
// 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, *url.URL) {
return GitBackend, repo.URL()
}
// 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, *url.URL) {
return GitBackend, repo.URL()
}
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, *url.URL) {
return DarcsBackend, repo.URL()
}
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, *url.URL) {
if err := gitHasFeatureConfigURLMatch(); err != nil {
logger.Log("warning", err.Error())
} else {
// 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 {
logger.Log("error", err.Error())
}
if backend, ok := vcsRegistry[vcs]; ok {
return backend, repo.URL()
}
}
// Detect VCS backend automatically
if cmdutil.RunSilently("git", "ls-remote", repo.url.String()) == nil {
return GitBackend, repo.URL()
}
vcs, repoURL, err := detectGoImport(repo.url)
if err == nil {
// vcs == "mod" (modproxy) not supported yet
return vcsRegistry[vcs], repoURL
}
if cmdutil.RunSilently("hg", "identify", repo.url.String()) == nil {
return MercurialBackend, repo.URL()
}
if cmdutil.RunSilently("svn", "info", repo.url.String()) == nil {
return SubversionBackend, repo.URL()
}
return nil, 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 == "hub.darcs.net" {
return &DarksHubRepository{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
}