Skip to content

perf(git): improve git_main_branch() to use remote HEAD symbolic ref #13212

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ElisarEisenbach
Copy link

@ElisarEisenbach ElisarEisenbach commented Jul 13, 2025

Standards checklist:

  • The PR title is descriptive.
  • The PR doesn't replicate another PR which is already open.
  • I have read the contribution guide and followed all the instructions.
  • The code follows the code style guide detailed in the wiki.
  • The code is mine or it's from somewhere with an MIT-compatible license.
  • The code is efficient, to the best of my ability, and does not waste computer resources.
  • The code is stable and I have tested it myself, to the best of my abilities.
  • If the code introduces new aliases, I provide a valid use case for all plugin users down below.

Changes:

Fall back to git symbolic-ref to query the default branch from remote HEAD
after checking common branch names. This approach is more accurate,
and respects Git's own understanding of the default
branch regardless of naming conventions.

refactor: use symbolic-ref to determine default branch

Replaced manual scanning of possible main branch names with
`git symbolic-ref refs/remotes/origin/HEAD` for a more reliable and
modern approach to detecting the repository's default branch.

Includes fallback to traditional branch name checks if the symbolic-ref
fails, ensuring backward compatibility.
…anch detection

Updated the `git_main_branch` function to first check for the default branch using `git symbolic-ref` for both `origin` and `upstream`. If no symbolic reference is found, it now searches for common branch names in a more structured way. The function retains a fallback to "master" while returning an error if no main branch is found, improving reliability and compatibility.
@ohmyzsh ohmyzsh bot added the Area: plugin Issue or PR related to a plugin label Jul 13, 2025
@ohmyzsh ohmyzsh bot added this to Main project Jul 13, 2025
@github-project-automation github-project-automation bot moved this to Backlog in Main project Jul 13, 2025
…nction

Reorganized the `git_main_branch` function to first attempt to retrieve the default branch from remote HEAD symbolic refs for both `origin` and `upstream`. This change enhances the reliability of branch detection by prioritizing symbolic references before falling back to common branch names. The function still defaults to "master" if no main branch is found, maintaining backward compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: plugin Issue or PR related to a plugin
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

1 participant