diff --git a/commands/create.go b/commands/create.go index f75e474d75a08c83ac36915d48a707394a4f02b7..08725aae58e14655a40f905c6fc80645d29098a8 100644 --- a/commands/create.go +++ b/commands/create.go @@ -109,8 +109,15 @@ func create(command *Command, args *Args) { project := github.NewProject(owner, newRepoName, host.Host) gh := github.NewClient(project.Host) - if gh.IsRepositoryExist(project) { - ui.Errorln("Existing repository detected. Updating git remote") + repo, err := gh.Repository(project) + + if err == nil { + 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. Updating git remote") + } } else { if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) diff --git a/features/create.feature b/features/create.feature index b94bc71b48445267d1883ce7d4738e4a1455146e..01063de38620aea53b4f7d32126935870cc62a37 100644 --- a/features/create.feature +++ b/features/create.feature @@ -146,6 +146,30 @@ Feature: hub create Then the output should contain "Existing repository detected. Updating git remote\n" And the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Scenario: GitHub repo already exists and is not private + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles', + :private => false + } + """ + When I run `hub create -p` + Then the output should contain "Repository 'mislav/dotfiles' already exists and is public\n" + And the exit status should be 1 + And there should be no "origin" remote + + Scenario: GitHub repo already exists and is private + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles', + :private => true + } + """ + When I successfully run `hub create -p` + Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Scenario: API response changes the clone URL Given the GitHub API server: """ diff --git a/github/client.go b/github/client.go index 6e7eccf7a7d883ae81e805eb2284a150d3f7bbf0..1a778bfdb5da7291d26e7d640eb69a39f1e5db0c 100644 --- a/github/client.go +++ b/github/client.go @@ -213,12 +213,6 @@ func (client *Client) Repository(project *Project) (repo *Repository, err error) return } -func (client *Client) IsRepositoryExist(project *Project) bool { - repo, err := client.Repository(project) - - return err == nil && repo != nil -} - func (client *Client) CreateRepository(project *Project, description, homepage string, isPrivate bool) (repo *Repository, err error) { repoURL := "user/repos" if project.Owner != client.Host.User {