mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-23 02:35:38 +01:00
refactor(git): improve default branch detection in git_main_branch function
Reorganized the `git_main_branch` function to first attempt to retrieve the default branch from remote HEAD symbolic refs for both `origin` and `upstream`. This change enhances the reliability of branch detection by prioritizing symbolic references before falling back to common branch names. The function still defaults to "master" if no main branch is found, maintaining backward compatibility.
This commit is contained in:
parent
908fdd0fa3
commit
86977d2bdf
1 changed files with 9 additions and 8 deletions
|
|
@ -36,18 +36,19 @@ function git_main_branch() {
|
|||
command git rev-parse --git-dir &>/dev/null || return
|
||||
|
||||
local remote ref
|
||||
for remote in origin upstream; do
|
||||
ref=$(command git symbolic-ref --quiet refs/remotes/$remote/HEAD 2>/dev/null)
|
||||
if [[ -n $ref ]]; then
|
||||
echo ${ref#refs/remotes/$remote/}
|
||||
|
||||
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: search for common main branch names in order of preference
|
||||
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}
|
||||
# Fallback: try to get the default branch from remote HEAD symbolic refs
|
||||
for remote in origin upstream; do
|
||||
ref=$(command git symbolic-ref --quiet refs/remotes/$remote/HEAD 2>/dev/null)
|
||||
if [[ -n $ref ]]; then
|
||||
echo ${ref#refs/remotes/$remote/}
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue