git-extras icon indicating copy to clipboard operation
git-extras copied to clipboard

Add a flag to "git delete-squashed-branches" to not abort on first error

Open nkiesel opened this issue 2 years ago • 2 comments

I love this command, but I often have squashed branches which cannot be deleted because they are checked out locally in a different worktree. And because the script immediately aborts in this situation, it will not try to delete other squashed branches.

I created a local copy which simply replaces git branch -D "$branch" with git branch -D "$branch" || true, but I could file a PR which instead uses a flag for this if you are interested. or is there a reason to not do that?

nkiesel avatar Feb 12 '24 20:02 nkiesel

something like the following (do not like the flag name):

--- /opt/homebrew/bin/git-delete-squashed-branches	2023-10-29 03:52:56
+++ /Users/norbert.kiesel/bin/git-delete-squashed-branches	2024-02-12 12:51:33
@@ -37,6 +37,17 @@

 set -euo pipefail

+try-next() {
+  false
+}
+
+if [[ $# -gt 0 && "$1" == "--all" ]]; then
+    try-next() {
+      true
+    }
+    shift
+fi
+
 if [[ $# -eq 0 ]]; then
   targetBranch=$(git rev-parse --abbrev-ref HEAD)
 else
@@ -48,6 +59,6 @@
     mergeBase=$(git merge-base "$targetBranch" "$branch")

     if [[ $(git cherry "$targetBranch" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
-        git branch -D "$branch"
+        git branch -D "$branch" || try-next
     fi
 done

nkiesel avatar Feb 12 '24 20:02 nkiesel

PR is welcome! What name do you want to use for this new flag?

spacewander avatar Feb 13 '24 05:02 spacewander

Done

spacewander avatar Apr 16 '24 08:04 spacewander