Debian Codegraph @

debian/cvsmgdiff

raw
1 #! /bin/bash
2
3 #
4 # This script lets you recursively diff the CVS directories you have
5 # checked out. To use, just pass in an optional revision levels and
6 # an optional file directory name. This script then will show you the
7 # differences you're interested in.
8 #
9 # -- Paul Serice
10 #
11
12 CVSMGDIFF_VERSION=1.1
13
14 : ${MGDIFF:=/usr/bin/mgdiff}
15 : ${TMP:=/tmp}
16
17 function usage {
18 if [ $# -ne 1 ] ; then
19 echo "progname: Error: Invalid args for usage: \"$@\"" >&2
20 exit 1
21 fi
22 cmd=
23 cmd="$cmd echo >&2 ;"
24 cmd="$cmd echo \"Usage: $progname [-h] [-v]"
25 cmd="$cmd [-g <gui>] [[-r <rev1>] [-r <rev2>]] file\" >&$1 ;"
26 cmd="$cmd echo >&2"
27 eval "$cmd"
28 if [ $1 -eq 1 ] ; then
29 exit 0
30 fi
31 exit 1
32 }
33
34 function verify_exec {
35 if [ $# -ne 1 ] ; then
36 echo "$progname: Error: Invalid args for verify_exec: \"$@\"" >&2
37 exit 1
38 fi
39 if ! type "$1" >/dev/null 2>&1 ; then
40 echo "$progname: Error: Unable to find executable for \"$1\"." >&2
41 exit 1
42 fi
43 }
44
45 tmp_base="cvsmgdiff-$$."
46 function clean_up {
47 # These signal handlers are rough. It is, as far as I can tell,
48 # impossible to pass in the name of the currently active temporary
49 # files. A reasonable alternative is to scan $TMP for files that
50 # match the pattern we use being careful not to allow a malicious
51 # user to trick us into deleting some other file.
52 find "$TMP" -maxdepth 1 \
53 -type f \
54 -links 1 \
55 -uid `id -u` \
56 -name "$tmp_base"'*' \
57 -print0 \
58 | xargs -r0 rm -f
59 exit 0
60 }
61
62 #
63 # getunique() -- Finds two unique temporary file names.
64 #
65 getunique()
66 {
67 old_umask=`umask`
68 umask 077
69 if ! tmp_file_1=$(mktemp -q "$TMP/${tmp_base}XXXXXX") ; then
70 echo "Error: Unable to allocate a necessary temporary file." >&2
71 exit 1
72 fi
73
74 if ! tmp_file_2=$(mktemp -q "$TMP/${tmp_base}XXXXXX") ; then
75 echo "Error: Unable to allocate a necessary temporary file." >&2
76 exit 1
77 fi
78 umask $old_umask
79 }
80
81 #
82 # Script Starts Here !!!
83 #
84
85 progname="cvsmgdiff.sh"
86 verify_exec "basename"
87 progname=`basename $0`
88
89 # Signal Trap handler to clean up temporary files in "$TMP".
90 if ! trap 'clean_up' HUP INT QUIT TERM ; then
91 echo "$progname: Unable to register signal handler." >&2
92 exit 1
93 fi
94
95 # Get cvs revision(s) to use.
96 cnt=0
97 while getopts "g:hr:v" OPT ; do
98 case "$OPT" in
99 g) MGDIFF="$OPTARG"
100 ;;
101 h) usage 1
102 ;;
103 r) rev[$cnt]="-r$OPTARG"
104 cnt=`expr $cnt + 1`
105 ;;
106 v) echo "$progname: ${CVSMGDIFF_VERSION}"
107 exit 0
108 ;;
109 \?) usage 2
110 ;;
111 esac
112 done
113 shift `expr $OPTIND - 1`
114
115 verify_exec "cut"
116 verify_exec "cvs"
117 verify_exec "echo"
118 verify_exec "expr"
119 verify_exec "find"
120 verify_exec "grep"
121 verify_exec "$MGDIFF"
122 verify_exec "mktemp"
123 verify_exec "sleep"
124 verify_exec "xargs"
125
126 mgdiff_basename=`basename "$MGDIFF"`
127
128 # Portability issues.
129 if [ "$mgdiff_basename" = "mgdiff" ] ; then
130 QUIET_OPT="-quit"
131 FNAME_OPT="-file"
132 elif [ "$mgdiff_basename" = "xdiff" ] ; then
133 QUIET_OPT="-D"
134 FNAME_OPT="-N"
135 elif [ "$mgdiff_basename" = "xxdiff" ] ; then
136 QUIET_OPT="-D"
137 FNAME_OPT="-N"
138 TITLE_1_OPT="--title1"
139 TITLE_2_OPT="--title2"
140 fi
141
142 if [ $cnt -gt 2 ] ; then
143 echo
144 echo "Error: Too many revisions."
145 echo
146 exit 1
147 fi
148
149 if [ ! -d `pwd`"/CVS" ] && [ ! -d `pwd`"/$1/CVS" ] ; then
150 echo
151 echo "Warning: \"$1\" does not appear to be a CVS directory." 1>&2
152 echo "Trying to diff \"$1\" with CVS repository anyway."
153 echo
154 sleep 5
155 fi
156
157 # Run CVS recursively on the entire directory.
158 cvs diff "${rev[@]}" "$@" 2>/dev/null \
159 | grep '^Index:' \
160 | cut -d ' ' -f 2 \
161 | while read fname ; do
162
163 echo -n "Processing $fname . . . "
164
165 # tkdiff CVS access built-in.
166 if [ "$mgdiff_basename" = "tkdiff" ] ; then
167
168 "$MGDIFF" ${rev[0]+"${rev[@]}"} "$fname"
169
170 # The others require a little bit of scripting.
171 elif [ $cnt -eq 2 ] ; then
172
173 getunique
174
175 \cvs update -p "${rev[0]}" "$fname" > "$tmp_file_1" 2> /dev/null
176 \cvs update -p "${rev[1]}" "$fname" > "$tmp_file_2" 2> /dev/null
177
178 "$MGDIFF" ${QUIET_OPT+"$QUIET_OPT"} \
179 ${TITLE_1_OPT+"$TITLE_1_OPT" "$fname (rev ${rev[0]#-r})"} \
180 ${TITLE_2_OPT+"$TITLE_2_OPT" "$fname (rev ${rev[1]#-r})"} \
181 "$tmp_file_1" "$tmp_file_2"
182
183 \rm -f "$tmp_file_1" > /dev/null 2>&1
184 \rm -f "$tmp_file_2" > /dev/null 2>&1
185
186 elif [ $cnt -le 1 ] ; then
187
188 if [ $cnt -eq 1 ] ; then
189 title_rev="${rev[0]#-r}"
190 else
191 title_rev=`cvs status "$fname" \
192 | grep 'Working revision:' \
193 | awk '{print $3;}'`
194 fi
195
196 # For some reason, xxdiff does not like to work with pipes
197 # despite its saying otherwise.
198 if [ "$TITLE_1_OPT" ] ; then
199
200 getunique
201
202 # The convention that "diff" uses is that the old file is on
203 # the left and the new file is on the right. We use this to
204 # display the files for all but "mgdiff" which has a
205 # "File->Save As..." menu option that works better the other
206 # way around.
207 file_first="$tmp_file_1"
208 file_second="$fname"
209 if [ "$mgdiff_basename" = "mgdiff" ] ; then
210 file_first="$fname"
211 file_second="$tmp_file_1"
212 fi
213
214 cvs update -p ${rev[0]+"${rev[0]}"} "$fname" \
215 > "$tmp_file_1" 2>/dev/null
216
217 "$MGDIFF" ${QUIET_OPT+"$QUIET_OPT"} \
218 ${TITLE_1_OPT+"$TITLE_1_OPT" "$fname (rev $title_rev)"}\
219 "$file_first" "$file_second"
220
221 \rm -f "$tmp_file_1" > /dev/null 2>&1
222 \rm -f "$tmp_file_2" > /dev/null 2>&1
223
224 else
225
226 # See comment above.
227 file_first="-"
228 file_second="$fname"
229 if [ "$mgdiff_basename" = "mgdiff" ] ; then
230 file_first="$fname"
231 file_second="-"
232 fi
233
234 cvs update -p ${rev[0]+"${rev[0]}"} "$fname" 2>/dev/null \
235 | "$MGDIFF" ${QUIET_OPT+"$QUIET_OPT"} \
236 ${FNAME_OPT+"$FNAME_OPT" "$fname (rev $title_rev)"} \
237 "$file_first" "$file_second"
238
239 fi
240
241 fi
242
243 echo "Done."
244
245 done