2013-07-09 22:03:07 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-10 01:36:02 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-10 01:42:06 +04:00
|
|
|
"strings"
|
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/git"
|
|
|
|
"github.com/github/hub/github"
|
2015-02-10 11:26:45 +03:00
|
|
|
"github.com/github/hub/ui"
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/utils"
|
2013-07-10 01:36:02 +04:00
|
|
|
)
|
|
|
|
|
2013-07-09 22:03:07 +04:00
|
|
|
var cmdCreate = &Command{
|
|
|
|
Run: create,
|
2016-09-11 19:21:23 +03:00
|
|
|
Usage: "create [-poc] [-d <DESCRIPTION>] [-h <HOMEPAGE>] [[<ORGANIZATION>/]<NAME>]",
|
2016-01-24 11:56:18 +03:00
|
|
|
Long: `Create a new repository on GitHub and add a git remote for it.
|
|
|
|
|
|
|
|
## Options:
|
2018-12-18 19:50:38 +03:00
|
|
|
-p, --private
|
2016-01-24 11:56:18 +03:00
|
|
|
Create a private repository.
|
|
|
|
|
2019-01-21 23:35:15 +03:00
|
|
|
-d, --description <DESCRIPTION>
|
2019-01-21 14:42:49 +03:00
|
|
|
A short description of the GitHub repository.
|
2016-01-24 11:56:18 +03:00
|
|
|
|
2019-01-21 23:35:15 +03:00
|
|
|
-h, --homepage <HOMEPAGE>
|
2019-01-21 14:42:49 +03:00
|
|
|
A URL with more information about the repository. Use this, for example, if
|
|
|
|
your project has an external website.
|
2016-01-24 11:56:18 +03:00
|
|
|
|
2019-02-13 03:37:52 +03:00
|
|
|
--remote-name <REMOTE>
|
|
|
|
Set the name for the new git remote (default: "origin").
|
|
|
|
|
2016-09-11 19:21:23 +03:00
|
|
|
-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.
|
|
|
|
|
2016-01-24 11:56:18 +03:00
|
|
|
[<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 git@github.com:USER/REPO.git
|
|
|
|
|
|
|
|
$ hub create sinatra/recipes
|
|
|
|
[ repo created in GitHub organization ]
|
|
|
|
> git remote add -f origin git@github.com:sinatra/recipes.git
|
2016-01-24 18:50:01 +03:00
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub-init(1), hub(1)
|
2013-07-09 22:03:07 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-07-10 01:36:02 +04:00
|
|
|
func init() {
|
2013-12-30 02:18:14 +04:00
|
|
|
CmdRunner.Use(cmdCreate)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 22:03:07 +04:00
|
|
|
func create(command *Command, args *Args) {
|
2013-12-10 10:47:15 +04:00
|
|
|
_, err := git.Dir()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("'create' must be run from inside a git repository")
|
|
|
|
utils.Check(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newRepoName string
|
2013-07-10 01:36:02 +04:00
|
|
|
if args.IsParamsEmpty() {
|
2016-02-28 14:51:21 +03:00
|
|
|
dirName, err := git.WorkdirName()
|
2013-07-10 01:36:02 +04:00
|
|
|
utils.Check(err)
|
2016-02-28 14:51:21 +03:00
|
|
|
newRepoName = github.SanitizeProjectName(dirName)
|
2013-07-10 01:36:02 +04:00
|
|
|
} else {
|
2013-12-10 10:47:15 +04:00
|
|
|
newRepoName = args.FirstParam()
|
2019-02-13 03:29:08 +03:00
|
|
|
if newRepoName == "" {
|
|
|
|
utils.Check(command.UsageError(""))
|
|
|
|
}
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 04:46:13 +04:00
|
|
|
config := github.CurrentConfig()
|
|
|
|
host, err := config.DefaultHost()
|
2014-03-28 03:42:14 +04:00
|
|
|
if err != nil {
|
|
|
|
utils.Check(github.FormatError("creating repository", err))
|
|
|
|
}
|
2013-12-10 10:47:15 +04:00
|
|
|
|
2014-03-26 04:18:30 +04:00
|
|
|
owner := host.User
|
2013-12-10 10:47:15 +04:00
|
|
|
if strings.Contains(newRepoName, "/") {
|
|
|
|
split := strings.SplitN(newRepoName, "/", 2)
|
|
|
|
owner = split[0]
|
|
|
|
newRepoName = split[1]
|
|
|
|
}
|
|
|
|
|
2014-03-26 04:18:30 +04:00
|
|
|
project := github.NewProject(owner, newRepoName, host.Host)
|
2013-12-17 19:45:48 +04:00
|
|
|
gh := github.NewClient(project.Host)
|
2013-12-10 10:47:15 +04:00
|
|
|
|
2019-01-18 03:34:44 +03:00
|
|
|
flagCreatePrivate := args.Flag.Bool("--private")
|
|
|
|
|
2018-08-28 21:59:56 +03:00
|
|
|
repo, err := gh.Repository(project)
|
|
|
|
if err == nil {
|
2018-08-31 14:00:04 +03:00
|
|
|
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 {
|
2018-12-28 02:28:22 +03:00
|
|
|
ui.Errorln("Existing repository detected")
|
2018-08-31 14:00:04 +03:00
|
|
|
project = foundProject
|
|
|
|
}
|
2018-08-28 09:45:39 +03:00
|
|
|
} else {
|
2018-08-31 14:00:04 +03:00
|
|
|
repo = nil
|
2018-08-28 09:45:39 +03:00
|
|
|
}
|
2013-07-10 01:36:02 +04:00
|
|
|
} else {
|
2018-08-31 14:00:04 +03:00
|
|
|
repo = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if repo == nil {
|
2013-07-10 01:36:02 +04:00
|
|
|
if !args.Noop {
|
2019-01-18 03:34:44 +03:00
|
|
|
flagCreateDescription := args.Flag.Value("--description")
|
|
|
|
flagCreateHomepage := args.Flag.Value("--homepage")
|
2013-07-10 01:36:02 +04:00
|
|
|
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
|
|
|
|
utils.Check(err)
|
2013-12-10 10:47:15 +04:00
|
|
|
project = github.NewProject(repo.FullName, "", project.Host)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:17:43 +04:00
|
|
|
localRepo, err := github.LocalRepo()
|
|
|
|
utils.Check(err)
|
|
|
|
|
2019-02-13 03:37:52 +03:00
|
|
|
originName := args.Flag.Value("--remote-name")
|
|
|
|
if originName == "" {
|
|
|
|
originName = "origin"
|
|
|
|
}
|
2019-02-09 15:11:53 +03:00
|
|
|
|
2018-12-28 02:28:22 +03:00
|
|
|
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 {
|
2013-07-10 01:36:02 +04:00
|
|
|
url := project.GitURL("", "", true)
|
2018-12-28 02:28:22 +03:00
|
|
|
args.Before("git", "remote", "add", "-f", originName, url)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|
|
|
|
|
2016-09-11 05:42:24 +03:00
|
|
|
webUrl := project.WebURL("", "", "")
|
|
|
|
args.NoForward()
|
2019-01-18 03:34:44 +03:00
|
|
|
flagCreateBrowse := args.Flag.Bool("--browse")
|
|
|
|
flagCreateCopy := args.Flag.Bool("--copy")
|
2016-09-11 19:21:23 +03:00
|
|
|
printBrowseOrCopy(args, webUrl, flagCreateBrowse, flagCreateCopy)
|
2013-07-10 01:36:02 +04:00
|
|
|
}
|