Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active April 5, 2018 00:10
Show Gist options
  • Save havenwood/f2a4d64b2cdaa7c913296f4a9c13ee4f to your computer and use it in GitHub Desktop.
Save havenwood/f2a4d64b2cdaa7c913296f4a9c13ee4f to your computer and use it in GitHub Desktop.
A bash git command that prints the shasum of your last commit. Just make it executable and put it in your PATH, then `git sha`.
#!/usr/bin/env bash
##
# Use color by default if stdout is a terminal.
if [[ -t 1 ]]; then
color=1
else
color=0
fi
while (( $# )); do
case "$1" in
# Git redirects --help to the manpage.
-h)
printf "usage: git sum [-h --help] [--no-color]\n"
exit
;;
--no-color)
color=0
shift 1
;;
*)
printf "fatal: unrecognized option: %s\n" "$1" >&2
exit 1
;;
esac
done
checksum=$(git rev-parse HEAD) || exit
if (( color )); then
short_size=$(git config --get core.abbrev || printf "9")
printf "%s\n" "$(tput bold)$(tput setaf 2)${checksum:0:short_size}$(tput setaf 5)${checksum:short_size}"
else
printf "%s\n" "$checksum"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment