/
list.go
101 lines (90 loc) · 2.3 KB
/
list.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
package main
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
func doList(c *cli.Context) error {
var (
w = c.App.Writer
query = c.Args().First()
exact = c.Bool("exact")
vcsBackend = c.String("vcs")
printFullPaths = c.Bool("full-path")
printUniquePaths = c.Bool("unique")
)
filterByQuery := func(_ *LocalRepository) bool {
return true
}
if query != "" {
if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) {
if url, err := newURL(query); err == nil {
if repo, err := LocalRepositoryFromURL(url); err == nil {
query = repo.RelPath
}
}
}
if exact {
filterByQuery = func(repo *LocalRepository) bool {
return repo.Matches(query)
}
} else {
var host string
paths := strings.Split(query, "/")
if len(paths) > 1 && looksLikeAuthorityPattern.MatchString(paths[0]) {
query = strings.Join(paths[1:], "/")
host = paths[0]
}
filterByQuery = func(repo *LocalRepository) bool {
return strings.Contains(repo.NonHostPath(), query) &&
(host == "" || repo.PathParts[0] == host)
}
}
}
filterByVCS := func(repo *LocalRepository) bool {
return vcsBackend == "" ||
vcsRegistry[vcsBackend] == repo.VCS()
}
repos := []*LocalRepository{}
if err := walkLocalRepositories(func(repo *LocalRepository) {
if !filterByQuery(repo) || !filterByVCS(repo) {
return
}
repos = append(repos, repo)
}); err != nil {
return err
}
if printUniquePaths {
subpathCount := map[string]int{} // Count duplicated subpaths (ex. foo/dotfiles and bar/dotfiles)
reposCount := map[string]int{} // Check duplicated repositories among roots
// Primary first
for _, repo := range repos {
if reposCount[repo.RelPath] == 0 {
for _, p := range repo.Subpaths() {
subpathCount[p] = subpathCount[p] + 1
}
}
reposCount[repo.RelPath] = reposCount[repo.RelPath] + 1
}
for _, repo := range repos {
if reposCount[repo.RelPath] > 1 && repo.IsUnderPrimaryRoot() == false {
continue
}
for _, p := range repo.Subpaths() {
if subpathCount[p] == 1 {
fmt.Fprintln(w, p)
break
}
}
}
} else {
for _, repo := range repos {
if printFullPaths {
fmt.Fprintln(w, repo.FullPath)
} else {
fmt.Fprintln(w, repo.RelPath)
}
}
}
return nil
}