forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-delta
More file actions
executable file
·481 lines (398 loc) · 11.3 KB
/
get-delta
File metadata and controls
executable file
·481 lines (398 loc) · 11.3 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#! /usr/local/bin/bash
#
# get-delta.sh
#
# Usage:
#
# get-delta.sh [opts] output-file.sh [file ...]
#
# This script must be executed from within a project tree. It
# examines the set of files that have been checked out (via neartool)
# and modified, and generates a script file that can be used to apply
# the changes made back to the main ClearCase vobs.
#
# By default, it generates a script for all checked-out files. You
# can restrict its operation to certain files and/or directories by
# listing them on the command line.
#
# Options:
#
# -c collapse versioning information on local copy after completion.
# Use this option with caution, as it is irreversible (and
# noninterruptible). Once the versioning information has been
# collapsed, it will be impossible to regenerate a script
# representing the changes that have been made locally; you
# should only do this when you are sure that your changes have
# been successfully applied to the other end.
#
# On the other hand, if you forget to run get-delta with -c after
# you have successfully applied your changes, you may
# inadvertently attempt to apply them again if you subsequently
# try to apply more changes.
#
#ENDCOMMENT
function usage {
sed '/#ENDCOMMENT/,$d' <$0 >&2
exit 1
}
#
# list_comments ( dirname basename )
#
# Writes to stdout any comments associated with checked-out versions of
# the indicated file, in order.
#
function list_comments {
local dirname=$1
local basename=$2
local filename=$dirname/$basename
local file comment version
if [ -f $dirname/.ct0.$basename ]; then
# Now look for comments, in version-number order.
# We use a series of ls commands so we don't try to sort the
# filenames between the one-, two-, and three-digit version
# numbers.
for file in `(cd $dirname; ls .ct[0-9].$basename; ls .ct[0-9][0-9].$basename; ls .ct[0-9][0-9][0-9].$basename) 2>/dev/null`; do
version=`echo $file | sed "s/^\.ct\([0-9]*\).*$/\1/"`
comment=$dirname/.ct${version}comment.$basename
if [ -f $comment ]; then
cat $comment
fi
done
fi
}
#
# get_fullpath ( local_dir )
#
# Sets $fullpath to the fully-qualified pathname associated with $local_dir.
#
function get_fullpath {
local local_dir=$1
if [ -z "$local_dir" ]; then
fullpath=`pwd`
else
if [ ! -d "$local_dir" ]; then
echo "Invalid directory: $local_dir" 1>&2
exit 1
fi
# If we use pwd instead of /bin/pwd, $PWD will be used, which will give
# the wrong answer
fullpath=`(cd $local_dir; /bin/pwd)`
fi
}
#
# get_rel_dir ( root_dir local_dir )
#
# Sets $rel_dir to the string which represents $local_dir relative to
# $root_dir. This is a simple string-prefix operation, and could fail
# in some obscure cases.
#
function get_rel_dir {
get_fullpath $1
local root_dir=$fullpath
get_fullpath $2
local local_dir=$fullpath
# Now remove the initial prefix.
if [ "$root_dir" = "$local_dir" ]; then
rel_dir="."
else
rel_dir=`echo $local_dir | sed 's:^'$root_dir/'::'`
if [ "$rel_dir" = "$local_dir" ]; then
echo "$local_dir is not a directory within $root_dir." 1>&2
exit 1
fi
fi
}
collapse=
while getopts "ch" flag; do
case $flag in
c) collapse=y;;
h) usage;;
\?) exit 1;
esac
done
shift `expr $OPTIND - 1`
output=$1
shift
projroot=`ctproj -r`
if [ -z "$projroot" ]; then
echo "You must execute this script in a project tree."
exit 1
fi
if [ -z "$output" ]; then
usage
fi
# Perform some sanity checks on input parameters.
if [ ! -d "$projroot" ]; then
echo "$projroot is not a directory!"
exit 1
fi
if [ `basename $output .sh` = `basename $output` ]; then
echo "$output should end in .sh"
exit 1
fi
if [ -f "$output" ]; then
rm -i $output
if [ -f "$output" ]; then
echo "Not overwriting $output"
exit 1
fi
elif [ -e "$output" ]; then
echo "Cannot overwrite $output"
exit 1
fi
echo ""
# Temporary files we'll build up as we process the files.
base=`basename $output`
temp_ct0=/tmp/gd.ct0.$base
temp_checkout=/tmp/gd.checkout.$base
temp_dirs=/tmp/gd.dirs.$base
temp_files=/tmp/gd.files.$base
temp_diffs=/tmp/gd.diffs.$base
rm -f $temp_ct0 $temp_checkout $temp_dirs $temp_files $temp_diffs
touch $temp_ct0 $temp_dirs $temp_files
# Get the list of files we'll want to delta in.
if [ $# -eq 0 ]; then
# No explicit files, get all of them.
(cd $projroot; find . -name .ct0.\* -print) >>$temp_ct0
else
# An explicit list of files.
for filename in $*; do
if [ -f $filename ]; then
dirname=`dirname $filename`
basename=`basename $filename`
if [ -f $dirname/.ct0.$basename ]; then
get_rel_dir $projroot $dirname
echo ./$rel_dir/.ct0.$basename >>$temp_ct0
else
echo $filename has no versions.
fi
elif [ -d $filename ]; then
get_rel_dir $projroot $filename
(cd $projroot; find ./$rel_dir -name .ct0.\* -print) >>$temp_ct0
else
echo $filename not found.
fi
done
fi
# Now start to build up the script.
echo "#! /bin/sh" >$output
chmod 755 $output
if [ ! -w $output ]; then
echo "Cannot write to $output!"
exit 1
fi
projname=`basename $projroot`
# This part we cat in quoted, verbatim.
cat << 'EOF' >>$output
any_opts=
list=
checkout=
patch=
cleanup=
checkin=
delta=
help=
while getopts "lopcidfh" flag; do
any_opts=y
only_list=y
case $flag in
l) list=y;;
o) checkout=y; only_list=;;
p) patch=y; only_list=;;
c) cleanup=y; only_list=;;
i) checkin=y; only_list=;;
d) delta=y; only_list=;;
f) checkout=y
patch=y
cleanup=y
checkin=y
delta=y
only_list=;;
h) help=y;;
\?) exit 1;
esac
done
EOF
# This part we cat in unquoted, so we can substitute the projname
# variable.
cat << EOFOUTER >>$output
if [ \$help ]; then
cat << 'EOF'
This patch file was generated using get-delta on a remote $projname tree.
It's designed to be run one time to apply the changes made remotely
back to the main branch of the tree.
It should be run from within your own view, somewhere within the
$projname hierarchy on the ClearCase system.
Options:
-l List information about the patch file, including the creation
date and the list of modified files.
-o Checkout all the relevant files and perform other ClearCase
operations (like renaming, creating, and removing files).
-p Apply the relevant patches to all files after they have been
checked out. If this operation fails, the rest of the script
will not continue.
-c Cleanup after successfully patching by removing .orig and .rej
files.
-i Checkin modified files after successfully patching.
-d Perform final merge by executing ctdelta on modified files.
-f Perform full checkout/patch/merge cycle. This is equivalent to
specifying -opcid.
-h This help page.
If no options are specified, the default is -opc.
EOF
exit 0
fi
if [ -z "\$any_opts" ]; then
checkout=y
patch=y
cleanup=y
fi
EOFOUTER
any_merged=
# We start with the commands given in the project's .ctcmds file.
ctcmds=$projroot/.ctcmds
if [ -f $ctcmds ]; then
any_merged=y
cat $ctcmds >>$temp_checkout
if [ $collapse ]; then
rm $ctcmds
fi
fi
for ct0 in `cat $temp_ct0`; do
dir=`dirname $ct0`
base=`basename $ct0 | sed 's/^\.ct0\.//'`
file=$dir/$base
ctnew=$projroot/$dir/.ctnew.$base
if (cd $projroot; diff -u $ct0 $file >>$temp_diffs); then
# If diff returned success, it means the files were identical. In
# this case, don't bother checking it out.
echo "$file is unchanged."
else
# Otherwise, the files were genuinely different. Check it out on
# the remote end.
echo $file
if [ -f $ctnew ]; then
# This file is newly created. We need to create an element for
# it on the remote end.
eltype=`cat $ctnew`
if grep -x $dir $temp_dirs >/dev/null; then
echo directory already checked out >/dev/null
else
echo "ctco -nc $dir" >>$temp_checkout
echo $dir >> $temp_dirs
fi
echo "touch $file" >>$temp_checkout
echo "ctmkelem -eltype $eltype $file << 'EOF' 2>/dev/null" >>$temp_checkout
list_comments $projroot/$dir $base >>$temp_checkout
echo EOF >>$temp_checkout
echo $file >> $temp_files
else
echo "ctco $file << 'EOF' 2>/dev/null" >>$temp_checkout
list_comments $projroot/$dir $base >>$temp_checkout
echo EOF >>$temp_checkout
echo $file >> $temp_files
fi
fi
if [ $collapse ]; then
echo Collapsing $file
neartool collapse $projroot/$file
fi
done
echo ""
# Handle -l, list files modified
echo "" >>$output
echo 'if [ $list ]; then' >>$output
echo ' echo ""' >>$output
echo ' echo Patch file for '$projname' tree, built on '`date` >> $output
echo ' echo File was built by '`whoami` on `hostname` >>$output
echo ' echo ""' >>$output
echo ' echo Affected files are:' >>$output
echo ' cat <<EOF' >>$output
sed "s/^\./ $projname/" $temp_files >>$output
echo 'EOF' >>$output
echo ' echo ""' >>$output
echo 'fi' >>$output
# Everything else depends on being in the proper tree.
cat <<EOF >>$output
if [ \$only_list ]; then
exit 0
fi
projroot=\`ctproj -r\`
if [ -z "\$projroot" ]; then
echo ""
echo "You must execute this script within the $projname tree."
echo ""
exit 1
fi
if [ \`basename \$projroot\` != "$projname" ]; then
echo ""
echo "This script is intended for the $projname tree."
echo ""
exit 1
fi
if [ ! -d /usr/atria ]; then
echo ""
echo "This script is intended to be run on an actual ClearCase vobs."
echo ""
exit 1
fi
tmpfile=\`whoami\`-merge-$projname.tmp
cd \$projroot
touch \$tmpfile
EOF
# Handle -o, checkout stuff (and perform general ClearCase changes)
echo "" >>$output
echo 'if [ $checkout ]; then' >>$output
if [ -f $temp_checkout ]; then
cat $temp_checkout >> $output
else
echo 'echo Nothing to checkout.' >>$output
fi
echo 'fi' >>$output
# Handle -p, apply patch
echo "" >>$output
echo 'if [ $patch ]; then' >>$output
if [ -f $temp_diffs ]; then
any_merged=y;
echo " echo ''" >> $output
echo " echo Applying patches." >> $output
echo " if sed 's/^X//' << 'EOF' | patch -fsu; then" >>$output
sed 's/^/X/' < $temp_diffs >>$output
echo EOF >>$output
echo " echo All patches applied successfully." >>$output
echo " echo ''" >>$output
echo " else" >>$output
echo " echo Some conflicts detected:" >>$output
echo " find . -name '*.rej' -newer \$tmpfile -print" >>$output
echo " rm -f \$tmpfile" >>$output
echo " exit 1" >>$output
echo " fi" >>$output
else
echo ' echo No patches to apply.' >>$output
fi
echo 'fi' >>$output
echo "rm -f \$tmpfile" >>$output
# Handle -c, cleanup
echo "" >>$output
echo 'if [ $cleanup ]; then' >>$output
sed 's/^\(.*\)$/ rm -f \1.orig \1.rej/' <$temp_files >>$output
echo 'fi' >>$output
# Handle -i, checkin
echo "" >>$output
echo 'if [ $checkin ]; then' >>$output
sed 's/^\(.*\)$/ ctci -nc \1/' <$temp_files >>$output
sed 's/^\(.*\)$/ ctci -nc \1/' <$temp_dirs >>$output
echo 'fi' >>$output
# Handle -d, delta
echo "" >>$output
echo 'if [ $delta ]; then' >>$output
sed 's/^\(.*\)$/ ctdelta \1/' <$temp_files >>$output
sed 's/^\(.*\)$/ ctdelta \1/' <$temp_dirs >>$output
echo 'fi' >>$output
rm -f $temp_ct0 $temp_checkout $temp_dirs $temp_files $temp_diffs
if [ -z "$any_merged" ]; then
echo "Nothing to do!"
echo ""
rm -f $output
exit 1
fi