Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pr checkout, cannot set up tracking info #9497

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

muzimuzhi
Copy link
Contributor

@muzimuzhi muzimuzhi commented Aug 21, 2024

Fixes #4287

Briefly, gh pr checkout <pr> [--branch LOCAL_BRANCH] works in two steps:

  1. fetch the head branch of <pr> by running git fetch REMOTE +refs/heads/BRANCH:refs/remotes/REMOTE/BRANCH
  2. checkout the fetched branch by running git checkout -b LOCAL_BRANCH --track REMOTE/BRANCH

What #4287 reported is that when the fetch refs/remotes/REMOTE/BRANCH is not covered in remote.REMOTE.fetch git config, then the second step failed with

fatal: cannot set up tracking information; starting point 'REMOTE/BRANCH' is not a branch
failed to run git: exit status 128

This PR solves this problem by running1

git config --add remote.REMOTE.fetch +refs/heads/BRANCH:refs/remotes/REMOTE/BRANCH

if local branch REMOTE/BRANCH doesn't exist after the first step.

Note that remote.<name>.fetch is a multi-valued Git config, though not clearly documented.

Note also that an old syntax git config --add <name> <value> is used to gain compatibility with old Git versions. Since Git 2.26.0, git config learnt some new subcommands and options, see the section Deprecated modes in git config doc.

TODO

  • add some code comment
  • add new test; Need help mocking a restricted remote.origin.fetch config in test.

Footnotes

  1. Just realized that it does exactly the same as git remote set-branches --add origin mybranch mentioned by @mislav in https://github.com/cli/cli/issues/4287#issuecomment-915112505

@muzimuzhi muzimuzhi requested a review from a team as a code owner August 21, 2024 08:35
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Aug 21, 2024
@cliAutomation cliAutomation added this to Needs review 🤔 in The GitHub CLI Aug 21, 2024
@muzimuzhi
Copy link
Contributor Author

muzimuzhi commented Aug 22, 2024

Explanation is added to the initial comment.

I just found the git fetch option --refmap=<refspec> which might give a more elegant solution, without polluting remote.REMOTE.fetch config.

Update: Didn't succeed.

@muzimuzhi muzimuzhi changed the title Fix pr checkout, non-branch starting point Fix pr checkout, cannot set up tracking info Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
The GitHub CLI
  
Needs review 🤔
Development

Successfully merging this pull request may close these issues.

PR checkout no longer works for branches not configured to be fetched from the remote
2 participants