/
fetch.go
98 lines (81 loc) · 2.23 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package commands
import (
"fmt"
"regexp"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
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
## See also:
hub-remote(1), hub(1), git-fetch(1)
`,
}
func init() {
CmdRunner.Use(cmdFetch)
}
func fetch(command *Command, args *Args) {
if !args.IsParamsEmpty() {
err := transformFetchArgs(args)
utils.Check(err)
}
}
func transformFetchArgs(args *Args) error {
names := parseRemoteNames(args)
localRepo, err := github.LocalRepo()
utils.Check(err)
currentProject, currentProjectErr := localRepo.CurrentProject()
projects := make(map[*github.Project]bool)
ownerRegexp := regexp.MustCompile(fmt.Sprintf("^%s$", OwnerRe))
for _, name := range names {
if ownerRegexp.MatchString(name) && !isCloneable(name) {
_, err := localRepo.RemoteByName(name)
if err != nil {
utils.Check(currentProjectErr)
project := github.NewProject(name, currentProject.Name, "")
gh := github.NewClient(project.Host)
repo, err := gh.Repository(project)
if err != nil {
continue
}
projects[project] = repo.Private || repo.Permissions.Push
}
}
}
for project, private := range projects {
args.Before("git", "remote", "add", project.Owner, project.GitURL("", "", private))
}
return nil
}
func parseRemoteNames(args *Args) (names []string) {
words := args.Words()
if i := args.IndexOfParam("--multiple"); i != -1 {
if args.ParamsSize() > 1 {
names = words
}
} else if len(words) > 0 {
remoteName := words[0]
commaPattern := fmt.Sprintf("^%s(,%s)+$", OwnerRe, OwnerRe)
remoteNameRegexp := regexp.MustCompile(commaPattern)
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
}