From 86eba8d87be8ffa0a2e26483f02843e34daf85cd Mon Sep 17 00:00:00 2001 From: d0zingcat Date: Thu, 26 Feb 2026 14:17:39 +0800 Subject: [PATCH] zsh: make git_clean auto-detect base branch --- .zshrc | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/.zshrc b/.zshrc index f32ced0..e7ff532 100644 --- a/.zshrc +++ b/.zshrc @@ -158,16 +158,31 @@ function rsync_work() { } function git_clean() { - if [[ $# != 1 || ! $1 =~ 'main|master|develop' ]] - then - echo 'Invalid parameter, should based on develop/main/master' - else - git fetch --all --prune - git checkout $1 && \ - git config pull.rebase false && \ - git pull && \ - git branch --merged | grep -v $1 | cat | xargs git branch -d + git fetch --all --prune + + base_branch="$1" + if [[ -z "$base_branch" ]]; then + base_branch=$(git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null | sed 's#^origin/##') fi + + if [[ -z "$base_branch" ]]; then + for branch in main master develop; do + if git show-ref --verify --quiet "refs/heads/$branch" || git show-ref --verify --quiet "refs/remotes/origin/$branch"; then + base_branch="$branch" + break + fi + done + fi + + if [[ -z "$base_branch" ]]; then + echo 'Could not detect base branch. Pass it explicitly, e.g. git_clean main' + return 1 + fi + + git checkout "$base_branch" && \ + git config pull.rebase false && \ + git pull && \ + git branch --merged | grep -v " $base_branch$" | cat | xargs git branch -d } function git_config() {