Skip to content

Instantly share code, notes, and snippets.

@nitingupta910
Created March 11, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitingupta910/11e4a202b2dc25cf280b6f584d6e4e38 to your computer and use it in GitHub Desktop.
Save nitingupta910/11e4a202b2dc25cf280b6f584d6e4e38 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##
# git-diffc: Graphical Git diff viewer
# 'c' -> comprehensive :)
# Author: Nitin Gupta <nitin@nitingupta.dev>
# Last Modified (mm-dd-yy): 03/11/2020
#
##
# Usage:
# git-diffc [usual git diff arguments]
#
# This script recreates (sparse) trees containing original
# and modified files. These trees are given to the specified
# diff viewer which should be able to handle arguments like:
# $DIFF_BIN <olddir> <newdir>
#
# Following diff viewers are known to handle such input:
# meld, kdiff3, kompare
#
##
# Config:
# DIFF_BIN: diff viewer (default: kdiff3)
# CLEANUP: remove temporary files on exit (default: 1)
# TMPDIR: directory for dumping diff tree (default: /tmp)
#
DIFF_BIN="${DIFF_BIN-`which meld`}"
CLEANUP="${CLEANUP-"1"}"
TMPDIR="${TMPDIR-"/tmp"}"
[ -x "${DIFF_BIN}" ] || {
echo "Could not find diff viewer [${DIFF_BIN}]. Exiting." 1>&2
exit 1;
}
function showDiffs {
echo "SOURCE: ${TMP}"
"${DIFF_BIN}" "${TMP}/old" "${TMP}/new" 2>&1
}
function cleanup {
# remove empty list.txt file
rm ${TMP}/list.txt
[ "${CLEANUP}" -eq "1" ] || exit 1;
#echo "deleting: ${TMP}"
rm -rf "${TMP}"
}
function setupTempDirs {
TMP="${TMPDIR}/.git_diffc"
TMP="${TMP}/diff.$RANDOM"
# echo "TMP=${TMP}"
[ ! -d "${TMP}" ] || {
echo "Temp directory [${TMP}] already exists! Exiting." 1>&2
exit 1
}
mkdir -p "${TMP}" || {
echo "Could not create temporary directory! Exiting." 1>&2
exit 1
}
}
if [ -n "${GIT_EXTERNAL_DIFF}" ]; then
[ "${GIT_EXTERNAL_DIFF}" = "${0}" ] || {
echo "GIT_EXTERNAL_DIFF set to unexpected value" 1>&2;
exit 1;
}
FLIST="${TMP}/list.txt"
FNAME=`head -n 1 "${FLIST}"`
echo "DIFF: $FNAME"
sed -i '1 d' "${FLIST}"
# echo "[($2) : ($5) : ($FNAME)]"
D=`dirname $FNAME`
F=`basename $FNAME`
if [ x"$2" != x/dev/null ] ; then
OLD_DIR="${TMP}/old/${D}"
[ -d "${OLD_DIR}" ] || mkdir -p "${OLD_DIR}"
cp "${2}" "${OLD_DIR}/${F}"
fi
if [ x"$5" != x/dev/null ] ; then
NEW_DIR="${TMP}/new/${D}"
[ -d "${NEW_DIR}" ] || mkdir -p "${NEW_DIR}"
cp "${5}" "${NEW_DIR}/${F}"
fi
else
setupTempDirs
git diff "$@" --raw | awk '{ print $6 }' > ${TMP}/list.txt
GIT_EXTERNAL_DIFF="${0}" TMP="${TMP}" git --no-pager diff "$@"
showDiffs
cleanup
fi
@nitingupta910
Copy link
Author

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