Skip to content

Commit

Permalink
Merge pull request #155 from msh5/list-vcs-option
Browse files Browse the repository at this point in the history
List vcs option
  • Loading branch information
Songmu committed May 5, 2019
2 parents 88a318e + 738de52 commit db07460
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions commands.go
Expand Up @@ -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"},
},
Expand Down
21 changes: 12 additions & 9 deletions list.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions list_test.go
Expand Up @@ -54,14 +54,17 @@ 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",
"github.com/Songmu/gobump",
"golang.org/x/crypt",
"golang.org/x/image",
}
svnRepos := []string{
"github.com/msh5/svntest",
}
testCases := []struct {
name string
args []string
Expand Down Expand Up @@ -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...)
Expand Down

0 comments on commit db07460

Please sign in to comment.