From 86977d2bdfa72d9a70e79747858eb4a55ac72304 Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Mon, 14 Jul 2025 10:47:03 +0300 Subject: [PATCH] refactor(git): improve default branch detection in git_main_branch function 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. --- plugins/git/git.plugin.zsh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index bb2583205..4bfabcf29 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -36,18 +36,19 @@ function git_main_branch() { command git rev-parse --git-dir &>/dev/null || return local remote ref - for remote in origin upstream; do - ref=$(command git symbolic-ref --quiet refs/remotes/$remote/HEAD 2>/dev/null) - if [[ -n $ref ]]; then - echo ${ref#refs/remotes/$remote/} + + for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,stable,master}; do + if command git show-ref -q --verify $ref; then + echo ${ref:t} return 0 fi done - # Fallback: search for common main branch names in order of preference - for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,stable,master}; do - if command git show-ref -q --verify $ref; then - echo ${ref:t} + # Fallback: try to get the default branch from remote HEAD symbolic refs + for remote in origin upstream; do + ref=$(command git symbolic-ref --quiet refs/remotes/$remote/HEAD 2>/dev/null) + if [[ -n $ref ]]; then + echo ${ref#refs/remotes/$remote/} return 0 fi done