forked from mislav/hub
/
remote.go
135 lines (115 loc) · 3.29 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
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 [-p] OPTIONS USER[/REPOSITORY]",
Short: "View and manage a set of remote repositories",
Long: `Add remote "git://github.com/USER/REPOSITORY.git" as with
git-remote(1). When /REPOSITORY is omitted, the basename of the
current working directory is used. With -p, use private remote
"git@github.com:USER/REPOSITORY.git". If USER is "origin"
then uses your GitHub login.
`,
}
func init() {
CmdRunner.Use(cmdRemote)
}
/*
$ hub remote add jingweno
> git remote add jingweno git://github.com/jingweno/THIS_REPO.git
$ hub remote add -p jingweno
> git remote add jingweno git@github.com:jingweno/THIS_REPO.git
$ hub remote add origin
> git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git
*/
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
words := args.Words()
isPrivate := parseRemotePrivateFlag(args)
if len(words) == 2 && words[1] == "origin" {
// Origin special case triggers default user/repo
owner = hostConfig.User
} else if len(words) == 2 {
// gh remote add jingweno foo/bar
if idx := args.IndexOfParam(words[1]); idx != -1 {
args.ReplaceParam(idx, owner)
}
} else {
args.RemoveParam(args.ParamsSize() - 1)
}
if strings.ToLower(owner) == strings.ToLower(hostConfig.User) {
owner = hostConfig.User
isPrivate = true
}
project := github.NewProject(owner, name, host)
// for GitHub Enterprise
isPrivate = isPrivate || project.Host != github.GitHubHost
url := project.GitURL(name, owner, 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) {
ownerRe := fmt.Sprintf("^(%s)$", OwnerRe)
ownerRegexp := regexp.MustCompile(ownerRe)
if ownerRegexp.MatchString(nameWithOwner) {
owner = ownerRegexp.FindStringSubmatch(nameWithOwner)[1]
return
}
nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
owner = result[1]
name = result[2]
}
return
}