From 65dbf9e26706bc54b77ba7452039d899aade93d6 Mon Sep 17 00:00:00 2001 From: ElisarEisenbach Date: Tue, 10 Jun 2025 19:14:47 +0300 Subject: [PATCH] Update git.plugin.zsh refactor: use symbolic-ref to determine default branch Replaced manual scanning of possible main branch names with `git symbolic-ref refs/remotes/origin/HEAD` for a more reliable and modern approach to detecting the repository's default branch. Includes fallback to traditional branch name checks if the symbolic-ref fails, ensuring backward compatibility. --- plugins/git/git.plugin.zsh | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index 1d043da35..ca2753e0b 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -31,19 +31,25 @@ function git_develop_branch() { return 1 } -# Check if main exists and use instead of master -function git_main_branch() { +unction git_main_branch() { command git rev-parse --git-dir &>/dev/null || return + local ref - 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 + ref=$(git symbolic-ref --quiet refs/remotes/origin/HEAD 2>/dev/null) + if [[ $? -eq 0 && -n "$ref" ]]; then + echo "${ref##*/}" + return + fi + + # Fallback if origin/HEAD is not set + for branch in main master trunk default stable; do + if git show-ref -q --verify "refs/heads/$branch"; then + echo "$branch" + return fi done - # If no main branch was found, fall back to master but return error - echo master + echo "master" # Default fallback return 1 }