55a98fc06d9ea9d003683dec5aae6cb6b0130ab0

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

git: better algorithm to decide what's the main branch

Fixes #9103

Co-authored-by: Yufan You <ouuansteve@gmail.com>

Diff

 1diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
 2index 6cae3dac258a722da641e5ee59844a6453e361cd..7284cd121a5065e48569fabecda94b3ea5532cf2 100644
 3--- a/plugins/git/git.plugin.zsh
 4+++ b/plugins/git/git.plugin.zsh
 5@@ -25,13 +25,30 @@ function work_in_progress() {
 6   fi
 7 }
 8 
 9-# Check if main exists and use instead of master
10+# Get the default 'main' branch
11+# Marc(2020-07-11): I hope to be able to remove this someday
12 function git_main_branch() {
13-  if [[ -n "$(git branch --list master)" ]]; then
14-    echo master
15-  else
16-    echo main
17+  # Get default branch from the origin remote
18+  local branch
19+  branch="${$(command git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null)#refs/remotes/origin/}"
20+
21+  if [[ -n "$branch" ]]; then
22+    echo "$branch"
23+    return
24   fi
25+
26+  # Look up list of local branches and return the first one that exists
27+  local -a branches
28+  branches=(${(@f)"$(command git for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)"})
29+  for branch in master main; do
30+    if (( ${branches[(Ie)$branch]} )); then
31+      echo "$branch"
32+      return
33+    fi
34+  done
35+
36+  echo master
37+  return 1
38 }
39 
40 #