mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2025-12-06 15:30:40 +01:00
feat(git): use remote default branch to guess main branch (#13212)
Co-authored-by: Carlo Sala <carlosalag@protonmail.com>
This commit is contained in:
@@ -31,10 +31,12 @@ function git_develop_branch() {
|
|||||||
return 1
|
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() {
|
function git_main_branch() {
|
||||||
command git rev-parse --git-dir &>/dev/null || return
|
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
|
for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,stable,master}; do
|
||||||
if command git show-ref -q --verify $ref; then
|
if command git show-ref -q --verify $ref; then
|
||||||
echo ${ref:t}
|
echo ${ref:t}
|
||||||
@@ -42,6 +44,14 @@ function git_main_branch() {
|
|||||||
fi
|
fi
|
||||||
done
|
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
|
# If no main branch was found, fall back to master but return error
|
||||||
echo master
|
echo master
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
Reference in New Issue
Block a user