lazygit_branch_pruner.zsh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/zsh
  2. if [[ -z "$1" ]]; then exit 0; fi;
  3. git fetch
  4. git remote prune origin &>/dev/null
  5. ACTION=$1
  6. CURRENT_USER_EMAIL=""
  7. if [[ "true" == "$2" ]]; then CURRENT_USER_EMAIL=$(git config user.email); fi;
  8. MAIN_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
  9. TARGET_BRANCHES=$(
  10. {
  11. git branch -vv | grep 'origin/.*: gone]';
  12. git branch -vv | grep '\[origin/.*\]' | grep -v 'ahead\|behind';
  13. } |
  14. awk '{print $1}' | sort -u | while read branch; do
  15. if [[ "$branch" == "$MAIN_BRANCH" ]]; then
  16. continue;
  17. elif [[ -z $CURRENT_USER_EMAIL ]]; then
  18. echo "$branch"
  19. elif git log --format='%ae' "${MAIN_BRANCH}..${branch}" | grep -v -F -q "$CURRENT_USER_EMAIL"; then
  20. echo "$branch"
  21. fi;
  22. done
  23. )
  24. case "$ACTION" in
  25. print)
  26. echo "$TARGET_BRANCHES" | while read branch; do
  27. if [[ -z "$branch" ]]; then continue; fi;
  28. if [[ "*" == "$branch" ]]; then; continue; fi
  29. info=$(git log -1 --format='%cr|%s' "$branch")
  30. ahead=$(git rev-list --count "$MAIN_BRANCH..$branch")
  31. echo "$branch | +$ahead | $info"
  32. done | column -t -s '|'
  33. ;;
  34. nuke)
  35. if [[ -z "$CURRENT_USER_EMAIL" ]]; then exit 1; fi;
  36. echo "$TARGET_BRANCHES" | xargs git branch -D
  37. ;;
  38. esac