From 7b26707fca1b236d06c13a1368a5fb6559636b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Wed, 15 Jul 2020 17:02:49 +0200 Subject: [PATCH] git: go back to previous main branch detection logic Fixes #9114 --- plugins/git/README.md | 18 +++++++++--------- plugins/git/git.plugin.zsh | 26 +++++--------------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/plugins/git/README.md b/plugins/git/README.md index b92d0bf0..b7f21681 100644 --- a/plugins/git/README.md +++ b/plugins/git/README.md @@ -183,8 +183,8 @@ plugins=(... git) Following the recent push for removing racially-charged words from our technical vocabulary, the git plugin favors using a branch name other than `master`. In this case, we favor the shorter, neutral and descriptive term `main`. This means -that any aliases and functions that previously used `master`, will use `main` if `master` doesn't exist. We do this via -the function `git_main_branch`. +that any aliases and functions that previously used `master`, will use `main` if that branch exists. We do this via the +function `git_main_branch`. ### Deprecated aliases @@ -208,13 +208,13 @@ These are aliases that have been removed, renamed, or otherwise modified in a wa ### Current -| Command | Description | -|:-----------------------|:---------------------------------------------------------------------------------------| -| `grename ` | Rename `old` branch to `new`, including in origin remote | -| current_branch | Return the name of the current branch | -| git_current_user_name | Returns the `user.name` config value | -| git_current_user_email | Returns the `user.email` config value | -| git_main_branch | Returns the name of the main branch: from origin or from local branches, else 'master' | +| Command | Description | +|:-----------------------|:-----------------------------------------------------------------------------| +| `grename ` | Rename `old` branch to `new`, including in origin remote | +| current_branch | Return the name of the current branch | +| git_current_user_name | Returns the `user.name` config value | +| git_current_user_email | Returns the `user.email` config value | +| git_main_branch | Returns the name of the main branch: `main` if it exists, `master` otherwise | ### Work in Progress (WIP) diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh index 8f8edcd2..5a9ccff5 100644 --- a/plugins/git/git.plugin.zsh +++ b/plugins/git/git.plugin.zsh @@ -25,29 +25,13 @@ function work_in_progress() { fi } -# Get the default 'main' branch: from origin, from local branches, or else just 'master' +# Check if main exists and use instead of master function git_main_branch() { - # 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 + if [[ -n "$(git branch --list main)" ]]; then + echo main + else + echo master 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 } #