Skip to content

Instantly share code, notes, and snippets.

@galvez

galvez/gist:263457

Forked from leandro/gist:263095
Created Dec 25, 2009
Embed
What would you like to do?
#!/bin/bash
# ddiff: Improved diff for directories
# ---------------------------------------------------------------------------------
# This is useful for creating a diff file between two directories.
# It's built upon the tradicional diff program.
# With a difference: it will create diff files for each file present only in the first directory.
# So, for given dirs A and B:
# full-diff A B
#
# it will generate a diff file with all the difference between both directories including diffs for whole files present only in A directory;
# PS.: it also generates 'mkdir -p PATH' bash-commented commands for eventual empty directories only present on A directory.
# PS.: you can pass the same diff's options to this shell command. I strongly recommend you to always pass -r (to diff the entire directory tree, recursively).
# The output generated is meant to be applied on B directory.
#
# Author: Leandro N. Camargo
# E-mail: leandroico _=AT=_ gmail
_i=0
_np=$(expr $# - 2)
_opts=
_params=
_args=("$@")
_append_diff()
{
if [ -d $1 ]; then
echo "# You need to create the following directory: $1"
echo "# mkdir -p $1"
else
echo "diff $_opts $1 $(echo "$1" | sed "s=$_base_dir=$_ref_dir=")"
diff $_opts $1 /dev/null
fi
}
while [ $_i -lt $# ]; do
if [ $_i -lt $_np ]; then
_opts="$_opts ${_args[$_i]}"
else
_t=${#_params}
_params="$_params ${_args[$_i]}"
if [ $_t -eq 0 ]; then
_base_dir=${_args[$_i]}
else
_ref_dir=${_args[$_i]}
fi
fi
_i=$(expr $_i + 1)
done
# trimming the first blank space of each var
_opts=$(echo "$_opts" | sed 's/^.//')
_params=$(echo "$_params" | sed 's/^.//')
_m_base_dir=$(echo "$_base_dir" | sed -e 's/\./\\./g' -e 's=/=\\\/=g')
_diff=$(diff $_opts $_params)
_diff_lines=$(echo "$_diff" | sed '/^Only/d')
_full_files=$(echo "$_diff" | sed -e "/^Only in $_m_base_dir/!d" -e "s=^Only in \([^:]*\): \(.*\)$=\1/\2=" -e "s=/\{2,\}=/=g")
echo "$_diff_lines"
for L in $_full_files; do
_append_diff $L
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.