Lists Stale Branches for Current Repo in CSV or a Markdown Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env zsh | |
# Variables | |
DATE_COMPARISON_OPERATOR=${DATE_COMPARISON_OPERATOR-<} | |
REPO_URL=${REPO_URL-$(gh browse -n)} | |
# Set generating command | |
GENERATING_COMMAND='stale-branches' | |
if [ "$DATE_COMPARISON_OPERATOR" = ">" ]; then | |
GENERATING_COMMAND="DATE_COMPARISON_OPERATOR='>' $GENERATING_COMMAND" | |
fi | |
if [ "$REPO_URL" != "$(gh browse -n)" ]; then | |
GENERATING_COMMAND="REPO_URL='$REPO_URL' $GENERATING_COMMAND" | |
fi | |
# Detect system compatiability | |
if [ -z ${CSV_ONLY+x} ]; then | |
command -v csv2md >/dev/null 2>&1 || { | |
echo >&2 "Could not find csv2md in \$PATH. Please run \`gem install csv2md\` and try again."; exit 1 | |
} | |
fi | |
uname -a | grep -q Darwin || { | |
echo >&2 "BSD date is required. Please modify script to support GNU date. Aborting."; exit 1 | |
} | |
git fetch --all # Ensure all remote branches/commits are present | |
function generate_csv() { | |
echo "Last Commit,SHA,Branch,Author" && # Print CSV Header | |
git branch -a --sort=committerdate --format='%(committerdate:short),\[%(objectname:short)]('$REPO_URL'/commit/%(objectname)),\[%(refname:short)]('$REPO_URL'/branches/all?query=%(refname:short)),%(authorname)' | # Print every branch name,branch_link,author as CSV rows in order of commit date (includes \[ link hack) | |
awk '{ if ($1 '$DATE_COMPARISON_OPERATOR' "'$(date -v-6m +%Y-%m-%d)'") { print $0 } }' | # Do/Don't print branches updated in the last 6 months | |
sed 's/origin\///g' | # Clean-up branch names | |
sed 's/\,\\\[/\,\[/g' # Remove \[ link hack | |
} | |
if [ "$CSV_ONLY" = "true" ]; then | |
generate_csv | |
else | |
generate_csv | { | |
csv2md && # Convert to MarkDown Table | |
echo "\nGenerated on $(date +%Y-%m-%d) by @$USER via ["'`'"$GENERATING_COMMAND"'`'"](https://gist.github.com/ericboehs/9609fb0312c94e302c2d0eebf2b35384)." | |
} | tee >(pbcopy) # Print output and place on clipboard | |
echo "\nOutput has been saved to your clipboard." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install and run: