summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElisarEisenbach <elisar.eisenbach@gmail.com>2025-08-26 06:13:21 +0300
committerGitHub <noreply@github.com>2025-08-26 11:13:21 +0800
commit2525dae6613652ec9cb572bdc2fdf80ef837a967 (patch)
tree28f05006b56b492af30c743fc584cc847c972a17
parent266bc17ab3587920a7a98262c78dc5a026583bf3 (diff)
downloadzsh-2525dae6613652ec9cb572bdc2fdf80ef837a967.tar.gz
zsh-2525dae6613652ec9cb572bdc2fdf80ef837a967.tar.bz2
zsh-2525dae6613652ec9cb572bdc2fdf80ef837a967.zip
feat(git): use remote default branch to guess main branch (#13212)
Co-authored-by: Carlo Sala <carlosalag@protonmail.com>
-rw-r--r--plugins/git/git.plugin.zsh14
1 files changed, 12 insertions, 2 deletions
diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
index 335eaa8d0..830c18939 100644
--- a/plugins/git/git.plugin.zsh
+++ b/plugins/git/git.plugin.zsh
@@ -31,16 +31,26 @@ function git_develop_branch() {
return 1
}
-# Check if main exists and use instead of master
+# Get the default branch name from common branch names or fallback to remote HEAD
function git_main_branch() {
command git rev-parse --git-dir &>/dev/null || return
- local ref
+
+ local remote ref
+
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: try to get the default branch from remote HEAD symbolic refs
+ for remote in origin upstream; do
+ ref=$(command git rev-parse --abbrev-ref $remote/HEAD 2>/dev/null)
+ if [[ $ref == $remote/* ]]; then
+ echo ${ref#"$remote/"}; return 0
+ fi
+ done
# If no main branch was found, fall back to master but return error
echo master