Skip to content

Instantly share code, notes, and snippets.

@rbeer
Last active July 14, 2017 20:58
Embed
What would you like to do?
Diff working-tree of two git repositories, testing multiple branches
#! /usr/bin/env bash
_V="0.0.1"
function print_usage() {
printf '=%.0s' {1..80}; printf '\n'
echo "Diff Repos - $_V"
echo "Diff working-tree of two git repositories, testing multiple branches"
echo "Depends: ./diff-branches, write-access to ./"
echo "Usage: ./diff-repos.sh <repository_1> <repository_2> [-v]"
echo " -v | verbose; print diff log to stdout"
exit 1;
}
# branches to compare are stored in a
# simple text file ./diff-branches
# - one branch per line
if [ ! -e ./diff-branches ]; then
echo "No branch list (./diff-branches) found!"
exit 1;
fi
# show error when given path isn't a git repository
function no_repo_report() {
echo "No git repository found at: $1/.git"
echo "Notice: This script doesn't work with --bare repositories."
}
# eye candy
function print_header() {
printf '=%.0s' {1..80}; printf '\n'
echo "Comparing branch: $1"
printf '=%.0s' {1..80}; printf '\n'
}
function print_footer() {
printf '~%.0s' {1..80}; printf '\n'
echo "Differences: $1 "
printf '=%.0s' {1..80}; printf '\n'
printf '\n\n'
}
# set up repository paths
_REPO_1=$1
if [ ! -e "$_REPO_1/.git" ]; then
no_repo_report $_REPO_1
print_usage
exit 1;
fi
_REPO_2=$2
if [ ! -e "$_REPO_2/.git" ]; then
no_repo_report $_REPO_2
print_usage
exit 1;
fi
# changes branches within both
# given repositories
function change_branch() {
echo -n "Entering $1 >> "
cd $1
echo -n "Checking out $2 >> "
git checkout $2
echo "Returning to parent."
cd ..
}
echo -e "Processing ./diff-branches:\n"
# main loop
while read branch; do
# skip empty lines
if [ "$branch" == "" ]; then
echo "Skipping empty line..."
continue
fi
# new log entry
print_header $branch
# change branches in repos
change_branch $_REPO_1 $branch
change_branch $_REPO_2 $branch
# diff both repos,
# -q: only showing differences
# -r: recursively,
# -x: ignoring .git folder (obviously different; it's the point of it all)
diff_log=./.diff-log-$branch
diff $_REPO_1 $_REPO_2 -qr -x .git &> $diff_log
[ "$3" == "-v" ] && cat $diff_log
print_footer `cat $diff_log | wc -l`
done < ./diff-branches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment