2013-07-10 21:49:03 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2016-08-14 23:02:54 +03:00
|
|
|
"fmt"
|
2013-07-11 03:29:26 +04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-04-02 00:40:02 +04:00
|
|
|
|
2020-04-17 02:02:37 +03:00
|
|
|
"github.com/github/hub/v2/github"
|
|
|
|
"github.com/github/hub/v2/utils"
|
2013-07-10 21:49:03 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdFetch = &Command{
|
|
|
|
Run: fetch,
|
|
|
|
GitExtension: true,
|
2016-01-24 11:56:18 +03:00
|
|
|
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
|
2018-07-06 05:36:30 +03:00
|
|
|
> git remote add mislav git://github.com/mislav/REPO.git
|
2016-01-24 11:56:18 +03:00
|
|
|
> 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
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +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)
|
2014-04-02 00:40:02 +04:00
|
|
|
|
|
|
|
localRepo, err := github.LocalRepo()
|
|
|
|
utils.Check(err)
|
2013-12-12 02:08:57 +04:00
|
|
|
|
2016-08-14 21:33:14 +03:00
|
|
|
currentProject, currentProjectErr := localRepo.CurrentProject()
|
2016-02-28 13:46:11 +03:00
|
|
|
|
2013-12-12 02:08:57 +04:00
|
|
|
projects := make(map[*github.Project]bool)
|
2016-09-08 15:29:44 +03:00
|
|
|
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) {
|
2013-12-28 13:14:07 +04:00
|
|
|
_, err := localRepo.RemoteByName(name)
|
2013-07-11 03:29:26 +04:00
|
|
|
if err != nil {
|
2016-08-14 21:33:14 +03:00
|
|
|
utils.Check(currentProjectErr)
|
2016-02-28 13:46:11 +03:00
|
|
|
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
|
|
|
|
2018-01-19 17:49:46 +03:00
|
|
|
projects[project] = repo.Private || repo.Permissions.Push
|
2013-12-12 02:08:57 +04:00
|
|
|
}
|
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]
|
2016-08-14 23:02:54 +03:00
|
|
|
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
|
|
|
|
}
|