tj.git-extras/bin/git-delete-squashed-branches
Edwin Kofler 52897f73b9
Add --proceed flag to git-delete-squashed-branches (#1134) (#1135)
Co-authored-by: Norbert Kiesel <nk@iname.com>
2024-02-20 10:47:36 +08:00

29 lines
716 B
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
proceed=false
if [[ $# -gt 0 && ("$1" == "--proceed" || "$1" == "-p") ]]; then
proceed=true
shift
fi
if [[ $# -eq 0 ]]; then
targetBranch=$(git rev-parse --abbrev-ref HEAD)
else
targetBranch=$1
git checkout "$targetBranch"
fi
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
mergeBase=$(git merge-base "$targetBranch" "$branch")
if [[ $(git cherry "$targetBranch" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
if [[ $proceed == true ]]; then
git branch -D "$branch" || true
else
git branch -D "$branch"
fi
fi
done