Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ghq get --vcs=<vcs> #72

Merged
merged 2 commits into from Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions commands.go
Expand Up @@ -27,6 +27,7 @@ var cloneFlags = []cli.Flag{
cli.BoolFlag{Name: "update, u", Usage: "Update local repository if cloned already"},
cli.BoolFlag{Name: "p", Usage: "Clone with SSH"},
cli.BoolFlag{Name: "shallow", Usage: "Do a shallow clone"},
cli.StringFlag{Name: "vcs", Usage: "Specify VCS backend for cloning"},
}

var commandGet = cli.Command{
Expand Down Expand Up @@ -91,7 +92,7 @@ type commandDoc struct {
}

var commandDocs = map[string]commandDoc{
"get": {"", "[-u] <repository URL> | [-u] [-p] <user>/<project>"},
"get": {"", "[-u] [--vcs <vcs>] <repository URL> | [-u] [-p] <user>/<project>"},
"list": {"", "[-p] [-e] [<query>]"},
"look": {"", "<project> | <user>/<project> | <host>/<user>/<project>"},
"import": {"", "< file"},
Expand Down Expand Up @@ -129,6 +130,7 @@ func doGet(c *cli.Context) error {
argURL := c.Args().Get(0)
doUpdate := c.Bool("update")
isShallow := c.Bool("shallow")
vcsBackend := c.String("vcs")

if argURL == "" {
cli.ShowCommandHelp(c, "get")
Expand Down Expand Up @@ -176,14 +178,14 @@ func doGet(c *cli.Context) error {
os.Exit(1)
}

getRemoteRepository(remote, doUpdate, isShallow)
getRemoteRepository(remote, doUpdate, isShallow, vcsBackend)
return nil
}

// 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 getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool) {
func getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool, vcsBackend string) {
remoteURL := remote.URL()
local := LocalRepositoryFromURL(remoteURL)

Expand All @@ -202,10 +204,14 @@ func getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool)
if newPath {
utils.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, path))

vcs, repoURL := remote.VCS()
vcs := vcsRegistry[vcsBackend]
repoURL := remoteURL
if vcs == nil {
utils.Log("error", fmt.Sprintf("Could not find version control system: %s", remoteURL))
os.Exit(1)
vcs, repoURL = remote.VCS()
if vcs == nil {
utils.Log("error", fmt.Sprintf("Could not find version control system: %s", remoteURL))
os.Exit(1)
}
}

err := vcs.Clone(repoURL, path, isShallow)
Expand Down Expand Up @@ -364,9 +370,10 @@ func doLook(c *cli.Context) error {

func doImport(c *cli.Context) error {
var (
doUpdate = c.Bool("update")
isSSH = c.Bool("p")
isShallow = c.Bool("shallow")
doUpdate = c.Bool("update")
isSSH = c.Bool("p")
isShallow = c.Bool("shallow")
vcsBackend = c.String("vcs")
)

var (
Expand Down Expand Up @@ -432,7 +439,7 @@ func doImport(c *cli.Context) error {
continue
}

getRemoteRepository(remote, doUpdate, isShallow)
getRemoteRepository(remote, doUpdate, isShallow, vcsBackend)
}
if err := scanner.Err(); err != nil {
utils.Log("error", fmt.Sprintf("While reading input: %s", err))
Expand Down
2 changes: 1 addition & 1 deletion remote_repository.go
Expand Up @@ -175,7 +175,7 @@ func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL) {
vcs, repoURL, err := detectGoImport(repo.url)
if err == nil {
// vcs == "mod" (modproxy) not supported yet
return vcsBackendMap[vcs], repoURL
return vcsRegistry[vcs], repoURL
}

if utils.RunSilently("hg", "identify", repo.url.String()) == nil {
Expand Down
13 changes: 9 additions & 4 deletions vcs.go
Expand Up @@ -111,8 +111,13 @@ var DarcsBackend = &VCSBackend{
},
}

var vcsBackendMap = map[string]*VCSBackend{
"git": GitBackend,
"hg": MercurialBackend,
"svn": SubversionBackend,
var vcsRegistry = map[string]*VCSBackend{
"git": GitBackend,
"github": GitBackend,
"svn": SubversionBackend,
"subversion": SubversionBackend,
"git-svn": GitsvnBackend,
"hg": MercurialBackend,
"mercurial": MercurialBackend,
"darcs": DarcsBackend,
}