Browse Source

feat(lazygit): add branch pruning command

Joe 5 months ago
parent
commit
7670015e05
2 changed files with 66 additions and 0 deletions
  1. 23 0
      .config/lazygit/config.yml
  2. 43 0
      .scripts/lazygit_branch_pruner.zsh

+ 23 - 0
.config/lazygit/config.yml

@@ -164,3 +164,26 @@ customCommands:
         title: "Confirm"
         title: "Confirm"
         body: |
         body: |
           Set author and commit dates for {{.Form.Commit}} commit "{{.SelectedLocalCommit.Name}}" ({{.SelectedLocalCommit.Sha}}) to "{{.Form.Timestamp}}"?'
           Set author and commit dates for {{.Form.Commit}} commit "{{.SelectedLocalCommit.Name}}" ({{.SelectedLocalCommit.Sha}}) to "{{.Form.Timestamp}}"?'
+
+  - key: "<c-d>"
+    context: "localBranches"
+    description: "Clean up a local merged branch"
+    command: git branch -D {{ .Form.Branch }}
+    prompts:
+      - type: "menuFromCommand"
+        title: "Select Local Branch"
+        key: "Branch"
+        command: /usr/bin/env zsh -c "/bin/zsh $DOTFILES_DIR/.scripts/lazygit_branch_pruner.zsh print"
+        filter: '^(\S+)( .*)$'
+        valueFormat: '{{ .group_1 }}'
+        labelFormat: '{{ .group_1 }}{{ .group_2 }}'
+
+  - key: "<c-x>"
+    context: "localBranches"
+    description: "Clean up all local merged branches after pruning"
+    command: /usr/bin/env zsh -c "/bin/zsh $DOTFILES_DIR/.scripts/lazygit_branch_pruner.zsh {{ .Form.Confirmation }} true"
+    prompts:
+      - type: "menuFromCommand"
+        title: "Review Branches to be Nuked"
+        key: "Confirmation"
+        command: /usr/bin/env zsh -c "/bin/zsh $DOTFILES_DIR/.scripts/lazygit_branch_pruner.zsh print true && echo nuke"

+ 43 - 0
.scripts/lazygit_branch_pruner.zsh

@@ -0,0 +1,43 @@
+#!/bin/zsh
+
+if [[ -z "$1" ]]; then exit 0; fi;
+
+git fetch
+git remote prune origin &>/dev/null
+
+ACTION=$1
+CURRENT_USER_EMAIL=""
+if [[ "true" == "$2" ]]; then CURRENT_USER_EMAIL=$(git config user.email); fi;
+MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
+
+TARGET_BRANCHES=$(
+    {
+        git branch -vv | grep 'origin/.*: gone]';
+        git branch -vv | grep '\[origin/.*\]' | grep -v 'ahead\|behind';
+    } |
+    awk '{print $1}' | sort -u | while read branch; do
+        if [[ "$branch" == "$MAIN_BRANCH" ]]; then
+            continue;
+        elif [[ -z $CURRENT_USER_EMAIL ]]; then
+            echo "$branch"
+        elif git log --format='%ae' "${MAIN_BRANCH}..${branch}" | grep -v -F -q "$CURRENT_USER_EMAIL"; then
+            echo "$branch"
+        fi;
+    done
+)
+
+case "$ACTION" in
+    print)
+        echo "$TARGET_BRANCHES" | while read branch; do
+            if [[ -z "$branch" ]]; then continue; fi;
+            if [[ "*" == "$branch" ]]; then; continue; fi
+            info=$(git log -1 --format='%cr|%s' "$branch")
+            ahead=$(git rev-list --count "$MAIN_BRANCH..$branch")
+            echo "$branch | +$ahead | $info"
+        done | column -t -s '|'
+        ;;
+    nuke)
+        if [[ -z "$CURRENT_USER_EMAIL" ]]; then exit 1; fi;
+        echo "$TARGET_BRANCHES" | xargs git branch -D
+        ;;
+esac