24b2600558f999dd53f0d354ad61fc07cf7d6e2b

Author
tobi1805 <66414944+tobi1805@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(git): delete squash-merged branches in `gbda` (#11948)


Diff

 1diff --git a/plugins/git/README.md b/plugins/git/README.md
 2index 9c16cb6e761b6d22fa470138b3d5d89e1618689e..33c10ffd0d49474c13b3098811198e6795b59650 100644
 3--- a/plugins/git/README.md
 4+++ b/plugins/git/README.md
 5@@ -41,7 +41,6 @@ plugins=(... git)
 6 | `gba`                  | `git branch --all`                                                                                                                                                  |
 7 | `gbd`                  | `git branch --delete`                                                                                                                                               |
 8 | `gbD`                  | `git branch --delete --force`                                                                                                                                       |
 9-| `gbda`                 | `git branch --no-color --merged \| command grep -vE "^([+*]\|\s*($(git_main_branch)\|$(git_develop_branch))\s*$)" \| command xargs git branch --delete 2>/dev/null` |
10 | `gbgd`                 | `LANG=C git branch --no-color -vv \| grep ": gone\]" \| awk '"'"'{print $1}'"'"' \| xargs git branch -d`                                                            |
11 | `gbgD`                 | `LANG=C git branch --no-color -vv \| grep ": gone\]" \| awk '"'"'{print $1}'"'"' \| xargs git branch -D`                                                            |
12 | `gbm`                  | `git branch --move`                                                                                                                                                 |
13@@ -251,6 +250,7 @@ receive further support.
14 | `git_develop_branch`     | Returns the name of the “development” branch: `dev`, `devel`, `development` if they exist, `develop` otherwise. |
15 | `git_main_branch`        | Returns the name of the main branch: `main` if it exists, `master` otherwise.                                   |
16 | `grename <old> <new>`    | Renames branch `<old>` to `<new>`, including on the origin remote.                                              |
17+| `gbda`                   | Deletes all merged and squash-merged branches                                                                   |
18 
19 ### Work in Progress (WIP)
20 
21diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
22index cb848b40772f5354a7c8fb68d1231658b9b42cb9..ceb2011f986ac1b27d21dd6b372a8ded33cba9f5 100644
23--- a/plugins/git/git.plugin.zsh
24+++ b/plugins/git/git.plugin.zsh
25@@ -122,7 +122,22 @@ alias gb='git branch'
26 alias gba='git branch --all'
27 alias gbd='git branch --delete'
28 alias gbD='git branch --delete --force'
29-alias gbda='git branch --no-color --merged | command grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | command xargs git branch --delete 2>/dev/null'
30+
31+# Copied and modified from James Roeder (jmaroeder) under MIT License
32+# https://github.com/jmaroeder/plugin-git/blob/216723ef4f9e8dde399661c39c80bdf73f4076c4/functions/gbda.fish
33+function gbda() {
34+  git branch --no-color --merged | command grep -vE "^([+*]|\s*($(git_main_branch)|$(git_develop_branch))\s*$)" | command xargs git branch --delete 2>/dev/null
35+
36+  local default_branch=$(git_main_branch)
37+  git for-each-ref refs/heads/ "--format=%(refname:short)" | \
38+    while read branch; do
39+      local merge_base=$(git merge-base $default_branch $branch)
40+      if [[ '-*' == $(git cherry $default_branch $(git commit-tree $(git rev-parse $branch\^{tree}) -p $merge_base -m _)) ]]; then
41+        git branch -D $branch
42+      fi
43+    done
44+}
45+
46 alias gbgd='LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -d'
47 alias gbgD='LANG=C git branch --no-color -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs git branch -D'
48 alias gbm='git branch --move'