Skip to content

Instantly share code, notes, and snippets.

@micklove
Created February 9, 2020 02:05
Show Gist options
  • Save micklove/c4f0fb5538e0a280a389b5b3e4f7d52a to your computer and use it in GitHub Desktop.
Save micklove/c4f0fb5538e0a280a389b5b3e4f7d52a to your computer and use it in GitHub Desktop.
git diff file in column mode
#!/usr/bin/env bash
# diff file at given path to committed version.
# Use colordiff, if installed
# e.g. gdiff my-file.c
PREFERRED_DIFFER="colordiff"
command -v ${PREFERRED_DIFFER} >/dev/null 2>&1 || PREFERRED_DIFFER="diff"
if [[ -z "${1}" ]]; then
echo "Usage: ${0} <path to file>"
fi
FILE_TO_DIFF_PATH="${1}"
## Comparison version e.g. HEAD~1, default to 0
VERSION=0
if [[ -n "${2}" ]]; then
VERSION="${2}"
fi
# Must provide path to file in git command, add './' if required
if [[ "$(dirname "${1}")" == "." ]]; then
FILE_TO_DIFF_PATH="./${1}"
fi
printf "\nUsing ${PREFERRED_DIFFER}, on ${FILE_TO_DIFF_PATH}\n\n"
# set the diff column width for the current users setup
CURRENT_COLS=$(tput cols)
${PREFERRED_DIFFER} -yW"${CURRENT_COLS}" <(git show HEAD~${VERSION}:"${FILE_TO_DIFF_PATH}") "${FILE_TO_DIFF_PATH}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment