-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
cmd_list.go
119 lines (108 loc) · 2.82 KB
/
cmd_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/urfave/cli/v2"
)
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")
bare = c.Bool("bare")
)
filterByQuery := func(_ *LocalRepository) bool {
return true
}
if query != "" {
if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) {
if url, err := newURL(query, false, false); err == nil {
if repo, err := LocalRepositoryFromURL(url, bare); err == nil {
query = filepath.ToSlash(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]
}
// Using smartcase searching
if strings.ToLower(query) == query {
filterByQuery = func(repo *LocalRepository) bool {
return strings.Contains(strings.ToLower(repo.NonHostPath()), query) &&
(host == "" || repo.PathParts[0] == host)
}
} else {
filterByQuery = func(repo *LocalRepository) bool {
return strings.Contains(repo.NonHostPath(), query) &&
(host == "" || repo.PathParts[0] == host)
}
}
}
}
var (
repos []*LocalRepository
mu sync.Mutex
)
if err := walkLocalRepositories(vcsBackend, func(repo *LocalRepository) {
if !filterByQuery(repo) {
return
}
mu.Lock()
defer mu.Unlock()
repos = append(repos, repo)
}); err != nil {
return fmt.Errorf("failed to filter repos while walkLocalRepositories(repo): %w", err)
}
repoList := make([]string, 0, len(repos))
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() {
continue
}
for _, p := range repo.Subpaths() {
if subpathCount[p] == 1 {
repoList = append(repoList, p)
break
}
}
}
} else {
for _, repo := range repos {
if printFullPaths {
repoList = append(repoList, repo.FullPath)
} else {
repoList = append(repoList, repo.RelPath)
}
}
}
sort.Strings(repoList)
for _, r := range repoList {
fmt.Fprintln(w, r)
}
return nil
}