2525dae6613652ec9cb572bdc2fdf80ef837a967

Author
ElisarEisenbach <elisar.eisenbach@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(git): use remote default branch to guess main branch (#13212)

Co-authored-by: Carlo Sala <carlosalag@protonmail.com>

Diff

 1diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
 2index 335eaa8d064cb2f416ba27ce106d694add8c459b..830c18939b951b409d2c5331ea9af0ca24e1d44f 100644
 3--- a/plugins/git/git.plugin.zsh
 4+++ b/plugins/git/git.plugin.zsh
 5@@ -31,16 +31,26 @@ function git_develop_branch() {
 6   return 1
 7 }
 8 
 9-# Check if main exists and use instead of master
10+# Get the default branch name from common branch names or fallback to remote HEAD
11 function git_main_branch() {
12   command git rev-parse --git-dir &>/dev/null || return
13-  local ref
14+  
15+  local remote ref
16+  
17   for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,stable,master}; do
18     if command git show-ref -q --verify $ref; then
19       echo ${ref:t}
20       return 0
21     fi
22   done
23+  
24+  # Fallback: try to get the default branch from remote HEAD symbolic refs
25+  for remote in origin upstream; do
26+    ref=$(command git rev-parse --abbrev-ref $remote/HEAD 2>/dev/null)
27+    if [[ $ref == $remote/* ]]; then
28+      echo ${ref#"$remote/"}; return 0
29+    fi
30+  done
31 
32   # If no main branch was found, fall back to master but return error
33   echo master