From 4316194dc68d9a077afa5c4a389f4cc9928d8bd3 Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Thu, 18 Aug 2022 16:02:06 +1000 Subject: [PATCH 1/2] Minor linting --- commands.go | 2 +- local_repository_test.go | 4 ++-- remote_repository.go | 6 +++--- vcs_test.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index 0ac4e0c..fd23711 100644 --- a/commands.go +++ b/commands.go @@ -98,7 +98,7 @@ func mkCommandsTemplate(genTemplate func(commandDoc) string) string { func init() { argsTemplate := mkCommandsTemplate(func(doc commandDoc) string { return doc.Arguments }) - parentTemplate := mkCommandsTemplate(func(doc commandDoc) string { return string(strings.TrimLeft(doc.Parent+" ", " ")) }) + parentTemplate := mkCommandsTemplate(func(doc commandDoc) string { return strings.TrimLeft(doc.Parent+" ", " ") }) cli.CommandHelpTemplate = `NAME: {{.Name}} - {{.Usage}} diff --git a/local_repository_test.go b/local_repository_test.go index f942cf2..6225b5b 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -182,7 +182,7 @@ func TestList_Symlink(t *testing.T) { t.Fatal(err) } - paths := []string{} + var paths []string walkAllLocalRepositories(func(repo *LocalRepository) { paths = append(paths, repo.RelPath) }) @@ -219,7 +219,7 @@ func TestList_Symlink_In_Same_Directory(t *testing.T) { t.Fatal(err) } - paths := []string{} + var paths []string walkAllLocalRepositories(func(repo *LocalRepository) { paths = append(paths, repo.RelPath) }) diff --git a/remote_repository.go b/remote_repository.go index e389c43..95404a8 100644 --- a/remote_repository.go +++ b/remote_repository.go @@ -26,7 +26,7 @@ type GitHubRepository struct { url *url.URL } -// URL reutrns URL of the repository +// URL returns URL of the repository func (repo *GitHubRepository) URL() *url.URL { return repo.url } @@ -58,12 +58,12 @@ type GitHubGistRepository struct { url *url.URL } -// URL returns URL of the GistRepositroy +// URL returns URL of the GistRepository func (repo *GitHubGistRepository) URL() *url.URL { return repo.url } -// IsValid determine if the gist rpository is valid or not +// IsValid determine if the gist repository is valid or not func (repo *GitHubGistRepository) IsValid() bool { return true } diff --git a/vcs_test.go b/vcs_test.go index 1d9e068..4af7357 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -29,7 +29,7 @@ Last Changed Date: 2019-08-16 15:16:45 +0900 (Fri, 16 Aug 2019) func TestVCSBackend(t *testing.T) { tempDir := newTempDir(t) localDir := filepath.Join(tempDir, "repo") - _commands := []*exec.Cmd{} + var _commands []*exec.Cmd lastCommand := func() *exec.Cmd { return _commands[len(_commands)-1] } defer func(orig func(cmd *exec.Cmd) error) { cmdutil.CommandRunner = orig From fc92209e1b82b3e8c496b8a30fc043b39271232b Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Thu, 18 Aug 2022 16:02:48 +1000 Subject: [PATCH 2/2] Support branch/tag via @ syntax --- cmd_get.go | 13 ++++++++++--- cmd_get_test.go | 19 +++++++++++++++++++ getter.go | 10 +++++----- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd_get.go b/cmd_get.go index b4f952d..e85c229 100644 --- a/cmd_get.go +++ b/cmd_get.go @@ -23,6 +23,7 @@ func doGet(c *cli.Context) error { args = c.Args().Slice() andLook = c.Bool("look") parallel = c.Bool("parallel") + branch = c.String("branch") ) g := &getter{ update: c.Bool("update"), @@ -30,7 +31,6 @@ func doGet(c *cli.Context) error { ssh: c.Bool("p"), vcs: c.String("vcs"), silent: c.Bool("silent"), - branch: c.String("branch"), recursive: !c.Bool("no-recursive"), bare: c.Bool("bare"), } @@ -59,17 +59,24 @@ func doGet(c *cli.Context) error { if firstArg == "" { firstArg = target } + b := branch + if branch == "" { + pos := strings.LastIndexByte(target, '@') + if pos >= 0 { + target, b = target[:pos], target[pos+1:] + } + } if parallel { sem <- struct{}{} eg.Go(func() error { defer func() { <-sem }() - if err := g.get(target); err != nil { + if err := g.get(target, b); err != nil { logger.Logf("error", "failed to get %q: %s", target, err) } return nil }) } else { - if err := g.get(target); err != nil { + if err := g.get(target, b); err != nil { return fmt.Errorf("failed to get %q: %w", target, err) } } diff --git a/cmd_get_test.go b/cmd_get_test.go index 3e4f14b..ed4a71c 100644 --- a/cmd_get_test.go +++ b/cmd_get_test.go @@ -157,6 +157,25 @@ func TestCommandGet(t *testing.T) { } }, }, { + name: "specific branch using @ syntax", + scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) { + localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo") + + expectBranch := "hello" + app.Run([]string{"", "get", "-shallow", "motemen/ghq-test-repo@" + expectBranch}) + + expect := "https://github.com/motemen/ghq-test-repo" + if cloneArgs.remote.String() != expect { + t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect) + } + if filepath.ToSlash(cloneArgs.local) != filepath.ToSlash(localDir) { + t.Errorf("got: %s, expect: %s", filepath.ToSlash(cloneArgs.local), filepath.ToSlash(localDir)) + } + if cloneArgs.branch != expectBranch { + t.Errorf("got: %q, expect: %q", cloneArgs.branch, expectBranch) + } + }, + }, { name: "with --no-recursive option", scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) { app.Run([]string{"", "get", "--no-recursive", "motemen/ghq-test-repo"}) diff --git a/getter.go b/getter.go index d36dcf5..e92989b 100644 --- a/getter.go +++ b/getter.go @@ -21,10 +21,10 @@ func getRepoLock(localRepoRoot string) bool { type getter struct { update, shallow, silent, ssh, recursive, bare bool - vcs, branch string + vcs string } -func (g *getter) get(argURL string) error { +func (g *getter) get(argURL, branch string) error { u, err := newURL(argURL, g.ssh, false) if err != nil { return fmt.Errorf("Could not parse URL %q: %w", argURL, err) @@ -35,13 +35,13 @@ func (g *getter) get(argURL string) error { return err } - return g.getRemoteRepository(remote) + return g.getRemoteRepository(remote, branch) } // getRemoteRepository clones or updates a remote repository remote. // If doUpdate is true, updates the locally cloned repository. Otherwise does nothing. // If isShallow is true, does shallow cloning. (no effect if already cloned or the VCS is Mercurial and git-svn) -func (g *getter) getRemoteRepository(remote RemoteRepository) error { +func (g *getter) getRemoteRepository(remote RemoteRepository, branch string) error { remoteURL := remote.URL() local, err := LocalRepositoryFromURL(remoteURL) if err != nil { @@ -95,7 +95,7 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error { dir: localRepoRoot, shallow: g.shallow, silent: g.silent, - branch: g.branch, + branch: branch, recursive: g.recursive, bare: g.bare, })