-
Notifications
You must be signed in to change notification settings - Fork 13
/
viz
executable file
·94 lines (77 loc) · 1.75 KB
/
viz
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
#!/bin/bash
me="`basename $0`"
if [ $# != 1 ] || ! [ -e "$1" ]; then
echo "Usage: $me <file>"
exit 1
fi
dir="`dirname $1`"
file="`basename $1`"
orig_dir="`pwd`"
if ! viz_tmpdir=`mktemp -d /tmp/$me.XXXXXXXX` || ! [ -d "$viz_tmpdir" ]; then
echo "mktemp failed: $!"
exit 1
fi
cleanup () {
[ -n "$viz_tmpdir" ] && rm -rf "$viz_tmpdir"
}
trap cleanup EXIT
if ! co -l "$dir/$file"; then
echo "Checkout failed; aborting." >&2
exit 1
fi
tmpfile="$viz_tmpdir/$file"
cp "$dir/$file" "$tmpfile"
editor="${EDITOR:-${VISUAL:-vi}}"
validate () {
if [ -x "$dir/.vizrc" ]; then
echo "Testing validity of changes ... "
"$dir/.vizrc" "$tmpfile" "$dir" "$file" "$viz_tmpdir"
return $?
fi
return 0
}
trap 'echo -e "\nNo cheating now!"' INT
$editor "$tmpfile"
while [ 1 = 1 ]; do
changed=
cmp -s "$dir/$file" "$tmpfile" || changed=y
[ -z "$changed" ] && break
if validate "$tmpfile"; then
echo "Changes validated OK."
validates=y
else
echo "Changes failed validation!"
validates=
fi
replied=
while [ -z "$replied" ]; do
if [ -n "$validates" ]; then
read -n 1 -p "Re-edit, diff, check in, or abandon [r/d/c/a]? " reply
else
read -n 1 -p "Re-edit, diff, or abandon [r/d/a]? " reply
fi
case "$reply" in
a) changed=
echo
# get me outta here
break 10
;;
r) replied=y ; echo ;;
c) echo
if [ -n "$validates" ]; then
# get me outta here
break 10
fi
;;
d) echo; diff -u "$dir/$file" "$tmpfile" | less ;;
*) echo ;;
esac
done
$editor "$tmpfile"
done
if [ -z "$changed" ]; then
co -u -f "$dir/$file"
exit 0
fi
cp "$tmpfile" "$dir" # rather than mv, preserves permissions
ci -u "$dir/$file"