hub/commands/fetch.go

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

2013-07-10 21:49:03 +04:00
package commands
import (
"fmt"
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() {
2017-11-08 05:50:17 +03:00
err := transformFetchArgs(args)
2013-07-12 21:49:10 +04:00
utils.Check(err)
2013-07-11 03:29:26 +04:00
}
}
2017-11-08 05:50:17 +03:00
func transformFetchArgs(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
currentProject, currentProjectErr := localRepo.CurrentProject()
2013-12-12 02:08:57 +04:00
projects := make(map[*github.Project]bool)
ownerRegexp := regexp.MustCompile(fmt.Sprintf("^%s$", OwnerRe))
2013-07-11 03:29:26 +04:00
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 {
utils.Check(currentProjectErr)
project := github.NewProject(name, currentProject.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]
commaPattern := fmt.Sprintf("^%s(,%s)+$", OwnerRe, OwnerRe)
remoteNameRegexp := regexp.MustCompile(commaPattern)
2013-07-11 03:29:26 +04:00
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
}