summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMarc Cornellà <marc.cornella@live.com>2020-07-11 20:02:06 +0200
committerMarc Cornellà <marc.cornella@live.com>2020-07-11 20:02:06 +0200
commit55a98fc06d9ea9d003683dec5aae6cb6b0130ab0 (patch)
tree6df8a8ec84ec5f43e4b368dea6ae59f36659685c /plugins
parent9cdc2764967f9255fa343e77217d62d2703d5549 (diff)
downloadzsh-55a98fc06d9ea9d003683dec5aae6cb6b0130ab0.tar.gz
zsh-55a98fc06d9ea9d003683dec5aae6cb6b0130ab0.tar.bz2
zsh-55a98fc06d9ea9d003683dec5aae6cb6b0130ab0.zip
git: better algorithm to decide what's the main branch
Fixes #9103 Co-authored-by: Yufan You <ouuansteve@gmail.com>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/git/git.plugin.zsh27
1 files changed, 22 insertions, 5 deletions
diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
index 6cae3dac2..7284cd121 100644
--- a/plugins/git/git.plugin.zsh
+++ b/plugins/git/git.plugin.zsh
@@ -25,13 +25,30 @@ function work_in_progress() {
fi
}
-# Check if main exists and use instead of master
+# Get the default 'main' branch
+# Marc(2020-07-11): I hope to be able to remove this someday
function git_main_branch() {
- if [[ -n "$(git branch --list master)" ]]; then
- echo master
- else
- echo main
+ # Get default branch from the origin remote
+ local branch
+ branch="${$(command git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null)#refs/remotes/origin/}"
+
+ if [[ -n "$branch" ]]; then
+ echo "$branch"
+ return
fi
+
+ # Look up list of local branches and return the first one that exists
+ local -a branches
+ branches=(${(@f)"$(command git for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)"})
+ for branch in master main; do
+ if (( ${branches[(Ie)$branch]} )); then
+ echo "$branch"
+ return
+ fi
+ done
+
+ echo master
+ return 1
}
#