diff --git a/commands/create.go b/commands/create.go index 08725aae58e14655a40f905c6fc80645d29098a8..a5d386257ee76c5be4efb07ab282a38faac78add 100644 --- a/commands/create.go +++ b/commands/create.go @@ -110,15 +110,24 @@ func create(command *Command, args *Args) { gh := github.NewClient(project.Host) 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) + 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. Updating git remote") + project = foundProject + } } else { - ui.Errorln("Existing repository detected. Updating git remote") + repo = nil } } else { + repo = nil + } + + if repo == nil { if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) utils.Check(err) diff --git a/features/authentication.feature b/features/authentication.feature index bfe657966194731e917a1396e1ae7434c2d183d1..afb4bec37f985b6f22c2adaf2e44f312b6b1e627 100644 --- a/features/authentication.feature +++ b/features/authentication.feature @@ -358,7 +358,9 @@ Feature: OAuth authentication get('/user') { json :login => 'mislav' } - get('/repos/mislav/dotfiles') { status 200 } + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles' + } """ When I run `hub create` interactively When I type "mislav@example.com" diff --git a/features/create.feature b/features/create.feature index 017d9dac6fe773776a546cb7cd77f578830a8fa3..2048092e54549cb2c6c61486a7c3599eaa5b656c 100644 --- a/features/create.feature +++ b/features/create.feature @@ -140,7 +140,9 @@ Feature: hub create Scenario: GitHub repo already exists Given the GitHub API server: """ - get('/repos/mislav/dotfiles') { status 200 } + get('/repos/mislav/dotfiles') { + json :full_name => 'mislav/dotfiles' + } """ When I successfully run `hub create` Then the output should contain "Existing repository detected. Updating git remote\n" @@ -170,6 +172,36 @@ Feature: hub create When I successfully run `hub create -p` Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Scenario: Renamed GitHub repo already exists + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + redirect 'https://api.github.com/repositories/12345', 301 + } + get('/repositories/12345') { + json :full_name => 'mislav/DOTfiles' + } + """ + When I successfully run `hub create` + And the url for "origin" should be "git@github.com:mislav/DOTfiles.git" + + Scenario: Renamed GitHub repo is unrelated + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + redirect 'https://api.github.com/repositories/12345', 301 + } + get('/repositories/12345') { + json :full_name => 'mislav/old-dotfiles' + } + post('/user/repos') { + status 201 + json :full_name => 'mislav/mydotfiles' + } + """ + When I successfully run `hub create` + And the url for "origin" should be "git@github.com:mislav/mydotfiles.git" + Scenario: API response changes the clone URL Given the GitHub API server: """