Skip to content

Instantly share code, notes, and snippets.

@nickolasburr
Last active June 6, 2017 14:12
Show Gist options
  • Save nickolasburr/3cce9591c610b3f2b69aec28ad467ea1 to your computer and use it in GitHub Desktop.
Save nickolasburr/3cce9591c610b3f2b69aec28ad467ea1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# track: Track lifetime changes of a file (includes renames) via formatted diff log
# @todo: Better error handling, combining flags
E_NOTFOUND=85
E_BADARGS=86
FILENAME="${!#}"
# verify we're checking against a valid filename
if [[ ! -f $FILENAME ]]; then
printf 'Usage: track {-a[--all]|-f[--first]|-i[--inline]|-l[--last]|-L[--lines]|-r[--reverse]} filename\n'
return $E_NOTFOUND
fi
# if the only argument is a file name, set the default
# flag value to `--all` and update positional parameters
if [ "$1" == $FILENAME ]; then
set -- "--all" $FILENAME
# if the first argument is `--lines`, verify we're receiving two line numbers
# as the following two arguments ($2 and $3), with the filename as the last argument
elif [ "$1" == "-l" ] || [ "$1" == "--lines" ]; then
E_NUMARGS=4
if [ $# != $E_NUMARGS ]; then
printf 'Wrong number of arguments\nUsage: track --lines start end filename\n'
return $E_BADARGS
else
set -- "--lines" $2 $3 $FILENAME
fi
# special handling for `--last` flag which
# takes a single argument for the last n commits
elif [ "$1" == "-L" ] || [ "$1" == "--last" ]; then
E_NUMARGS=3
# if `--last` is given with no arguments, default to 1
if [ "$2" == $FILENAME ]; then
set -- "--last" 1 $FILENAME
# if `--last` is given with 1 argument, (re)set the positional parameters
elif [ "$3" == $FILENAME ]; then
set -- "--last" $2 $FILENAME
else
printf 'Wrong number of arguments\nUsage: track --last [commits] filename\n'
return $E_BADARGS
fi
# now check the argv count against what's expected
if [ $# != $E_NUMARGS ]; then
printf 'Wrong number of arguments\nUsage: track --last n filename\n'
return $E_BADARGS
fi
elif [ "$1" == "-F" ] || [ "$1" == "--func" ]; then
E_NUMARGS=3
# now check the argv count against what's expected
if [ $# != $E_NUMARGS ]; then
printf 'Wrong number of arguments\nUsage: track --func funcname filename\n'
return $E_BADARGS
fi
fi
# filter flag options accordingly
case $1 in
-a|--all)
git log --color-words --follow \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph --no-merges \
--patch --stat \
$FILENAME
;;
-f|--first)
git log --color-words --diff-filter=A \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph --no-merges \
--patch --stat \
$FILENAME
;;
-F|--func)
git log --color-words --follow \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph -L :$2:$FILENAME \
--no-merges --patch --stat \
$FILENAME
;;
-i|--inline)
git log --follow \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph --no-merges \
--patch --stat \
$FILENAME
;;
-l|--last)
git log --color-words --follow \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph --max-count $2 \
--no-merges --patch --stat \
$FILENAME
;;
-L|--lines)
git log --color-words \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--graph -L $2,$3:$FILENAME \
--no-merges --patch --stat
;;
-r|--reverse)
git log --color-words --follow \
--format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' \
--no-merges --patch \
--reverse --stat \
$FILENAME
;;
*)
printf 'Invalid flag\nUsage: track {-a[--all]|-f[--first]|-i[--inline]|-l[--last]|-L[--lines]|-r[--reverse]} filename\n'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment