From c4af574b19596ecbfec44738e9eab52142549702 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Fri, 25 Mar 2022 12:46:40 -0400 Subject: [PATCH] Change default protocol to HTTPS (#2939) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Explicitly setting `hub.protocol` to `git` or using `HUB_PROTOCOL=git` still uses the `git` protocol. Co-authored-by: Mislav Marohnić --- README.md | 10 +++---- commands/clone.go | 23 +++++++--------- commands/fork.go | 6 +---- features/authentication.feature | 4 +-- features/checkout.feature | 3 ++- features/cherry_pick.feature | 4 +-- features/clone.feature | 41 +++++++++++++++++++--------- features/create.feature | 38 ++++++++++---------------- features/fetch.feature | 47 ++++++++++++++++++--------------- features/fork.feature | 32 ++++++++-------------- features/init.feature | 6 ++--- features/remote_add.feature | 47 +++++++++++++-------------------- features/steps.rb | 4 +-- features/submodule_add.feature | 10 +++---- github/project.go | 20 +++++++------- github/util.go | 10 ------- share/man/man1/hub.1.md | 8 +++--- 17 files changed, 144 insertions(+), 169 deletions(-) delete mode 100644 github/util.go diff --git a/README.md b/README.md index 966a4bb4..9cbf5793 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,12 @@ Usage ``` sh $ hub clone rtomayko/tilt -#=> git clone git://github.com/rtomayko/tilt.git - -# if you prefer HTTPS to git/SSH protocols: -$ git config --global hub.protocol https -$ hub clone rtomayko/tilt #=> git clone https://github.com/rtomayko/tilt.git + +# or, if you prefer the SSH protocol: +$ git config --global hub.protocol ssh +$ hub clone rtomayko/tilt +#=> git clone git@github.com:rtomayko/tilt.git ``` See [usage examples](https://hub.github.com/#developer) or the [full reference diff --git a/commands/clone.go b/commands/clone.go index 5c3008b1..d05986f2 100644 --- a/commands/clone.go +++ b/commands/clone.go @@ -27,14 +27,13 @@ var cmdClone = &Command{ ## Protocol used for cloning -The ''git:'' protocol will be used for cloning public repositories, while the SSH -protocol will be used for private repositories and those that you have push -access to. Alternatively, hub can be configured to use HTTPS protocol for -everything. See "HTTPS instead of git protocol" and "HUB_PROTOCOL" of hub(1). +HTTPS protocol is used by hub as the default. Alternatively, hub can be +configured to use SSH protocol for all git operations. See "SSH instead +of HTTPS protocol" and "HUB_PROTOCOL" of hub(1). ## Examples: $ hub clone rtomayko/ronn - > git clone git://github.com/rtomayko/ronn.git + > git clone https://github.com/rtomayko/ronn.git ## See also: @@ -53,7 +52,7 @@ func clone(command *Command, args *Args) { } func transformCloneArgs(args *Args) { - isSSH := parseClonePrivateFlag(args) + isPrivate := parseClonePrivateFlag(args) // git help clone | grep -e '^ \+-.\+<' p := utils.NewArgsParser() @@ -80,7 +79,7 @@ func transformCloneArgs(args *Args) { i := p.PositionalIndices[0] a := args.Params[i] if nameWithOwnerRegexp.MatchString(a) && !isCloneable(a) { - url := getCloneURL(a, isSSH, args.Command != "submodule") + url := getCloneURL(a, isPrivate, args.Command != "submodule") args.ReplaceParam(i, url) } } @@ -95,7 +94,7 @@ func parseClonePrivateFlag(args *Args) bool { return false } -func getCloneURL(nameWithOwner string, isSSH, allowSSH bool) string { +func getCloneURL(nameWithOwner string, allowPush, allowPrivate bool) string { name := nameWithOwner owner := "" if strings.Contains(name, "/") { @@ -146,11 +145,9 @@ func getCloneURL(nameWithOwner string, isSSH, allowSSH bool) string { } } - if !isSSH && - allowSSH && - !github.IsHTTPSProtocol() { - isSSH = repo.Private || repo.Permissions.Push + if !allowPush && allowPrivate { + allowPush = repo.Private || repo.Permissions.Push } - return project.GitURL(name, owner, isSSH) + return project.GitURL(name, owner, allowPush) } diff --git a/commands/fork.go b/commands/fork.go index ecf168d1..38d93ad1 100644 --- a/commands/fork.go +++ b/commands/fork.go @@ -53,9 +53,6 @@ func fork(cmd *Command, args *Args) { host, err := config.PromptForHost(project.Host) utils.Check(github.FormatError("forking repository", err)) - originRemote, err := localRepo.RemoteForProject(project) - utils.Check(err) - params := map[string]interface{}{} forkOwner := host.User if flagForkOrganization := args.Flag.Value("--org"); flagForkOrganization != "" { @@ -100,8 +97,7 @@ func fork(cmd *Command, args *Args) { args.NoForward() if !args.Flag.Bool("--no-remote") { - - originURL := originRemote.URL.String() + originURL := project.GitURL("", "", false) url := forkProject.GitURL("", "", true) // Check to see if the remote already exists. diff --git a/features/authentication.feature b/features/authentication.feature index b379fcc0..22729699 100644 --- a/features/authentication.feature +++ b/features/authentication.feature @@ -322,7 +322,7 @@ Feature: OAuth authentication """ Given $GITHUB_TOKEN is "PTOKEN" When I successfully run `hub clone dotfiles` - Then it should clone "git@github.com:parkr/dotfiles.git" + Then it should clone "https://github.com/parkr/dotfiles.git" And the file "../home/.config/hub" should contain "user: mislav" And the file "../home/.config/hub" should contain "oauth_token: OTOKEN" @@ -462,7 +462,7 @@ Feature: OAuth authentication And the file "../home/.config/hub" should contain "git.my.org" And the file "../home/.config/hub" should contain "user: mislav" And the file "../home/.config/hub" should contain "oauth_token: OTOKEN" - And the url for "mislav" should be "git@git.my.org:mislav/dotfiles.git" + And the url for "mislav" should be "https://git.my.org/mislav/dotfiles.git" Scenario: Broken config is missing user. Given a file named "../home/.config/hub" with: diff --git a/features/checkout.feature b/features/checkout.feature index 5fa24d89..bd20875c 100644 --- a/features/checkout.feature +++ b/features/checkout.feature @@ -273,6 +273,7 @@ Feature: hub checkout }, :maintainer_can_modify => true } """ + And git protocol is preferred When I successfully run `hub checkout -f https://github.com/mojombo/jekyll/pull/77 -q` Then "git fetch origin refs/pull/77/head:fixes" should be run And "git checkout -f fixes -q" should be run @@ -299,6 +300,7 @@ Feature: hub checkout }, :maintainer_can_modify => true } """ + And git protocol is preferred And I am on the "fixes" branch And there is a git FETCH_HEAD When I successfully run `hub checkout https://github.com/mojombo/jekyll/pull/77` @@ -328,7 +330,6 @@ Feature: hub checkout }, :maintainer_can_modify => true } """ - And HTTPS is preferred When I successfully run `hub checkout -f https://github.com/mojombo/jekyll/pull/77 -q` Then "git fetch origin refs/pull/77/head:fixes" should be run And "git checkout -f fixes -q" should be run diff --git a/features/cherry_pick.feature b/features/cherry_pick.feature index 498d0cd7..649f9065 100644 --- a/features/cherry_pick.feature +++ b/features/cherry_pick.feature @@ -45,14 +45,14 @@ Feature: hub cherry-pick Scenario: Using GitHub owner@SHA notation with remote add When I run `hub cherry-pick mislav@a319d88` - Then "git remote add _hub-cherry-pick git://github.com/mislav/ronn.git" should be run + Then "git remote add _hub-cherry-pick https://github.com/mislav/ronn.git" should be run And "git fetch -q --no-tags _hub-cherry-pick" should be run And "git remote rm _hub-cherry-pick" should be run And "git cherry-pick a319d88" should be run Scenario: From fork that doesn't have a remote When I run `hub cherry-pick https://github.com/jingweno/ronn/commit/a319d88` - Then "git remote add _hub-cherry-pick git://github.com/jingweno/ronn.git" should be run + Then "git remote add _hub-cherry-pick https://github.com/jingweno/ronn.git" should be run And "git fetch -q --no-tags _hub-cherry-pick" should be run And "git remote rm _hub-cherry-pick" should be run And "git cherry-pick a319d88" should be run diff --git a/features/clone.feature b/features/clone.feature index e11d2643..1d511b92 100644 --- a/features/clone.feature +++ b/features/clone.feature @@ -12,7 +12,7 @@ Feature: hub clone } """ When I successfully run `hub clone rtomayko/ronn` - Then it should clone "git://github.com/rtomayko/ronn.git" + Then it should clone "https://github.com/rtomayko/ronn.git" And the output should not contain anything Scenario: Clone a public repo with period in name @@ -25,7 +25,7 @@ Feature: hub clone } """ When I successfully run `hub clone hookio/hook.js` - Then it should clone "git://github.com/hookio/hook.js.git" + Then it should clone "https://github.com/hookio/hook.js.git" And the output should not contain anything Scenario: Clone a public repo that starts with a period @@ -38,7 +38,7 @@ Feature: hub clone } """ When I successfully run `hub clone zhuangya/.vim` - Then it should clone "git://github.com/zhuangya/.vim.git" + Then it should clone "https://github.com/zhuangya/.vim.git" And the output should not contain anything Scenario: Clone a repo even if same-named directory exists @@ -52,11 +52,24 @@ Feature: hub clone """ And a directory named "rtomayko/ronn" When I successfully run `hub clone rtomayko/ronn` + Then it should clone "https://github.com/rtomayko/ronn.git" + And the output should not contain anything + + Scenario: Clone a public repo with git + Given git protocol is preferred + Given the GitHub API server: + """ + get('/repos/rtomayko/ronn') { + json :private => false, + :name => 'ronn', :owner => { :login => 'rtomayko' }, + :permissions => { :push => false } + } + """ + When I successfully run `hub clone rtomayko/ronn` Then it should clone "git://github.com/rtomayko/ronn.git" And the output should not contain anything Scenario: Clone a public repo with HTTPS - Given HTTPS is preferred Given the GitHub API server: """ get('/repos/rtomayko/ronn') { @@ -80,7 +93,7 @@ Feature: hub clone """ When I successfully run `git config --global alias.c "clone --bare"` And I successfully run `hub c rtomayko/ronn` - Then "git clone --bare git://github.com/rtomayko/ronn.git" should be run + Then "git clone --bare https://github.com/rtomayko/ronn.git" should be run And the output should not contain anything Scenario: Unchanged public clone @@ -133,8 +146,8 @@ Feature: hub clone :permissions => { :push => false } } """ - When I successfully run `hub --noop clone -p rtomayko/ronn` - Then the output should contain exactly "git clone git@github.com:rtomayko/ronn.git\n" + When I successfully run `hub --noop clone rtomayko/ronn` + Then the output should contain exactly "git clone https://github.com/rtomayko/ronn.git\n" But it should not clone anything Scenario: Clone a private repo @@ -147,7 +160,7 @@ Feature: hub clone } """ When I successfully run `hub clone -p rtomayko/ronn` - Then it should clone "git@github.com:rtomayko/ronn.git" + Then it should clone "https://github.com/rtomayko/ronn.git" And the output should not contain anything Scenario: Clone my repo @@ -160,7 +173,7 @@ Feature: hub clone } """ When I successfully run `hub clone dotfiles` - Then it should clone "git@github.com:mislav/dotfiles.git" + Then it should clone "https://github.com/mislav/dotfiles.git" And the output should not contain anything Scenario: Clone my repo that doesn't exist @@ -184,7 +197,7 @@ Feature: hub clone } """ When I successfully run `hub clone --bare -o master dotfiles` - Then "git clone --bare -o master git@github.com:mislav/dotfiles.git" should be run + Then "git clone --bare -o master https://github.com/mislav/dotfiles.git" should be run And the output should not contain anything Scenario: Clone repo to which I have push access to @@ -196,6 +209,7 @@ Feature: hub clone :permissions => { :push => true } } """ + And git protocol is preferred When I successfully run `hub clone sstephenson/rbenv` Then "git clone git@github.com:sstephenson/rbenv.git" should be run And the output should not contain anything @@ -209,6 +223,7 @@ Feature: hub clone :permissions => { :push => true } } """ + And git protocol is preferred When I successfully run `hub --noop clone sstephenson/rbenv` Then the output should contain exactly "git clone git@github.com:sstephenson/rbenv.git\n" But it should not clone anything @@ -225,7 +240,7 @@ Feature: hub clone } """ When I successfully run `hub clone myorg/myrepo` - Then it should clone "git@git.my.org:myorg/myrepo.git" + Then it should clone "https://git.my.org/myorg/myrepo.git" And the output should not contain anything Scenario: Clone from existing directory is a local clone @@ -251,7 +266,7 @@ Feature: hub clone } """ When I successfully run `hub clone rtomayko/ronn.wiki` - Then it should clone "git://github.com/RTomayko/ronin.wiki.git" + Then it should clone "https://github.com/RTomayko/ronin.wiki.git" And the output should not contain anything Scenario: Clone a nonexisting wiki @@ -284,5 +299,5 @@ Feature: hub clone } """ When I successfully run `hub clone rtomayko/ronn` - Then it should clone "git://github.com/RTomayko/ronin.git" + Then it should clone "https://github.com/RTomayko/ronin.git" And the output should not contain anything diff --git a/features/create.feature b/features/create.feature index a43a6e14..26ae9972 100644 --- a/features/create.feature +++ b/features/create.feature @@ -13,7 +13,7 @@ Feature: hub create } """ When I successfully run `hub create` - Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" And the output should contain exactly "https://github.com/mislav/dotfiles\n" Scenario: Create private repo @@ -25,6 +25,7 @@ Feature: hub create json :full_name => 'mislav/dotfiles' } """ + And git protocol is preferred When I successfully run `hub create -p` Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" @@ -37,21 +38,9 @@ Feature: hub create } """ When I successfully run `hub create --remote-name=work` - Then the url for "work" should be "git@github.com:mislav/dotfiles.git" + Then the url for "work" should be "https://github.com/mislav/dotfiles.git" And there should be no "origin" remote - Scenario: HTTPS is preferred - Given the GitHub API server: - """ - post('/user/repos') { - status 201 - json :full_name => 'mislav/dotfiles' - } - """ - And HTTPS is preferred - When I successfully run `hub create` - Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" - Scenario: Create in organization Given the GitHub API server: """ @@ -61,7 +50,7 @@ Feature: hub create } """ When I successfully run `hub create acme/dotfiles` - Then the url for "origin" should be "git@github.com:acme/dotfiles.git" + Then the url for "origin" should be "https://github.com/acme/dotfiles.git" And the output should contain exactly "https://github.com/acme/dotfiles\n" Scenario: Creating repo failed @@ -84,7 +73,7 @@ Feature: hub create } """ When I successfully run `hub create myconfig` - Then the url for "origin" should be "git@github.com:mislav/myconfig.git" + Then the url for "origin" should be "https://github.com/mislav/myconfig.git" Scenario: With description and homepage Given the GitHub API server: @@ -97,7 +86,7 @@ Feature: hub create } """ When I successfully run `hub create -d mydesc -h http://example.com` - Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" Scenario: Not in git repo Given the current dir is not a repo @@ -152,7 +141,7 @@ Feature: hub create """ And the "github" remote has url "git://github.com/mislav/dotfiles.git" When I successfully run `hub create` - Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" Scenario: GitHub repo already exists Given the GitHub API server: @@ -163,7 +152,7 @@ Feature: hub create """ When I successfully run `hub create` Then the output should contain "Existing repository detected\n" - And the url for "origin" should be "git@github.com:mislav/dotfiles.git" + And the url for "origin" should be "https://github.com/mislav/dotfiles.git" Scenario: GitHub repo already exists and is not private Given the GitHub API server: @@ -186,6 +175,7 @@ Feature: hub create :private => true } """ + And git protocol is preferred When I successfully run `hub create -p` Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" @@ -200,7 +190,7 @@ Feature: hub create } """ When I successfully run `hub create` - And the url for "origin" should be "git@github.com:mislav/DOTfiles.git" + And the url for "origin" should be "https://github.com/mislav/DOTfiles.git" Scenario: Renamed GitHub repo is unrelated Given the GitHub API server: @@ -217,7 +207,7 @@ Feature: hub create } """ When I successfully run `hub create` - And the url for "origin" should be "git@github.com:mislav/mydotfiles.git" + And the url for "origin" should be "https://github.com/mislav/mydotfiles.git" Scenario: API response changes the clone URL Given the GitHub API server: @@ -228,7 +218,7 @@ Feature: hub create } """ When I successfully run `hub create` - Then the url for "origin" should be "git@github.com:Mooslav/myconfig.git" + Then the url for "origin" should be "https://github.com/Mooslav/myconfig.git" And the output should contain exactly "https://github.com/Mooslav/myconfig\n" Scenario: Open new repository in web browser @@ -254,7 +244,7 @@ Feature: hub create } """ When I successfully run `hub create` - Then the url for "origin" should be "git@github.com:mislav/my-dot-files.git" + Then the url for "origin" should be "https://github.com/mislav/my-dot-files.git" Scenario: Verbose API output Given the GitHub API server: @@ -299,7 +289,7 @@ Feature: hub create """ And $GITHUB_HOST is "git.my.org" When I successfully run `hub create` - Then the url for "origin" should be "git@git.my.org:nsartor/dotfiles.git" + Then the url for "origin" should be "https://git.my.org/nsartor/dotfiles.git" And the output should contain exactly "https://git.my.org/nsartor/dotfiles\n" Scenario: Invalid GITHUB_HOST diff --git a/features/fetch.feature b/features/fetch.feature index fa7c83ce..199c9ada 100644 --- a/features/fetch.feature +++ b/features/fetch.feature @@ -36,6 +36,20 @@ Feature: hub fetch And there should be no "mislav" remote Scenario: Creates new remote + Given the GitHub API server: + """ + get('/repos/mislav/dotfiles') { + json :private => false, + :permissions => { :push => false } + } + """ + When I successfully run `hub fetch mislav` + Then "git fetch mislav" should be run + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" + And the output should not contain anything + + Scenario: Creates new remote with git + Given git protocol is preferred Given the GitHub API server: """ get('/repos/mislav/dotfiles') { @@ -58,22 +72,9 @@ Feature: hub fetch """ When I successfully run `hub fetch ankit-maverick` Then "git fetch ankit-maverick" should be run - And the url for "ankit-maverick" should be "git://github.com/ankit-maverick/dotfiles.git" + And the url for "ankit-maverick" should be "https://github.com/ankit-maverick/dotfiles.git" And the output should not contain anything - Scenario: HTTPS is preferred - Given the GitHub API server: - """ - get('/repos/mislav/dotfiles') { - json :private => false, - :permissions => { :push => false } - } - """ - And HTTPS is preferred - When I successfully run `hub fetch mislav` - Then "git fetch mislav" should be run - And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" - Scenario: Private repo Given the GitHub API server: """ @@ -82,6 +83,7 @@ Feature: hub fetch :permissions => { :push => false } } """ + And git protocol is preferred When I successfully run `hub fetch mislav` Then "git fetch mislav" should be run And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" @@ -95,6 +97,7 @@ Feature: hub fetch :permissions => { :push => true } } """ + And git protocol is preferred When I successfully run `hub fetch mislav` Then "git fetch mislav" should be run And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" @@ -121,8 +124,8 @@ Feature: hub fetch """ When I successfully run `hub fetch --multiple mislav rtomayko` Then "git fetch --multiple mislav rtomayko" should be run - And the url for "mislav" should be "git://github.com/mislav/dotfiles.git" - And the url for "rtomayko" should be "git://github.com/rtomayko/dotfiles.git" + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" + And the url for "rtomayko" should be "https://github.com/rtomayko/dotfiles.git" Scenario: Fetch multiple with filtering Given the GitHub API server: @@ -133,9 +136,9 @@ Feature: hub fetch } """ When I successfully run `git config remotes.mygrp "foo bar"` - When I successfully run `hub fetch --multiple origin mislav mygrp git://example.com typo` - Then "git fetch --multiple origin mislav mygrp git://example.com typo" should be run - And the url for "mislav" should be "git://github.com/mislav/dotfiles.git" + When I successfully run `hub fetch --multiple origin mislav mygrp https://example.com typo` + Then "git fetch --multiple origin mislav mygrp https://example.com typo" should be run + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" But there should be no "mygrp" remote And there should be no "typo" remote @@ -149,9 +152,9 @@ Feature: hub fetch """ When I successfully run `hub fetch mislav,rtomayko,dustinleblanc` Then "git fetch --multiple mislav rtomayko dustinleblanc" should be run - And the url for "mislav" should be "git://github.com/mislav/dotfiles.git" - And the url for "rtomayko" should be "git://github.com/rtomayko/dotfiles.git" - And the url for "dustinleblanc" should be "git://github.com/dustinleblanc/dotfiles.git" + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" + And the url for "rtomayko" should be "https://github.com/rtomayko/dotfiles.git" + And the url for "dustinleblanc" should be "https://github.com/dustinleblanc/dotfiles.git" Scenario: Doesn't create a new remote if repo doesn't exist on GitHub Given the GitHub API server: diff --git a/features/fork.feature b/features/fork.feature index 34716587..952b40a6 100644 --- a/features/fork.feature +++ b/features/fork.feature @@ -20,11 +20,12 @@ Feature: hub fork """ When I successfully run `hub fork` Then the output should contain exactly "new remote: mislav\n" - And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run - And "git remote set-url mislav git@github.com:mislav/dotfiles.git" should be run - And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" + And "git remote add -f mislav https://github.com/evilchelu/dotfiles.git" should be run + And "git remote set-url mislav https://github.com/mislav/dotfiles.git" should be run + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" Scenario: Fork the repository with new remote name specified + Given git protocol is preferred Given the GitHub API server: """ get('/repos/mislav/dotfiles') { 404 } @@ -46,6 +47,7 @@ Feature: hub fork And the url for "upstream" should be "git://github.com/evilchelu/dotfiles.git" Scenario: Fork the repository with redirect + Given git protocol is preferred Given the GitHub API server: """ before { @@ -69,6 +71,7 @@ Feature: hub fork Scenario: Fork the repository when origin URL is private Given the "origin" remote has url "git@github.com:evilchelu/dotfiles.git" + And git protocol is preferred Given the GitHub API server: """ before { halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN' } @@ -80,7 +83,7 @@ Feature: hub fork """ When I successfully run `hub fork` Then the output should contain exactly "new remote: mislav\n" - And "git remote add -f mislav ssh://git@github.com/evilchelu/dotfiles.git" should be run + And "git remote add -f mislav git://github.com/evilchelu/dotfiles.git" should be run And "git remote set-url mislav git@github.com:mislav/dotfiles.git" should be run And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" @@ -140,7 +143,7 @@ Feature: hub fork """ new remote: mislav\n """ - And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" + And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" Scenario: Redirected repo already exists Given the GitHub API server: @@ -225,19 +228,6 @@ Feature: hub fork Error creating fork: Unauthorized (HTTP 401)\n """ - Scenario: HTTPS is preferred - Given the GitHub API server: - """ - post('/repos/evilchelu/dotfiles/forks') { - status 202 - json :name => 'dotfiles', :owner => { :login => 'mislav' } - } - """ - And HTTPS is preferred - When I successfully run `hub fork` - Then the output should contain exactly "new remote: mislav\n" - And the url for "mislav" should be "https://github.com/mislav/dotfiles.git" - Scenario: Not in repo Given the current dir is not a repo When I run `hub fork` @@ -279,7 +269,7 @@ Feature: hub fork And I am "mislav" on git.my.org with OAuth token "FITOKEN" And "git.my.org" is a whitelisted Enterprise host When I successfully run `hub fork` - Then the url for "mislav" should be "git@git.my.org:mislav/dotfiles.git" + Then the url for "mislav" should be "https://git.my.org/mislav/dotfiles.git" Scenario: Enterprise fork using regular HTTP Given the GitHub API server: @@ -298,7 +288,7 @@ Feature: hub fork And I am "mislav" on http://git.my.org with OAuth token "FITOKEN" And "git.my.org" is a whitelisted Enterprise host When I successfully run `hub fork` - Then the url for "mislav" should be "git@git.my.org:mislav/dotfiles.git" + Then the url for "mislav" should be "https://git.my.org/mislav/dotfiles.git" Scenario: Fork a repo to a specific organization Given the GitHub API server: @@ -312,4 +302,4 @@ Feature: hub fork """ When I successfully run `hub fork --org=acme` Then the output should contain exactly "new remote: acme\n" - Then the url for "acme" should be "git@github.com:acme/dotfiles.git" + Then the url for "acme" should be "https://github.com/acme/dotfiles.git" diff --git a/features/init.feature b/features/init.feature index c0160f06..630c0879 100644 --- a/features/init.feature +++ b/features/init.feature @@ -6,16 +6,16 @@ Feature: hub init Scenario: Initializes a git repo with remote When I successfully run `hub init -g` - Then the url for "origin" should be "git@github.com:mislav/dotfiles.git" + Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" Scenario: Initializes a git repo in a new directory with remote When I successfully run `hub init -g new_dir` And I cd to "new_dir" - Then the url for "origin" should be "git@github.com:mislav/new_dir.git" + Then the url for "origin" should be "https://github.com/mislav/new_dir.git" Scenario: Enterprise host Given $GITHUB_HOST is "git.my.org" And I am "mislav" on git.my.org with OAuth token "FITOKEN" And "git.my.org" is a whitelisted Enterprise host When I successfully run `hub init -g` - Then the url for "origin" should be "git@git.my.org:mislav/dotfiles.git" + Then the url for "origin" should be "https://git.my.org/mislav/dotfiles.git" diff --git a/features/remote_add.feature b/features/remote_add.feature index 5bd9881f..e2d4ba9e 100644 --- a/features/remote_add.feature +++ b/features/remote_add.feature @@ -6,7 +6,7 @@ Feature: hub remote add Scenario: Add origin remote for my own repo Given there are no remotes When I successfully run `hub remote add origin` - Then the url for "origin" should be "git@github.com:EvilChelu/dotfiles.git" + Then the url for "origin" should be "https://github.com/EvilChelu/dotfiles.git" And the output should not contain anything Scenario: Add origin remote for my own repo using -C @@ -14,7 +14,7 @@ Feature: hub remote add And I cd to ".." When I successfully run `hub -C dotfiles remote add origin` And I cd to "dotfiles" - Then the url for "origin" should be "git@github.com:EvilChelu/dotfiles.git" + Then the url for "origin" should be "https://github.com/EvilChelu/dotfiles.git" And the output should not contain anything Scenario: Unchanged public remote add @@ -44,6 +44,7 @@ Feature: hub remote add Scenario: Add new remote for Enterprise repo Given "git.my.org" is a whitelisted Enterprise host + And git protocol is preferred And I am "ProLoser" on git.my.org with OAuth token "FITOKEN" And the "origin" remote has url "git@git.my.org:mislav/topsekrit.git" When I successfully run `hub remote add another` @@ -60,7 +61,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add mislav` - Then the url for "mislav" should be "git://github.com/mislav/dotfiles.git" + Then the url for "mislav" should be "https://github.com/mislav/dotfiles.git" And the output should not contain anything Scenario: Add detected private remote @@ -72,6 +73,7 @@ Feature: hub remote add :permissions => { :push => false } } """ + And git protocol is preferred When I successfully run `hub remote add mislav` Then the url for "mislav" should be "git@github.com:mislav/dotfiles.git" And the output should not contain anything @@ -86,7 +88,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add mislav` - Then the url for "mislav" should be "git@github.com:mislav/dotfiles.git" + Then the url for "mislav" should be "https://github.com/mislav/dotfiles.git" And the output should not contain anything Scenario: Add remote for missing repo @@ -104,11 +106,13 @@ Feature: hub remote add """ Scenario: Add explicitly private remote + Given git protocol is preferred When I successfully run `hub remote add -p mislav` Then the url for "mislav" should be "git@github.com:mislav/dotfiles.git" And the output should not contain anything Scenario: Remote for my own repo is automatically private + Given git protocol is preferred When I successfully run `hub remote add evilchelu` Then the url for "evilchelu" should be "git@github.com:EvilChelu/dotfiles.git" And the output should not contain anything @@ -123,7 +127,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add -f mislav` - Then "git remote add -f mislav git://github.com/mislav/dotfiles.git" should be run + Then "git remote add -f mislav https://github.com/mislav/dotfiles.git" should be run And the output should not contain anything Scenario: Add remote with branch argument @@ -136,21 +140,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add -f -t feature mislav` - Then "git remote add -f -t feature mislav git://github.com/mislav/dotfiles.git" should be run - And the output should not contain anything - - Scenario: Add HTTPS protocol remote - Given the GitHub API server: - """ - get('/repos/mislav/dotfiles') { - json :private => false, - :name => 'dotfiles', :owner => { :login => 'mislav' }, - :permissions => { :push => false } - } - """ - Given HTTPS is preferred - When I successfully run `hub remote add mislav` - Then the url for "mislav" should be "https://github.com/mislav/dotfiles.git" + Then "git remote add -f -t feature mislav https://github.com/mislav/dotfiles.git" should be run And the output should not contain anything Scenario: Add named public remote @@ -163,7 +153,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add mm mislav` - Then the url for "mm" should be "git://github.com/mislav/dotfiles.git" + Then the url for "mm" should be "https://github.com/mislav/dotfiles.git" And the output should not contain anything Scenario: set-url @@ -175,9 +165,9 @@ Feature: hub remote add :permissions => { :push => false } } """ - Given the "origin" remote has url "git://github.com/evilchelu/dotfiles.git" + Given the "origin" remote has url "https://github.com/evilchelu/dotfiles.git" When I successfully run `hub remote set-url origin mislav` - Then the url for "origin" should be "git://github.com/mislav/dotfiles.git" + Then the url for "origin" should be "https://github.com/mislav/dotfiles.git" And the output should not contain anything Scenario: Add public remote including repo name @@ -190,7 +180,7 @@ Feature: hub remote add } """ When I successfully run `hub remote add mislav/dotfilez.js` - Then the url for "mislav" should be "git://github.com/mislav/dotfilez.js.git" + Then the url for "mislav" should be "https://github.com/mislav/dotfilez.js.git" And the output should not contain anything Scenario: Add named public remote including repo name @@ -203,27 +193,28 @@ Feature: hub remote add } """ When I successfully run `hub remote add mm mislav/dotfilez.js` - Then the url for "mm" should be "git://github.com/mislav/dotfilez.js.git" + Then the url for "mm" should be "https://github.com/mislav/dotfilez.js.git" And the output should not contain anything Scenario: Add named private remote + Given git protocol is preferred When I successfully run `hub remote add -p mm mislav` Then the url for "mm" should be "git@github.com:mislav/dotfiles.git" And the output should not contain anything Scenario: Add private remote including repo name When I successfully run `hub remote add -p mislav/dotfilez.js` - Then the url for "mislav" should be "git@github.com:mislav/dotfilez.js.git" + Then the url for "mislav" should be "https://github.com/mislav/dotfilez.js.git" And the output should not contain anything Scenario: Add named private remote including repo name When I successfully run `hub remote add -p mm mislav/dotfilez.js` - Then the url for "mm" should be "git@github.com:mislav/dotfilez.js.git" + Then the url for "mm" should be "https://github.com/mislav/dotfilez.js.git" And the output should not contain anything Scenario: Add named private remote for my own repo including repo name When I successfully run `hub remote add ec evilchelu/dotfilez.js` - Then the url for "ec" should be "git@github.com:EvilChelu/dotfilez.js.git" + Then the url for "ec" should be "https://github.com/EvilChelu/dotfilez.js.git" And the output should not contain anything Scenario: Avoid crash in argument parsing diff --git a/features/steps.rb b/features/steps.rb index 83cefad6..44612da6 100644 --- a/features/steps.rb +++ b/features/steps.rb @@ -1,7 +1,7 @@ require 'fileutils' -Given(/^HTTPS is preferred$/) do - run_ignored_command %(git config --global hub.protocol https) +Given(/^git protocol is preferred$/) do + set_environment_variable "HUB_PROTOCOL", "git" end Given(/^there are no remotes$/) do diff --git a/features/submodule_add.feature b/features/submodule_add.feature index 23504743..4d34e177 100644 --- a/features/submodule_add.feature +++ b/features/submodule_add.feature @@ -18,7 +18,7 @@ Feature: hub submodule add } """ When I successfully run `hub submodule add mojombo/grit vendor/grit` - Then the "vendor/grit" submodule url should be "git://github.com/mojombo/grit.git" + Then the "vendor/grit" submodule url should be "https://github.com/mojombo/grit.git" And the output should contain exactly: """ Adding existing repo at 'vendor/grit' to the index\n @@ -34,7 +34,7 @@ Feature: hub submodule add } """ When I successfully run `hub submodule add -p mojombo/grit vendor/grit` - Then the "vendor/grit" submodule url should be "git@github.com:mojombo/grit.git" + Then the "vendor/grit" submodule url should be "https://github.com/mojombo/grit.git" Scenario: A submodule for my own repo is public nevertheless Given the GitHub API server: @@ -46,7 +46,7 @@ Feature: hub submodule add } """ When I successfully run `hub submodule add grit vendor/grit` - Then the "vendor/grit" submodule url should be "git://github.com/mislav/grit.git" + Then the "vendor/grit" submodule url should be "https://github.com/mislav/grit.git" Scenario: Add submodule with arguments Given the GitHub API server: @@ -58,7 +58,7 @@ Feature: hub submodule add } """ When I successfully run `hub submodule add -b foo --name grit mojombo/grit vendor/grit` - Then "git submodule add -b foo --name grit git://github.com/mojombo/grit.git vendor/grit" should be run + Then "git submodule add -b foo --name grit https://github.com/mojombo/grit.git vendor/grit" should be run Scenario: Add submodule with branch Given the GitHub API server: @@ -70,4 +70,4 @@ Feature: hub submodule add } """ When I successfully run `hub submodule add --branch foo mojombo/grit vendor/grit` - Then "git submodule add --branch foo git://github.com/mojombo/grit.git vendor/grit" should be run + Then "git submodule add --branch foo https://github.com/mojombo/grit.git vendor/grit" should be run diff --git a/github/project.go b/github/project.go index a0bdb275..0c5c9fb8 100644 --- a/github/project.go +++ b/github/project.go @@ -62,7 +62,7 @@ func (p *Project) WebURL(name, owner, path string) string { return url } -func (p *Project) GitURL(name, owner string, isSSH bool) (url string) { +func (p *Project) GitURL(name, owner string, allowPush bool) string { if name == "" { name = p.Name } @@ -72,15 +72,17 @@ func (p *Project) GitURL(name, owner string, isSSH bool) (url string) { host := rawHost(p.Host) - if preferredProtocol() == "https" { - url = fmt.Sprintf("https://%s/%s/%s.git", host, owner, name) - } else if isSSH || preferredProtocol() == "ssh" { - url = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name) - } else { - url = fmt.Sprintf("git://%s/%s/%s.git", host, owner, name) + switch preferredProtocol() { + case "git": + if allowPush { + return fmt.Sprintf("git@%s:%s/%s.git", host, owner, name) + } + return fmt.Sprintf("git://%s/%s/%s.git", host, owner, name) + case "ssh": + return fmt.Sprintf("git@%s:%s/%s.git", host, owner, name) + default: + return fmt.Sprintf("https://%s/%s/%s.git", host, owner, name) } - - return url } // Remove the scheme from host when the host url is absolute. diff --git a/github/util.go b/github/util.go deleted file mode 100644 index ef9d0628..00000000 --- a/github/util.go +++ /dev/null @@ -1,10 +0,0 @@ -package github - -import ( - "github.com/github/hub/v2/git" -) - -func IsHTTPSProtocol() bool { - httpProtocol, _ := git.Config("hub.protocol") - return httpProtocol == "https" -} diff --git a/share/man/man1/hub.1.md b/share/man/man1/hub.1.md index 1fd3cc9f..b0cee185 100644 --- a/share/man/man1/hub.1.md +++ b/share/man/man1/hub.1.md @@ -141,12 +141,12 @@ variables. Alternatively, you may provide `GITHUB_TOKEN`, an access token with **repo** permissions. This will not be written to `~/.config/hub`. -### HTTPS instead of git protocol +### SSH instead of HTTPS protocol -If you prefer the HTTPS protocol for git operations, you can configure hub to -generate all URLs with `https:` instead of `git:` or `ssh:`: +If you prefer the SSH protocol for git operations, you can configure hub to +generate SSH-style URLs: - $ git config --global hub.protocol https + $ git config --global hub.protocol ssh This will affect `clone`, `fork`, `remote add` and other hub commands that expand shorthand references to GitHub repo URLs.