forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
149 lines (125 loc) · 3.48 KB
/
remote.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
package commands
import (
"fmt"
"regexp"
"strings"
"github.com/github/hub/git"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
var cmdRemote = &Command{
Run: remote,
GitExtension: true,
Usage: `
remote add [-p] [<OPTIONS>] <USER>[/<REPOSITORY>]
remote set-url [-p] [<OPTIONS>] <NAME> <USER>[/<REPOSITORY>]
`,
Long: `Add a git remote for a GitHub repository.
## Options:
-p
(Deprecated) Use the 'ssh:' protocol instead of 'git:' for the remote URL.
The writeable 'ssh:' protocol is automatically used for own repos, GitHub
Enterprise remotes, and private or pushable repositories.
<USER>[/<REPOSITORY>]
If <USER> is "origin", that value will be substituted for your GitHub
username. <REPOSITORY> defaults to the name of the current working directory.
## Examples:
$ hub remote add jingweno
> git remote add jingweno git://github.com/jingweno/REPO.git
$ hub remote add origin
> git remote add origin git@github.com:USER/REPO.git
## See also:
hub-fork(1), hub(1), git-remote(1)
`,
}
func init() {
CmdRunner.Use(cmdRemote)
}
/*
*/
func remote(command *Command, args *Args) {
if !args.IsParamsEmpty() && (args.FirstParam() == "add" || args.FirstParam() == "set-url") {
transformRemoteArgs(args)
}
}
func transformRemoteArgs(args *Args) {
ownerWithName := args.LastParam()
owner, name := parseRepoNameOwner(ownerWithName)
if owner == "" {
return
}
localRepo, err := github.LocalRepo()
utils.Check(err)
var host string
mainProject, err := localRepo.MainProject()
if err == nil {
host = mainProject.Host
}
if name == "" {
if mainProject != nil {
name = mainProject.Name
} else {
dirName, err := git.WorkdirName()
utils.Check(err)
name = github.SanitizeProjectName(dirName)
}
}
var hostConfig *github.Host
if host == "" {
hostConfig, err = github.CurrentConfig().DefaultHost()
} else {
hostConfig, err = github.CurrentConfig().PromptForHost(host)
}
if err != nil {
utils.Check(github.FormatError("adding remote", err))
}
host = hostConfig.Host
numWord := 0
for i, p := range args.Params {
if !looksLikeFlag(p) && (i < 1 || args.Params[i-1] != "-t") {
numWord += 1
if numWord == 2 && strings.Contains(p, "/") {
args.ReplaceParam(i, owner)
} else if numWord == 3 {
args.RemoveParam(i)
}
}
}
if numWord == 2 && owner == "origin" {
owner = hostConfig.User
}
if strings.EqualFold(owner, hostConfig.User) {
owner = hostConfig.User
}
project := github.NewProject(owner, name, host)
isPrivate := parseRemotePrivateFlag(args) || owner == hostConfig.User || project.Host != github.GitHubHost
if !isPrivate {
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)
}
isPrivate = repo.Private || repo.Permissions.Push
}
url := project.GitURL("", "", isPrivate)
args.AppendParams(url)
}
func parseRemotePrivateFlag(args *Args) bool {
if i := args.IndexOfParam("-p"); i != -1 {
args.RemoveParam(i)
return true
}
return false
}
func parseRepoNameOwner(nameWithOwner string) (owner, name string) {
nameWithOwnerRe := fmt.Sprintf("^(%s)(?:\\/(%s))?$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
owner, name = result[1], result[2]
}
return
}