Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions cmd_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (

func doGet(c *cli.Context) error {
var (
args = c.Args().Slice()
andLook = c.Bool("look")
parallel = c.Bool("parallel")
silent = c.Bool("silent")
args = c.Args().Slice()
andLook = c.Bool("look")
parallel = c.Bool("parallel")
silent = c.Bool("silent")
printPath = c.Bool("print")
)
g := &getter{
update: c.Bool("update"),
Expand Down Expand Up @@ -74,15 +75,22 @@ func doGet(c *cli.Context) error {
sem <- struct{}{}
eg.Go(func() error {
defer func() { <-sem }()
if getInfo, err = g.get(target); err != nil {
logger.Logf("error", "failed to get %q: %s", target, err)
info, getErr := g.get(target)
getInfo, err = info, getErr
if getErr != nil {
logger.Logf("error", "failed to get %q: %s", target, getErr)
} else if printPath && info.localRepository != nil {
fmt.Println(info.localRepository.FullPath)
}
return nil
})
} else {
if getInfo, err = g.get(target); err != nil {
return fmt.Errorf("failed to get %q: %w", target, err)
}
if printPath && getInfo.localRepository != nil {
fmt.Println(getInfo.localRepository.FullPath)
}
}
}
if err = scr.Err(); err != nil {
Expand Down
67 changes: 67 additions & 0 deletions cmd_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,73 @@ func TestCommandGet(t *testing.T) {
}
}

func TestCommandGet_print(t *testing.T) {
testCases := []struct {
name string
args []string
inputRepos []string
}{{
name: "single repo",
args: []string{"", "get", "--print", "motemen/ghq-test-repo"},
inputRepos: nil,
}, {
name: "bulk from stdin",
args: []string{"", "get", "--print"},
inputRepos: []string{"github.com/x-motemen/ghq", "github.com/motemen/gore"},
}, {
name: "bulk parallel",
args: []string{"", "get", "--print", "--parallel"},
inputRepos: []string{"github.com/x-motemen/ghq", "github.com/motemen/gore"},
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withFakeGitBackend(t, func(t *testing.T, tmpRoot string, _ *_cloneArgs, _ *_updateArgs) {
// pre-create dirs for bulk cases
for _, r := range tc.inputRepos {
os.MkdirAll(filepath.Join(tmpRoot, r, ".git"), 0755)
}

var out string
var err error
if len(tc.inputRepos) > 0 {
out, _, err = captureWithInput(tc.inputRepos, func() {
newApp().Run(tc.args)
})
} else {
out, _, err = capture(func() {
newApp().Run(tc.args)
})
}
if err != nil {
t.Fatalf("capture error: %s", err)
}

lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
if len(tc.inputRepos) == 0 {
// single repo: output should contain the local path
if len(lines) != 1 || lines[0] == "" {
t.Errorf("expected one path in output, got: %q", out)
}
if !filepath.IsAbs(lines[0]) {
t.Errorf("expected absolute path, got: %q", lines[0])
}
} else {
// bulk: one path per repo
if len(lines) != len(tc.inputRepos) {
t.Errorf("expected %d paths, got %d: %q", len(tc.inputRepos), len(lines), out)
}
for _, line := range lines {
if !filepath.IsAbs(line) {
t.Errorf("expected absolute path, got: %q", line)
}
}
}
})
})
}
}

func TestLook(t *testing.T) {
withFakeGitBackend(t, func(t *testing.T, tmproot string, _ *_cloneArgs, _ *_updateArgs) {
os.MkdirAll(filepath.Join(tmproot, "github.com", "motemen", "ghq", ".git"), 0755)
Expand Down
3 changes: 2 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var commandGet = &cli.Command{
Usage: "Specify `branch` name. This flag implies --single-branch on Git"},
&cli.BoolFlag{Name: "parallel", Aliases: []string{"P"}, Usage: "Import parallelly"},
&cli.BoolFlag{Name: "bare", Usage: "Do a bare clone"},
&cli.BoolFlag{Name: "print", Usage: "Print local repository path after get"},
&cli.StringFlag{
Name: "partial",
Usage: "Do a partial clone. Can specify either \"blobless\" or \"treeless\"",
Expand Down Expand Up @@ -107,7 +108,7 @@ type commandDoc struct {
}

var commandDocs = map[string]commandDoc{
"get": {"", "[-u] [-p] [--shallow] [--vcs <vcs>] [--look] [--silent] [--branch <branch>] [--no-recursive] [--bare] [--partial blobless|treeless] <repository URL>|<project>|<user>/<project>|<host>/<user>/<project>"},
"get": {"", "[-u] [-p] [--shallow] [--vcs <vcs>] [--look] [--silent] [--branch <branch>] [--no-recursive] [--bare] [--partial blobless|treeless] [--print] <repository URL>|<project>|<user>/<project>|<host>/<user>/<project>"},
"list": {"", "[-p] [-e] [<query>]"},
"create": {"", "<project>|<user>/<project>|<host>/<user>/<project>"},
"rm": {"", "<project>|<user>/<project>|<host>/<user>/<project>"},
Expand Down