Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active July 27, 2022 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericboehs/9609fb0312c94e302c2d0eebf2b35384 to your computer and use it in GitHub Desktop.
Save ericboehs/9609fb0312c94e302c2d0eebf2b35384 to your computer and use it in GitHub Desktop.
Lists Stale Branches for Current Repo in CSV or a Markdown Table
#! /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
@ericboehs
Copy link
Author

ericboehs commented May 13, 2022

Install and run:

cd ~/bin
curl -sL https://gist.github.com/ericboehs/9609fb0312c94e302c2d0eebf2b35384/raw/stale-branches > stale-branches
chmod +x stale-branches

cd ~/Code/ericboehs/dotfiles # cd to your repository
# Show stale branches
stale-branches

# Show fresh branches
DATE_COMPARISON_OPERATOR='>' stale-branches

# Output CSV of fresh branches
CSV_ONLY=true DATE_COMPARISON_OPERATOR='>' stale-branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment