Support branch/tag via @ syntax

This commit is contained in:
Nathan Baulch 2022-08-18 16:02:48 +10:00
parent 4316194dc6
commit fc92209e1b
3 changed files with 34 additions and 8 deletions

View file

@ -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)
}
}

View file

@ -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"})

View file

@ -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,
})