-
Notifications
You must be signed in to change notification settings - Fork 28
/
diff-it.sh
executable file
·76 lines (61 loc) · 2.65 KB
/
diff-it.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <floyd at floyd dot ch> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return
# floyd http://floyd.ch @floyd_ch <floyd at floyd dot ch>
# July 2013
# ----------------------------------------------------------------------------
TARGET="./diff-output"
if [ $# -lt 2 ]
then
echo "Usage: `basename $0` old-dir new-dir [output-dir]"
exit 0
fi
if [ $# -eq 3 ]
then
#argument without last /
TARGET=${3%/}
fi
#remove last / of arguments
ONE=${1%/}
TWO=${2%/}
CUR="`pwd`"
echo "#Diffing $1 and $2"
mkdir "$TARGET"
cd "$ONE"
find . -type f -print | sort -u > "$CUR/$TARGET/file-list-ONE.txt"
cd "$CUR" #TWO can be relative, so go back first
cd "$TWO"
find . -type f -print | sort -u > "$CUR/$TARGET/file-list-TWO.txt"
cd "$CUR"
#Summary: Which files differ at all?
diff -E -b -w -r -q "./$ONE" "./$TWO" > "$TARGET/different-files.txt"
#Summary: Which files are new/were deleted
echo "Checking which files differ, were added or removed"
comm -23 "$TARGET/file-list-ONE.txt" "$TARGET/file-list-TWO.txt" > "$TARGET/removed-files.txt"
comm -13 "$TARGET/file-list-ONE.txt" "$TARGET/file-list-TWO.txt" > "$TARGET/new-files.txt"
comm -12 "$TARGET/file-list-ONE.txt" "$TARGET/file-list-TWO.txt" > "$TARGET/common-files.txt"
#The details of all diffs: This is what we should normally check...
echo "Producing the main diff"
diff -E -b -w -r "./$ONE" "./$TWO" > "$TARGET/diff-everything.txt"
#do it separately for each file extension, so if we're in a hurry, we can e.g. only look at .java files
#these types will generate a diff file each
types="java jsp m h properties xml c cpp"
for t in $types; do
grep -E "\.$t$" "$TARGET/common-files.txt" > "$TARGET/common-$t.txt"
done
#getting files with other extensions than $types, will create one file for all of them
grep -vE ".*\.(`echo $types | tr " " "|"`)$" "$TARGET/common-files.txt" > "$TARGET/common-others.txt"
types="$types others"
for t in $types; do
#generate the diff
echo "Diffing $t files"
#uncomment to generate the two-sided comparison - WARN: it's not possible to print filenames and line numbers this way
#cat common-$t.txt | xargs -I {} -n1 diff -E -b -w -y --strip-trailing-cr --suppress-common-lines -W 200 --tabsize=4 -t $ONE/{} $TWO/{} > diff-$t.txt
cat "$TARGET/common-$t.txt" | xargs -I {} diff -E -b -w -u "$ONE/{}" "$TWO/{}" > "$TARGET/diff-$t.txt"
done
echo "Cleaning up, removing empty files in $TARGET"
find $TARGET -type f -size 0 -maxdepth 1 -delete