forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
151 lines (123 loc) · 3.82 KB
/
create.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
package commands
import (
"fmt"
"strings"
"github.com/github/hub/v2/git"
"github.com/github/hub/v2/github"
"github.com/github/hub/v2/ui"
"github.com/github/hub/v2/utils"
)
var cmdCreate = &Command{
Run: create,
Usage: "create [-poc] [-d <DESCRIPTION>] [-h <HOMEPAGE>] [[<ORGANIZATION>/]<NAME>]",
Long: `Create a new repository on GitHub and add a git remote for it.
## Options:
-p, --private
Create a private repository.
-d, --description <DESCRIPTION>
A short description of the GitHub repository.
-h, --homepage <HOMEPAGE>
A URL with more information about the repository. Use this, for example, if
your project has an external website.
--remote-name <REMOTE>
Set the name for the new git remote (default: "origin").
-o, --browse
Open the new repository in a web browser.
-c, --copy
Put the URL of the new repository to clipboard instead of printing it.
[<ORGANIZATION>/]<NAME>
The name for the repository on GitHub (default: name of the current working
directory).
Optionally, create the repository within <ORGANIZATION>.
## Examples:
$ hub create
[ repo created on GitHub ]
> git remote add -f origin [email protected]:USER/REPO.git
$ hub create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add -f origin [email protected]:sinatra/recipes.git
## See also:
hub-init(1), hub(1)
`,
}
func init() {
CmdRunner.Use(cmdCreate)
}
func create(command *Command, args *Args) {
_, err := git.Dir()
if err != nil {
err = fmt.Errorf("'create' must be run from inside a git repository")
utils.Check(err)
}
var newRepoName string
if args.IsParamsEmpty() {
dirName, err := git.WorkdirName()
utils.Check(err)
newRepoName = github.SanitizeProjectName(dirName)
} else {
newRepoName = args.FirstParam()
if newRepoName == "" {
utils.Check(command.UsageError(""))
}
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("creating repository", err))
}
owner := host.User
if strings.Contains(newRepoName, "/") {
split := strings.SplitN(newRepoName, "/", 2)
owner = split[0]
newRepoName = split[1]
}
project := github.NewProject(owner, newRepoName, host.Host)
gh := github.NewClient(project.Host)
flagCreatePrivate := args.Flag.Bool("--private")
repo, err := gh.Repository(project)
if err == nil {
foundProject := github.NewProject(repo.FullName, "", project.Host)
if foundProject.SameAs(project) {
if !repo.Private && flagCreatePrivate {
err = fmt.Errorf("Repository '%s' already exists and is public", repo.FullName)
utils.Check(err)
} else {
ui.Errorln("Existing repository detected")
project = foundProject
}
} else {
repo = nil
}
} else {
repo = nil
}
if repo == nil {
if !args.Noop {
flagCreateDescription := args.Flag.Value("--description")
flagCreateHomepage := args.Flag.Value("--homepage")
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
utils.Check(err)
project = github.NewProject(repo.FullName, "", project.Host)
}
}
localRepo, err := github.LocalRepo()
utils.Check(err)
originName := args.Flag.Value("--remote-name")
if originName == "" {
originName = "origin"
}
if originRemote, err := localRepo.RemoteByName(originName); err == nil {
originProject, err := originRemote.Project()
if err != nil || !originProject.SameAs(project) {
ui.Errorf("A git remote named '%s' already exists and is set to push to '%s'.\n", originRemote.Name, originRemote.PushURL)
}
} else {
url := project.GitURL("", "", true)
args.Before("git", "remote", "add", "-f", originName, url)
}
webURL := project.WebURL("", "", "")
args.NoForward()
flagCreateBrowse := args.Flag.Bool("--browse")
flagCreateCopy := args.Flag.Bool("--copy")
printBrowseOrCopy(args, webURL, flagCreateBrowse, flagCreateCopy)
}