#!/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