Skip to content

git_protocol not respected when adding remote for fork #2711

Description

@MarijnS95

Describe the feature or problem you’d like to solve

I prefer to clone git repositories by hand, and use https for opensource projects as that's much faster without SSH authentication going on. As soon as I have something to submit and want to create a fork to push to I use SSH instead of https auth.

Setting git_protocol as suggested by other issues has previously been working fine for this use-case. However, since the introduction of #2434 (to close #1056) setting git_protocol globally nor for the github.com remote allows this anymore. The change in that PR overwrites the value read from git_protocol in the config with the scheme of the forked repo URL.

Proposed solution

I propose to change precedence of the protocol used when running gh repo fork. Right now it is, in order of descending precedence:

  1. scheme of the forked (upstream) remote
  2. git_protocol for the host
  3. Globally configured git_protocol.

Instead, this should be:

  1. git_protocol for the host
  2. Globally configured git_protocol
  3. scheme of the forked (upstream) remote
  4. Default config option.

Alternatively an extra --protocol flag might work though that makes git_protocol even more obsolete for the forking case.

Implementation

Upon very quick examination of the configuration stack it seems like that is not currently able to tell whether a retrieved config option is the default or explicitly configured by the user. That complicates the case above as only an explicitly configured git_protocol should take precedence over the forked URL scheme.

Activity

  1. mislav commented on Jan 1, 2021

    @mislav
    Contributor

    @MarijnS95 Thanks for detailing your use-case. Some people have asked to be able to have configuration for the preferred clone protocol be separate from the configuration for the preferred push protocol. It sounds like that is what you would benefit from as well. Do you agree?

  2. MarijnS95 commented on Jan 1, 2021

    @MarijnS95
    Author

    @mislav If the configured, preferred clone protocol is not overwritten by the configured pull url of the remote that is being forked, then yes :)

  3. driesvints commented on Mar 4, 2021

    @driesvints

    I'm coming here with the same reasoning for gh repo create. I've configured git_protocol to ssh but gh repo create adds the remote with the https protocol. That doesn't seems to make sense to me? At the very least as @MarijnS95 suggests we should have a way of controlling this with a flag or so.

  4. MarijnS95 commented on Mar 4, 2021

    @MarijnS95
    Author

    @driesvints Yeah, a bump to this issue would be nice. Reordering the precedence as per my suggestion is trivial. I have in fact forgotten about this open issue entirely after patching it locally and continuing my day.

    I could perhaps submit that as as a draft PR?

  5. driesvints commented on Mar 4, 2021

    @driesvints

    @MarijnS95 that would be amazing. Thanks for that!

  6. driesvints commented on Mar 4, 2021

    @driesvints

    I need to retract my phrasing. It seems that gh repo create does respect the git_protocol settings. Apologies, I should have double checked this.

    What I'm actually looking for is for a way to force the protocol to be ssh since https prompts people for their password/username. That leads to issues with automation. So a --protocol flag would be handy here.

  7. vilmibm commented on Mar 4, 2021

    @vilmibm
    Contributor

    What I'm actually looking for is for a way to force the protocol to be ssh since https prompts people for their password/username. That leads to issues with automation. So a --protocol flag would be handy here.

    Sorry for my confusion; if git_protocol is indeed being respected, how does the --protocol flag help?

  8. mislav commented on Mar 5, 2021

    @mislav
    Contributor

    What I'm actually looking for is for a way to force the protocol to be ssh

    If you prefer the ssh protocol across the board, then you can flip the global config:

    gh config set git_protocol ssh -h github.com
    

    since https prompts people for their password/username. That leads to issues with automation.

    You can set up git so that gh is the credential helper, authenticating pulls and pushes to https remotes without the need to interactively supply the password. Run this:

    git config --global credential.https://github.com.helper '!/path/to/bin/gh auth git-credential'

    In fact, this is what GitHub CLI (since recently) sets up for you if you go through the gh auth login flow and choose "HTTPS" as the preferred protocol.

  9. driesvints commented on Mar 5, 2021

    @driesvints

    Sorry for my confusion; if git_protocol is indeed being respected, how does the --protocol flag help?

    Because the code I'm trying to run is part of an automation. We don't have control over how people have set up their environment. They most likely have an ssh key set up with GitHub and the "gh" cli tool installed. https invokes a username/password prompt which halts the automation. ssh is seamlessly.

    If you prefer the ssh protocol across the board, then you can flip the global config:

    We can't make that part of the automation because people on their devices might now want that to be the default. We just want to invoke the ssh protocol for this specific command.

    You can set up git so that gh is the credential helper

    That's great, I didn't realise you could do that. But again, we can't control how people have set up their environment. This is however, a good note to make in our installer docs. At least that way we can make the automation seamlessly with https as well.

    Hope I made my use case clear. If you need any more info feel free to ask! 🙂

  10. mislav commented on Mar 5, 2021

    @mislav
    Contributor

    Hope I made my use case clear. If you need any more info feel free to ask! 🙂

    Sure, thanks for the feedback! Could you open the request for per-invocation --protocol flag in a separate issue, since we've gone slightly off-topic compared to the original post in this thread? Thank you 🙏

  11. driesvints commented on Mar 5, 2021

    @driesvints

    Done! #3088. Sorry for hijacking this thread.

  12. vilmibm commented on Mar 5, 2021

    @vilmibm
    Contributor

    ah, when i heard automation i thought of a fully controlled CI environment and not end user machines. thanks for explaining, makes sense!

  13. added a commit that references this issue on Mar 5, 2021
    19094d3
  14. added
    coreThis issue is not accepting PRs from outside contributors
    and removed
    more-info-neededMore info needed from user/contributor
    on Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreThis issue is not accepting PRs from outside contributorsenhancementa request to improve CLI

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions