hub/commands/fetch.go

94 строки
2.0 KiB
Go
Исходник Обычный вид История

2013-07-10 21:49:03 +04:00
package commands
import (
2013-07-11 03:29:26 +04:00
"regexp"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/utils"
2013-07-10 21:49:03 +04:00
)
var cmdFetch = &Command{
Run: fetch,
GitExtension: true,
Usage: "fetch <USER>[,<USER2>...]",
Long: `Add missing remotes prior to performing git fetch.
## Examples:
$ hub fetch --multiple jingweno mislav
> git remote add jingweno git://github.com/jingweno/REPO.git
> git remote add jingweno git://github.com/mislav/REPO.git
> git fetch jingweno
> git fetch mislav
2016-01-24 18:50:01 +03:00
## See also:
hub-remote(1), hub(1), git-fetch(1)
2013-07-10 21:49:03 +04:00
`,
}
func init() {
CmdRunner.Use(cmdFetch)
}
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)
localRepo, err := github.LocalRepo()
utils.Check(err)
2013-12-12 02:08:57 +04:00
projects := make(map[*github.Project]bool)
2013-07-11 03:29:26 +04:00
ownerRegexp := regexp.MustCompile(OwnerRe)
for _, name := range names {
2016-01-22 13:13:45 +03:00
if ownerRegexp.MatchString(name) && !isCloneable(name) {
_, 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
}