Git:Branch Author List

Whether your team uses the long-running, topic, or any other multi-branch branching workflows, you usually end up with many server-side "abandonded" branches that haven’t been commited to in a while, especially when the team is relatively agile. While a cluttered server-side branch set it isn’t always a pressing issue, the difficulty in cleanup can make it be a long-standing issue; one that gets worse and worse as time goes by.

Enter, the git-branch-authors.sh script.

I wrote this script because my team has the problem I described above. To preface the source code though, git doesn’t track who created a branch. It just tracks at which commit reference the branch was forked from its parent, which means we can’t actually tell who created a given branch. However, since a branch is usually commited to by its creator, we can make an educated guess by using the name of the person who commited most recently. At the very least, the most recent author will give a point of contact to help find out information about the branch.

#!/usr/bin/env bash

# Verify we're inside a git repo
git status 2>/dev/null 1>/dev/null
if [[ $? != 0 ]]; then
  echo "Error: '$(pwd)' is not a epository."
  exit 1
fi

# Set the column headers
out='Unix Timestamp~Branch~Timestamp~Commit~Author~Relative Time'

# Parse the branches
for i in $(git branch -r | grep -v HEAD); do
  format="unix:%at~${i}~%ai~%h~%an <%ae>~commited %ar"
  cmd=$(git show "${i}" --format="${format}" | head -n 1)
  out=${out}'\n'${cmd}
done

# Output the goodness
echo -e ${out} | sort -r -n | column -s '~' -t

To use this, simply save it to your /bin directory (ensure your PATH variable has \/bin in it or that won’t work) and chmod \+x ~/bin/git-branch-authors.sh.

Category:Bash Category:Scripts Category:Git