Skip to content

Compare two branch's content

Damien Buhl edited this page Oct 29, 2014 · 1 revision

Comparing two branches with git difftool shows you the changes one file after the other, which is tedious. Comparing the git log branchA..branchB or gitk branchA..branchB gives you the differing commits.

But if your interested in the changes to the files in a summarized view, then you should use the following script, as it uses kdiff3 to display a summary of the changes you have in both branches.

For example to compare changes made by branchB on top of branchA, you can do : git-diffc branchA branchB

git-diffc

#!/bin/bash

##
# git-diffc: Graphical Git diff viewer
#	'c' -> comprehensive :)
# Author: Nitin Gupta <ngupta [at] vflare [dot] org>
# Last Modified (mm-dd-yy): 12/27/09
#

##
# Usage:
#	git-diffc [usual git diff arguments]
#
# This script recreates (sparse) trees containing original
# and modified files. These trees are given to specified
# diff viewer which should be able to handle arguments like:
#	$DIFF_BIN <olddir> <newdir>
# Following diff viewers are known to handle such input:
#	kdiff3, kompare, meld
#

##
# 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 kdiff3`}"
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"
}

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 "${FLIST}" -n 1`
	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