mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-30 02:44:42 +01:00
refactor(git): enhance git_main_branch function for better default branch detection
Updated the `git_main_branch` function to first check for the default branch using `git symbolic-ref` for both `origin` and `upstream`. If no symbolic reference is found, it now searches for common branch names in a more structured way. The function retains a fallback to "master" while returning an error if no main branch is found, improving reliability and compatibility.
This commit is contained in:
parent
65dbf9e267
commit
908fdd0fa3
1 changed files with 20 additions and 16 deletions
|
|
@ -31,25 +31,29 @@ function git_develop_branch() {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
unction git_main_branch() {
|
# Get the default branch name from remote HEAD or fallback to common branch names
|
||||||
|
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
|
||||||
ref=$(git symbolic-ref --quiet refs/remotes/origin/HEAD 2>/dev/null)
|
for remote in origin upstream; do
|
||||||
if [[ $? -eq 0 && -n "$ref" ]]; then
|
ref=$(command git symbolic-ref --quiet refs/remotes/$remote/HEAD 2>/dev/null)
|
||||||
echo "${ref##*/}"
|
if [[ -n $ref ]]; then
|
||||||
return
|
echo ${ref#refs/remotes/$remote/}
|
||||||
fi
|
return 0
|
||||||
|
fi
|
||||||
# Fallback if origin/HEAD is not set
|
done
|
||||||
for branch in main master trunk default stable; do
|
|
||||||
if git show-ref -q --verify "refs/heads/$branch"; then
|
# Fallback: search for common main branch names in order of preference
|
||||||
echo "$branch"
|
for ref in refs/{heads,remotes/{origin,upstream}}/{main,trunk,mainline,default,stable,master}; do
|
||||||
return
|
if command git show-ref -q --verify $ref; then
|
||||||
|
echo ${ref:t}
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "master" # Default fallback
|
# If no main branch was found, fall back to master but return error
|
||||||
|
echo master
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -433,4 +437,4 @@ for old_alias new_alias (
|
||||||
print -Pu2 \"%F{yellow}[oh-my-zsh] '%F{red}${old_alias}%F{yellow}' is a deprecated alias, using '%F{green}${new_alias}%F{yellow}' instead.%f\"
|
print -Pu2 \"%F{yellow}[oh-my-zsh] '%F{red}${old_alias}%F{yellow}' is a deprecated alias, using '%F{green}${new_alias}%F{yellow}' instead.%f\"
|
||||||
$new_alias"
|
$new_alias"
|
||||||
done
|
done
|
||||||
unset old_alias new_alias
|
unset old_alias new_alias
|
||||||
Loading…
Add table
Add a link
Reference in a new issue