Repository navigation
gh repo fork no longer respects the git protocol of the original repo when git_protocol is not configured #9058
Description
Activity
- addedbugSomething isn't workingSomething isn't workingpriority-3Affects a small number of users or is largely cosmeticAffects a small number of users or is largely cosmeticgh-reporelating to the gh repo commandrelating to the gh repo command
on May 7, 2024 Why didn't the tests catch this?
This branch is covered by tests but they didn't fail when the behaviour changed because the test setup doesn't accurately reflect reality. In reality, the
git_protocolconfig value can only behttpsorsshbut the test sets it to the empty string on line257:cli/pkg/cmd/repo/fork/fork_test.go
Lines 243 to 264 in 4896546
name: "implicit match, no configured protocol", tty: true, opts: &ForkOptions{ Remote: true, RemoteName: "fork", }, remotes: []*context.Remote{ { Remote: &git.Remote{Name: "origin", PushURL: &url.URL{ Scheme: "ssh", }}, Repo: ghrepo.New("OWNER", "REPO"), }, }, cfgStubs: func(_ *testing.T, c config.Config) { c.Set("", "git_protocol", "") }, httpStubs: forkPost, execStubs: func(cs *run.CommandStubber) { cs.Register(`git remote add fork git@github\.com:someone/REPO\.git`, 0, "") }, wantErrOut: "✓ Created fork someone/REPO\n✓ Added remote fork\n", The consequence for this is that our config code treats this as a valid configuration with the value of empty string rather than returning
httpsas it would when exercised through the CLI.Removing this configuration stub fails as expected, with an attempt to add an
httpsremote failing because the expectation is that we are adding ansshremote:execStubs: func(cs *run.CommandStubber) { cs.Register(`git remote add fork git@github\.com:someone/REPO\.git`, 0, "") },--- FAIL: TestRepoFork (0.01s) --- FAIL: TestRepoFork/implicit_match,_no_configured_protocol (0.00s) /Users/williammartin/workspace/cli/pkg/cmd/repo/fork/stub.go:27: unmatched stubs (1): git remote add fork git@github\.com:someone/REPO\.git panic: no exec stub for `git remote add fork https://github.com/someone/REPO.git` [recovered] panic: no exec stub for `git remote add fork https://github.com/someone/REPO.git`What should we do about this?
Although no one has complained that we've broken them, the previous behaviour seems reasonable and we did have a regression. I'm inclined to return this behaviour to it's previous state.
I have an additional reason to believe this is the right thing to do and that is because
gh auth refreshoffers configuration of the Git Credential Manager in cases where thegit_protocolwas not set andghis not the credential manager e.g.:➜ cat ~/.config/gh/hosts.yml github.com: users: williammartin: user: williammartin➜ gh auth refresh ? Authenticate Git with your GitHub credentials? (Y/n)This is a departure from
gh auth loginwhich only prompts for this if the user selected thehttpsprotocol.- linked a pull request that will close this issueFix repo fork regression #9063
on May 8, 2024
Describe the bug
In #1056, it was requested that
gh repo forkrespected the protocol of the existing remote i.e. if the remote was usingsshthen the fork would as well. Later in #2711, it was requested that the configuredgit_protocolwould take precedence over the remote, and only if nogit_protocolwas configured, we would use the protocol of the existing remote.In #8246 the
repo forkcommand was modified to callconfig.GitProtocolrather than directly accessing thegit_protocolconfig key. The difference here is that theGitProtocolmethod always returns the configured value or the default ofhttps. However, this means that the following code branch could never be exercised:cli/pkg/cmd/repo/fork/fork.go
Lines 257 to 272 in 4896546
Steps to reproduce the behavior
cd $(mktemp -d)gh repo clone [email protected]:cli/cli.gitcd cligh repo fork --remote=truegit remote -vExpected behaviour
The new fork uses the ssh protocol because the repo was originally cloned through that.
Actual behaviour
The fork was added using the https protocol:
Extra Details
It's worth knowing that the
git_protocolwill not be configured in the following cases:--with-tokenand not providing--git-protocole.g.echo $TOKEN | gh auth login --with-tokenGH_PROMPT_DISABLED=true gh auth loginGH_TOKENor another env var for authentication (and no entry in the hosts file)