diff --git a/commands.go b/commands.go index 3508f840..813063bf 100644 --- a/commands.go +++ b/commands.go @@ -58,6 +58,7 @@ var commandList = cli.Command{ Action: doList, Flags: []cli.Flag{ cli.BoolFlag{Name: "exact, e", Usage: "Perform an exact match"}, + cli.StringFlag{Name: "vcs", Usage: "Specify VCS backend for matching"}, cli.BoolFlag{Name: "full-path, p", Usage: "Print full paths"}, cli.BoolFlag{Name: "unique", Usage: "Print unique subpaths"}, }, diff --git a/list.go b/list.go index 9b40151c..615e45f4 100644 --- a/list.go +++ b/list.go @@ -12,16 +12,15 @@ func doList(c *cli.Context) error { 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") ) - var filterFn func(*LocalRepository) bool - if query == "" { - filterFn = func(_ *LocalRepository) bool { - return true - } - } else { + 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 { @@ -31,7 +30,7 @@ func doList(c *cli.Context) error { } if exact { - filterFn = func(repo *LocalRepository) bool { + filterByQuery = func(repo *LocalRepository) bool { return repo.Matches(query) } } else { @@ -41,16 +40,20 @@ func doList(c *cli.Context) error { query = strings.Join(paths[1:], "/") host = paths[0] } - filterFn = func(repo *LocalRepository) bool { + 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 !filterFn(repo) { + if !filterByQuery(repo) || !filterByVCS(repo) { return } repos = append(repos, repo) diff --git a/list_test.go b/list_test.go index d1b8a0d5..921c6c41 100644 --- a/list_test.go +++ b/list_test.go @@ -54,7 +54,7 @@ func TestCommandListUnknown(t *testing.T) { } func TestDoList_query(t *testing.T) { - repos := []string{ + gitRepos := []string{ "github.com/motemen/ghq", "github.com/motemen/gobump", "github.com/motemen/gore", @@ -62,6 +62,9 @@ func TestDoList_query(t *testing.T) { "golang.org/x/crypt", "golang.org/x/image", } + svnRepos := []string{ + "github.com/msh5/svntest", + } testCases := []struct { name string args []string @@ -102,12 +105,19 @@ func TestDoList_query(t *testing.T) { name: "exact query", args: []string{"-e", "men/go"}, expect: "", + }, { + name: "vcs", + args: []string{"--vcs", "svn"}, + expect: "github.com/msh5/svntest\n", }} withFakeGitBackend(t, func(t *testing.T, tmproot string, _ *_cloneArgs, _ *_updateArgs) { - for _, r := range repos { + for _, r := range gitRepos { os.MkdirAll(filepath.Join(tmproot, r, ".git"), 0755) } + for _, r := range svnRepos { + os.MkdirAll(filepath.Join(tmproot, r, ".svn"), 0755) + } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { args := append([]string{"ghq", "list"}, tc.args...)