Skip to content

Commit db07460

Browse files
authoredMay 5, 2019
Merge pull request #155 from msh5/list-vcs-option
List vcs option
2 parents 88a318e + 738de52 commit db07460

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed
 

‎commands.go

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var commandList = cli.Command{
5858
Action: doList,
5959
Flags: []cli.Flag{
6060
cli.BoolFlag{Name: "exact, e", Usage: "Perform an exact match"},
61+
cli.StringFlag{Name: "vcs", Usage: "Specify VCS backend for matching"},
6162
cli.BoolFlag{Name: "full-path, p", Usage: "Print full paths"},
6263
cli.BoolFlag{Name: "unique", Usage: "Print unique subpaths"},
6364
},

‎list.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ func doList(c *cli.Context) error {
1212
w = c.App.Writer
1313
query = c.Args().First()
1414
exact = c.Bool("exact")
15+
vcsBackend = c.String("vcs")
1516
printFullPaths = c.Bool("full-path")
1617
printUniquePaths = c.Bool("unique")
1718
)
1819

19-
var filterFn func(*LocalRepository) bool
20-
if query == "" {
21-
filterFn = func(_ *LocalRepository) bool {
22-
return true
23-
}
24-
} else {
20+
filterByQuery := func(_ *LocalRepository) bool {
21+
return true
22+
}
23+
if query != "" {
2524
if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) {
2625
if url, err := newURL(query); err == nil {
2726
if repo, err := LocalRepositoryFromURL(url); err == nil {
@@ -31,7 +30,7 @@ func doList(c *cli.Context) error {
3130
}
3231

3332
if exact {
34-
filterFn = func(repo *LocalRepository) bool {
33+
filterByQuery = func(repo *LocalRepository) bool {
3534
return repo.Matches(query)
3635
}
3736
} else {
@@ -41,16 +40,20 @@ func doList(c *cli.Context) error {
4140
query = strings.Join(paths[1:], "/")
4241
host = paths[0]
4342
}
44-
filterFn = func(repo *LocalRepository) bool {
43+
filterByQuery = func(repo *LocalRepository) bool {
4544
return strings.Contains(repo.NonHostPath(), query) &&
4645
(host == "" || repo.PathParts[0] == host)
4746
}
4847
}
4948
}
49+
filterByVCS := func(repo *LocalRepository) bool {
50+
return vcsBackend == "" ||
51+
vcsRegistry[vcsBackend] == repo.VCS()
52+
}
5053

5154
repos := []*LocalRepository{}
5255
if err := walkLocalRepositories(func(repo *LocalRepository) {
53-
if !filterFn(repo) {
56+
if !filterByQuery(repo) || !filterByVCS(repo) {
5457
return
5558
}
5659
repos = append(repos, repo)

‎list_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ func TestCommandListUnknown(t *testing.T) {
5454
}
5555

5656
func TestDoList_query(t *testing.T) {
57-
repos := []string{
57+
gitRepos := []string{
5858
"github.com/motemen/ghq",
5959
"github.com/motemen/gobump",
6060
"github.com/motemen/gore",
6161
"github.com/Songmu/gobump",
6262
"golang.org/x/crypt",
6363
"golang.org/x/image",
6464
}
65+
svnRepos := []string{
66+
"github.com/msh5/svntest",
67+
}
6568
testCases := []struct {
6669
name string
6770
args []string
@@ -102,12 +105,19 @@ func TestDoList_query(t *testing.T) {
102105
name: "exact query",
103106
args: []string{"-e", "men/go"},
104107
expect: "",
108+
}, {
109+
name: "vcs",
110+
args: []string{"--vcs", "svn"},
111+
expect: "github.com/msh5/svntest\n",
105112
}}
106113

107114
withFakeGitBackend(t, func(t *testing.T, tmproot string, _ *_cloneArgs, _ *_updateArgs) {
108-
for _, r := range repos {
115+
for _, r := range gitRepos {
109116
os.MkdirAll(filepath.Join(tmproot, r, ".git"), 0755)
110117
}
118+
for _, r := range svnRepos {
119+
os.MkdirAll(filepath.Join(tmproot, r, ".svn"), 0755)
120+
}
111121
for _, tc := range testCases {
112122
t.Run(tc.name, func(t *testing.T) {
113123
args := append([]string{"ghq", "list"}, tc.args...)

0 commit comments

Comments
 (0)