Skip to content

Commit

Permalink
Merge pull request #347 from NathanBaulch/master
Browse files Browse the repository at this point in the history
Support branch/tag via @ syntax
  • Loading branch information
Songmu authored Feb 22, 2023
2 parents 8f948f2 + fc92209 commit 515ba78
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
13 changes: 10 additions & 3 deletions cmd_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ 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"),
shallow: c.Bool("shallow"),
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"),
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
19 changes: 19 additions & 0 deletions cmd_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
10 changes: 5 additions & 5 deletions getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
})
Expand Down
4 changes: 2 additions & 2 deletions local_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down
6 changes: 3 additions & 3 deletions remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 515ba78

Please sign in to comment.