Skip to content

Instantly share code, notes, and snippets.

@dahart
Created October 8, 2019 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dahart/5e70702242ac23bc78b30d5a4c073d12 to your computer and use it in GitHub Desktop.
Save dahart/5e70702242ac23bc78b30d5a4c073d12 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o errexit # exit if a command fails
set -o nounset # exit if an unset variable is used
set -o pipefail # inherit exit code of any command in the pipe
#set -o xtrace # enable trace debugging
# create a color escape sequence from an r, g, b triplet
# $1, $2, $3 (r, g, b) must be in the range [0..5]
color() { echo -e "\x1b[38;5;$(( 16 + $1 * 36 + $2 * 6 + $3 ))m"; }
color_reset() { echo -e "\x1b[0m"; }
# git colors (override with shell export)
: ${GIT_COLOR_HASH:=$(color 3 3 0)}
: ${GIT_COLOR_DATE:=$(color 3 0 0)}
: ${GIT_COLOR_AUTHOR:=$(color 0 2 3)}
: ${GIT_COLOR_DECORATION:=$(color 0 3 0)}
: ${GIT_COLOR_SUBJECT:=$(color_reset)}
: ${GIT_COLOR_RESET:=$(color_reset)}
# field length variables
: ${COLUMNS:=100} # number of columns in the window (override with shell export)
hash_size=10 # max length of the hash
date_size=5 # max length of the date field
author_size=10 # max length of the author field
gap_size=1 # gap between fields
previous_decoration=
# get current day (mm-dd) and year (yyyy) - match git log date format
isodate=$(date +%Y-%m-%d)
today_mmdd=${isodate#*-}
today_yyyy=${isodate%%-*}
# format the log so we can tear it apart
git log --pretty="#^%H^%ci^%aN^%d^%s" "${@}" |
# format it like a ^barbarian
while IFS='^' read -r mark hash commitdate author decoration subject; do
# # ignore any output except specific format
# [[ $mark != *# ]] && continue;
# massage the mark (it may contain the graph)
mark="${mark%#*}"
mark_size=${#mark}
# massage the author string
author="${author% (*}" # drop "(Studio)" suffix
# massage commitdate string
# HH:MM if today
# mm-dd if this year
# mm/yy otherwise
commitdate_yyyy="${commitdate%%-*}"
if [[ $today_yyyy != $commitdate_yyyy ]]; then
commitdate_yy="${commitdate_yyyy#??}"
commitdate_mm="${commitdate%% *}"
commitdate_mm="${commitdate_mm#*-}"
commitdate_mm="${commitdate_mm%-*}"
commitdate="${commitdate_mm}/${commitdate_yy}"
else
commitdate="${commitdate%:*}" # remove seconds and gmt offset
commitdate="${commitdate#*-}" # remove yyyy-
commitdate="${commitdate/${today_mmdd} /}" # remove mm-dd (if today), leaving HH:MM
commitdate="${commitdate% *}" # remove HH:MM, leaving mm-dd
fi
# massage decoration string
decoration="${decoration%$previous_decoration}" # remove last decoration (drop dupes)
decoration_size=${#decoration} # size of the decoration (including space)
decoration="${decoration:1}" # remove the leading space
# compute size left for the subject
size=$(( $COLUMNS - $mark_size - $hash_size - $date_size - $author_size - $decoration_size - $gap_size * 3 ))
size=$(( $size < 0 ? 0 : $size ))
# chop fields down
hash="${hash:0:$hash_size}"
author="${author:0:$author_size}"
subject="${subject:0:${size}}"
# print the format with color
printf "%s" "$mark"
printf "%b%-${hash_size}s%${gap_size}s" "${GIT_COLOR_HASH}" "$hash" " "
printf "%b%-${date_size}s%${gap_size}s" "${GIT_COLOR_DATE}" "$commitdate" " "
printf "%b%-${author_size}s%${gap_size}s" "${GIT_COLOR_AUTHOR}" "$author" " "
${decoration:+printf "%b%s " "${GIT_COLOR_DECORATION}" "$decoration"}
printf "%b%s" "${GIT_COLOR_SUBJECT}" "$subject"
printf "%b\n" "${GIT_COLOR_RESET}"
previous_decoration="$decoration"
done |
# drop into the pager
exec ${GIT_PAGER:-${PAGER}}
@edgewood
Copy link

edgewood commented Oct 8, 2019

This is great! I'd like to modify it a little and maybe distribute it to my team. Is it available under a FOSS license?

@dahart
Copy link
Author

dahart commented Oct 8, 2019

Go for it. Consider it public domain. It was written by a friend and me. FWIW, I've been invoking it like this by default for many years: alias gtl='git tiny-log -15'. It also passes extra args on to git log, so you can do things like gtl --graph, if you want.

@zkSNARK
Copy link

zkSNARK commented Oct 21, 2019

Do you just put this script in /usr/bin/env and then make the alias in your .bashrc file? Also, Is the "Consider it public domain" enough for formalities? I'd love to see a license added in a comment inside the script.

@zkSNARK
Copy link

zkSNARK commented Oct 21, 2019

@edgewood

This is great! I'd like to modify it a little and maybe distribute it to my team. Is it available under a FOSS license?

Curious as to what modifications you would make.

@edgewood
Copy link

edgewood commented Nov 9, 2019

@zkSNARK

Curious as to what modifications you would make.

Making the field length variables configurable.

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