2013-07-10 21:49:03 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
2013-07-11 03:29:26 +04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2013-07-10 21:49:03 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdFetch = &Command{
|
|
|
|
Run: fetch,
|
|
|
|
GitExtension: true,
|
|
|
|
Usage: "fetch [USER...]",
|
|
|
|
Short: "Download data, tags and branches from a remote repository",
|
|
|
|
Long: `Adds missing remote(s) with git remote add prior to fetching. New
|
|
|
|
remotes are only added if they correspond to valid forks on GitHub.
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +04:00
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdFetch)
|
|
|
|
}
|
|
|
|
|
2013-07-10 21:49:03 +04:00
|
|
|
/*
|
|
|
|
$ gh fetch jingweno
|
|
|
|
> git remote add jingweno git://github.com/jingweno/REPO.git
|
|
|
|
> git fetch jingweno
|
|
|
|
|
|
|
|
$ git fetch jingweno,foo
|
|
|
|
> git remote add jingweno ...
|
|
|
|
> git remote add foo ...
|
|
|
|
> git fetch --multiple jingweno foo
|
2013-07-11 03:29:26 +04:00
|
|
|
|
|
|
|
$ git fetch --multiple jingweno foo
|
|
|
|
> git remote add jingweno ...
|
|
|
|
> git remote add foo ...
|
|
|
|
> git fetch --multiple jingweno foo
|
2013-07-10 21:49:03 +04:00
|
|
|
*/
|
|
|
|
func fetch(command *Command, args *Args) {
|
2013-07-11 03:29:26 +04:00
|
|
|
if !args.IsParamsEmpty() {
|
2013-07-12 21:49:10 +04:00
|
|
|
err := tranformFetchArgs(args)
|
|
|
|
utils.Check(err)
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 21:49:10 +04:00
|
|
|
func tranformFetchArgs(args *Args) error {
|
2013-07-11 03:29:26 +04:00
|
|
|
names := parseRemoteNames(args)
|
2013-12-12 02:08:57 +04:00
|
|
|
localRepo := github.LocalRepo()
|
|
|
|
|
|
|
|
projects := make(map[*github.Project]bool)
|
2013-07-11 03:29:26 +04:00
|
|
|
ownerRegexp := regexp.MustCompile(OwnerRe)
|
|
|
|
for _, name := range names {
|
2013-12-12 02:08:57 +04:00
|
|
|
if ownerRegexp.MatchString(name) {
|
2013-12-28 13:14:07 +04:00
|
|
|
_, err := localRepo.RemoteByName(name)
|
2013-07-11 03:29:26 +04:00
|
|
|
if err != nil {
|
2013-12-12 02:08:57 +04:00
|
|
|
project := github.NewProject(name, "", "")
|
2013-12-17 19:45:48 +04:00
|
|
|
gh := github.NewClient(project.Host)
|
2013-12-12 02:08:57 +04:00
|
|
|
repo, err := gh.Repository(project)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-11 03:29:26 +04:00
|
|
|
|
2013-12-12 02:08:57 +04:00
|
|
|
projects[project] = repo.Private
|
|
|
|
}
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 02:08:57 +04:00
|
|
|
for project, private := range projects {
|
|
|
|
args.Before("git", "remote", "add", project.Owner, project.GitURL("", "", private))
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
2013-07-12 21:49:10 +04:00
|
|
|
|
|
|
|
return nil
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseRemoteNames(args *Args) (names []string) {
|
2013-12-12 02:08:57 +04:00
|
|
|
words := args.Words()
|
2013-07-11 03:29:26 +04:00
|
|
|
if i := args.IndexOfParam("--multiple"); i != -1 {
|
|
|
|
if args.ParamsSize() > 1 {
|
2013-12-12 02:08:57 +04:00
|
|
|
names = words
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
2013-12-12 02:08:57 +04:00
|
|
|
} else if len(words) > 0 {
|
|
|
|
remoteName := words[0]
|
2013-07-11 03:29:26 +04:00
|
|
|
remoteNameRegexp := regexp.MustCompile("^\\w+(,\\w+)$")
|
|
|
|
if remoteNameRegexp.MatchString(remoteName) {
|
|
|
|
i := args.IndexOfParam(remoteName)
|
|
|
|
args.RemoveParam(i)
|
|
|
|
names = strings.Split(remoteName, ",")
|
|
|
|
args.InsertParam(i, names...)
|
|
|
|
args.InsertParam(i, "--multiple")
|
|
|
|
} else {
|
|
|
|
names = append(names, remoteName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|