-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
local_repository.go
447 lines (402 loc) · 10.2 KB
/
local_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"github.com/Songmu/gitconfig"
"github.com/saracen/walker"
"github.com/x-motemen/ghq/logger"
)
const envGhqRoot = "GHQ_ROOT"
// LocalRepository represents local repository
type LocalRepository struct {
FullPath string
RelPath string
RootPath string
PathParts []string
repoPath string
vcsBackend *VCSBackend
}
// RepoPath returns local repository path
func (repo *LocalRepository) RepoPath() string {
if repo.repoPath != "" {
return repo.repoPath
}
return repo.FullPath
}
// LocalRepositoryFromFullPath resolve LocalRepository from file path
func LocalRepositoryFromFullPath(fullPath string, backend *VCSBackend) (*LocalRepository, error) {
var relPath string
roots, err := localRepositoryRoots(true)
if err != nil {
return nil, err
}
var root string
for _, root = range roots {
if !strings.HasPrefix(fullPath, root) {
continue
}
var err error
relPath, err = filepath.Rel(root, fullPath)
if err == nil {
break
}
}
if relPath == "" {
return nil, fmt.Errorf("no local repository found for: %s", fullPath)
}
pathParts := strings.Split(relPath, string(filepath.Separator))
return &LocalRepository{
FullPath: fullPath,
RelPath: filepath.ToSlash(relPath),
RootPath: root,
PathParts: pathParts,
vcsBackend: backend,
}, nil
}
// LocalRepositoryFromURL resolve LocalRepository from URL
func LocalRepositoryFromURL(remoteURL *url.URL, bare bool) (*LocalRepository, error) {
pathParts := append(
[]string{remoteURL.Hostname()}, strings.Split(remoteURL.Path, "/")...,
)
relPath := strings.TrimSuffix(filepath.Join(pathParts...), ".git")
pathParts[len(pathParts)-1] = strings.TrimSuffix(pathParts[len(pathParts)-1], ".git")
if bare {
// Force to append ".git" even if remoteURL does not end with ".git".
relPath = relPath + ".git"
pathParts[len(pathParts)-1] = pathParts[len(pathParts)-1] + ".git"
}
var (
localRepository *LocalRepository
mu sync.Mutex
)
// Find existing local repository first
if err := walkAllLocalRepositories(func(repo *LocalRepository) {
if repo.RelPath == relPath {
mu.Lock()
localRepository = repo
mu.Unlock()
}
}); err != nil {
return nil, err
}
if localRepository != nil {
return localRepository, nil
}
var remoteURLStr = remoteURL.String()
if remoteURL.Scheme == "codecommit" {
remoteURLStr = remoteURL.Opaque
}
prim, err := getRoot(remoteURLStr)
if err != nil {
return nil, err
}
// No local repository found, returning new one
return &LocalRepository{
FullPath: filepath.Join(prim, relPath),
RelPath: relPath,
RootPath: prim,
PathParts: pathParts,
}, nil
}
func getRoot(u string) (string, error) {
prim := os.Getenv(envGhqRoot)
if prim != "" {
return prim, nil
}
var err error
if !codecommitLikeURLPattern.MatchString(u) {
prim, err = gitconfig.Do("--path", "--get-urlmatch", "ghq.root", u)
if err != nil && !gitconfig.IsNotFound(err) {
return "", err
}
}
if prim == "" {
prim, err = primaryLocalRepositoryRoot()
if err != nil {
return "", err
}
}
return prim, nil
}
// Subpaths returns lists of tail parts of relative path from the root directory (shortest first)
// for example, {"ghq", "motemen/ghq", "github.com/motemen/ghq"} for $root/github.com/motemen/ghq.
func (repo *LocalRepository) Subpaths() []string {
tails := make([]string, len(repo.PathParts))
for i := range repo.PathParts {
tails[i] = strings.Join(repo.PathParts[len(repo.PathParts)-(i+1):], "/")
}
return tails
}
// NonHostPath returns non host path
func (repo *LocalRepository) NonHostPath() string {
return strings.Join(repo.PathParts[1:], "/")
}
// list as bellow
// - "$GHQ_ROOT/github.com/motemen/ghq/cmdutil" // repo.FullPath
// - "$GHQ_ROOT/github.com/motemen/ghq"
// - "$GHQ_ROOT/github.com/motemen
func (repo *LocalRepository) repoRootCandidates() []string {
hostRoot := filepath.Join(repo.RootPath, repo.PathParts[0])
nonHostParts := repo.PathParts[1:]
candidates := make([]string, len(nonHostParts))
for i := 0; i < len(nonHostParts); i++ {
candidates[i] = filepath.Join(append(
[]string{hostRoot}, nonHostParts[0:len(nonHostParts)-i]...)...)
}
return candidates
}
// IsUnderPrimaryRoot or not
func (repo *LocalRepository) IsUnderPrimaryRoot() bool {
prim, err := primaryLocalRepositoryRoot()
if err != nil {
return false
}
return strings.HasPrefix(repo.FullPath, prim)
}
// Matches checks if any subpath of the local repository equals the query.
func (repo *LocalRepository) Matches(pathQuery string) bool {
for _, p := range repo.Subpaths() {
if p == pathQuery {
return true
}
}
return false
}
// VCS returns VCSBackend of the repository
func (repo *LocalRepository) VCS() (*VCSBackend, string) {
if repo.vcsBackend == nil {
for _, dir := range repo.repoRootCandidates() {
backend := findVCSBackend(dir, "")
if backend != nil {
repo.vcsBackend = backend
repo.repoPath = dir
break
}
}
}
return repo.vcsBackend, repo.RepoPath()
}
var vcsContentsMap = map[string]*VCSBackend{
".git": GitBackend,
".hg": MercurialBackend,
".svn": SubversionBackend,
"_darcs": DarcsBackend,
".pijul": PijulBackend,
".bzr": BazaarBackend,
".fslckout": FossilBackend, // file
"_FOSSIL_": FossilBackend, // file
"CVS/Repository": cvsDummyBackend,
}
var vcsContents = [...]string{
".git",
".hg",
".svn",
"_darcs",
".pijul",
".bzr",
".fslckout",
"._FOSSIL_",
"CVS/Repository",
}
func findVCSBackend(fpath, vcs string) *VCSBackend {
// When vcs is not empty, search only specified contents of vcs
if vcs != "" {
vcsBackend, ok := vcsRegistry[vcs]
if !ok {
return nil
}
if vcsBackend == GitBackend && strings.HasSuffix(fpath, ".git") {
return vcsBackend
}
for _, d := range vcsBackend.Contents {
if _, err := os.Stat(filepath.Join(fpath, d)); err == nil {
return vcsBackend
}
}
return nil
}
if strings.HasSuffix(fpath, ".git") {
return GitBackend
}
for _, d := range vcsContents {
if _, err := os.Stat(filepath.Join(fpath, d)); err == nil {
return vcsContentsMap[d]
}
}
return nil
}
func walkAllLocalRepositories(callback func(*LocalRepository)) error {
return walkLocalRepositories("", callback)
}
func walkLocalRepositories(vcs string, callback func(*LocalRepository)) error {
roots, err := localRepositoryRoots(true)
if err != nil {
return err
}
walkFn := func(fpath string, fi os.FileInfo) error {
isSymlink := false
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
isSymlink = true
realpath, err := filepath.EvalSymlinks(fpath)
if err != nil {
return nil
}
fi, err = os.Stat(realpath)
if err != nil {
return nil
}
}
if !fi.IsDir() {
return nil
}
vcsBackend := findVCSBackend(fpath, vcs)
if vcsBackend == nil {
return nil
}
repo, err := LocalRepositoryFromFullPath(fpath, vcsBackend)
if err != nil || repo == nil {
return nil
}
callback(repo)
if isSymlink {
return nil
}
return filepath.SkipDir
}
errCb := walker.WithErrorCallback(func(pathname string, err error) error {
if os.IsPermission(errors.Unwrap(err)) {
logger.Log("warning", fmt.Sprintf("%s: Permission denied", pathname))
return nil
}
return err
})
for _, root := range roots {
fi, err := os.Stat(root)
if err != nil {
if os.IsNotExist(err) {
continue
}
}
if fi.Mode()&0444 == 0 {
logger.Log("warning", fmt.Sprintf("%s: Permission denied", root))
continue
}
if err := walker.Walk(root, walkFn, errCb); err != nil {
return err
}
}
return nil
}
var (
_home string
_homeErr error
homeOnce = &sync.Once{}
)
func getHome() (string, error) {
homeOnce.Do(func() {
_home, _homeErr = os.UserHomeDir()
})
return _home, _homeErr
}
var (
_localRepositoryRoots []string
_localRepoErr error
localRepoOnce = &sync.Once{}
)
// localRepositoryRoots returns locally cloned repositories' root directories.
// The root dirs are determined as following:
//
// - If GHQ_ROOT environment variable is nonempty, use it as the only root dir.
// - Otherwise, use the result of `git config --get-all ghq.root` as the dirs.
// - Otherwise, fallback to the default root, `~/.ghq`.
// - When GHQ_ROOT is empty, specific root dirs are added from the result of
// `git config --path --get-regexp '^ghq\..+\.root$`
func localRepositoryRoots(all bool) ([]string, error) {
localRepoOnce.Do(func() {
var roots []string
envRoot := os.Getenv(envGhqRoot)
if envRoot != "" {
roots = filepath.SplitList(envRoot)
} else {
var err error
roots, err = gitconfig.PathAll("ghq.root")
if err != nil && !gitconfig.IsNotFound(err) {
_localRepoErr = err
return
}
// reverse slice
for i := len(roots)/2 - 1; i >= 0; i-- {
opp := len(roots) - 1 - i
roots[i], roots[opp] =
roots[opp], roots[i]
}
}
if len(roots) == 0 {
homeDir, err := getHome()
if err != nil {
_localRepoErr = err
return
}
roots = []string{filepath.Join(homeDir, "ghq")}
}
if all && envRoot == "" {
localRoots, err := urlMatchLocalRepositoryRoots()
if err != nil {
_localRepoErr = err
return
}
roots = append(roots, localRoots...)
}
seen := make(map[string]bool, len(roots))
for _, v := range roots {
path := filepath.Clean(v)
if _, err := os.Stat(path); err == nil {
if path, err = evalSymlinks(path); err != nil {
_localRepoErr = err
return
}
}
if !filepath.IsAbs(path) {
var err error
if path, err = filepath.Abs(path); err != nil {
_localRepoErr = err
return
}
}
if seen[path] {
continue
}
seen[path] = true
_localRepositoryRoots = append(_localRepositoryRoots, path)
}
})
return _localRepositoryRoots, _localRepoErr
}
func urlMatchLocalRepositoryRoots() ([]string, error) {
out, err := gitconfig.Do("--path", "--get-regexp", `^ghq\..+\.root$`)
if err != nil {
if gitconfig.IsNotFound(err) {
return nil, nil
}
return nil, err
}
items := strings.Split(out, "\x00")
ret := make([]string, len(items))
for i, kvStr := range items {
kv := strings.SplitN(kvStr, "\n", 2)
ret[i] = kv[1]
}
return ret, nil
}
func primaryLocalRepositoryRoot() (string, error) {
roots, err := localRepositoryRoots(false)
if err != nil {
return "", err
}
return roots[0], nil
}