-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.go
156 lines (129 loc) · 3.48 KB
/
clone.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package commands
import (
"fmt"
"regexp"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
var cmdClone = &Command{
Run: clone,
GitExtension: true,
Usage: "clone [-p] [<OPTIONS>] [<USER>/]<REPOSITORY> [<DESTINATION>]",
Long: `Clone a repository from GitHub.
## Options:
-p
(Deprecated) Clone private repositories over SSH.
[<USER>/]<REPOSITORY>
<USER> defaults to your own GitHub username.
<DESTINATION>
Directory name to clone into (default: <REPOSITORY>).
## Protocol used for cloning
The 'git:' protocol will be used for cloning public repositories, while the SSH
protocol will be used for private repositories and those that you have push
access to. Alternatively, hub can be configured to use HTTPS protocol for
everything. See "HTTPS instead of git protocol" and "HUB_PROTOCOL" of hub(1).
## Examples:
$ hub clone rtomayko/ronn
> git clone git://github.com/rtomayko/ronn.git
## See also:
hub-fork(1), hub(1), git-clone(1)
`,
}
func init() {
CmdRunner.Use(cmdClone)
}
func clone(command *Command, args *Args) {
if !args.IsParamsEmpty() {
transformCloneArgs(args)
}
}
func transformCloneArgs(args *Args) {
isSSH := parseClonePrivateFlag(args)
// git help clone | grep -e '^ \+-.\+<'
p := utils.NewArgsParser()
p.RegisterValue("--branch", "-b")
p.RegisterValue("--depth")
p.RegisterValue("--reference")
if args.Command == "submodule" {
p.RegisterValue("--name")
} else {
p.RegisterValue("--config", "-c")
p.RegisterValue("--jobs", "-j")
p.RegisterValue("--origin", "-o")
p.RegisterValue("--reference-if-able")
p.RegisterValue("--separate-git-dir")
p.RegisterValue("--shallow-exclude")
p.RegisterValue("--shallow-since")
p.RegisterValue("--template")
p.RegisterValue("--upload-pack", "-u")
}
p.Parse(args.Params)
nameWithOwnerRegexp := regexp.MustCompile(NameWithOwnerRe)
for _, i := range p.PositionalIndices {
a := args.Params[i]
if nameWithOwnerRegexp.MatchString(a) && !isCloneable(a) {
url := getCloneUrl(a, isSSH, args.Command != "submodule")
args.ReplaceParam(i, url)
}
break
}
}
func parseClonePrivateFlag(args *Args) bool {
if i := args.IndexOfParam("-p"); i != -1 {
args.RemoveParam(i)
return true
}
return false
}
func getCloneUrl(nameWithOwner string, isSSH, allowSSH bool) string {
name := nameWithOwner
owner := ""
if strings.Contains(name, "/") {
split := strings.SplitN(name, "/", 2)
owner = split[0]
name = split[1]
}
var host *github.Host
if owner == "" {
config := github.CurrentConfig()
h, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("cloning repository", err))
}
host = h
owner = host.User
}
var hostStr string
if host != nil {
hostStr = host.Host
}
expectWiki := strings.HasSuffix(name, ".wiki")
if expectWiki {
name = strings.TrimSuffix(name, ".wiki")
}
project := github.NewProject(owner, name, hostStr)
gh := github.NewClient(project.Host)
repo, err := gh.Repository(project)
if err != nil {
if strings.Contains(err.Error(), "HTTP 404") {
err = fmt.Errorf("Error: repository %s/%s doesn't exist", project.Owner, project.Name)
}
utils.Check(err)
}
owner = repo.Owner.Login
name = repo.Name
if expectWiki {
if !repo.HasWiki {
utils.Check(fmt.Errorf("Error: %s/%s doesn't have a wiki", owner, name))
} else {
name = name + ".wiki"
}
}
if !isSSH &&
allowSSH &&
!github.IsHttpsProtocol() {
isSSH = repo.Private || repo.Permissions.Push
}
return project.GitURL(name, owner, isSSH)
}