Debian Codegraph @

tests/pdiff

raw
1 #!/bin/sh
2
3 # Script to get stable diff output on any platform.
4 #
5 # Output of this script is almost equivalent to GNU diff with "-Nru".
6 #
7 # Use this script as "hg pdiff" via extdiff extension with preparation
8 # below in test scripts:
9 #
10 # $ cat >> $HGRCPATH <<EOF
11 # > [extdiff]
12 # > pdiff = sh "$RUNTESTDIR/pdiff"
13 # > EOF
14
15 filediff(){
16 # USAGE: filediff file1 file2 [header]
17
18 # compare with /dev/null if file doesn't exist (as "-N" option)
19 file1="$1"
20 if test ! -f "$file1"; then
21 file1=/dev/null
22 fi
23 file2="$2"
24 if test ! -f "$file2"; then
25 file2=/dev/null
26 fi
27
28 if cmp -s "$file1" "$file2" 2> /dev/null; then
29 # Return immediately, because comparison isn't needed. This
30 # also avoids redundant message of diff like "No differences
31 # encountered" (on Solaris)
32 return
33 fi
34
35 if test -n "$3"; then
36 # show header only in recursive case
37 echo "$3"
38 fi
39
40 # replace "/dev/null" by corresponded filename (as "-N" option)
41 diff -u "$file1" "$file2" |
42 sed "s@^--- /dev/null\(.*\)\$@--- $1\1@" |
43 sed "s@^\+\+\+ /dev/null\(.*\)\$@+++ $2\1@"
44
45 # in this case, files differ from each other
46 return 1
47 }
48
49 if test -d "$1" -o -d "$2"; then
50 # ensure comparison in dictionary order
51 (
52 if test -d "$1"; then (cd "$1" && find . -type f); fi
53 if test -d "$2"; then (cd "$2" && find . -type f); fi
54 ) |
55 sed 's@^\./@@g' | sort | uniq |
56 while read file; do
57 filediff "$1/$file" "$2/$file" "diff -Nru $1/$file $2/$file"
58 done
59
60 # TODO: there is no portable way for current while-read based
61 # implementation to return 1 at detecting changes.
62 #
63 # On bash and dash, assignment to variable inside while-block
64 # doesn't affect outside, because inside while-block is executed
65 # in sub-shell. BTW, it affects outside while-block on ksh (as sh
66 # on Solaris).
67 else
68 filediff "$1" "$2"
69 fi