| 1 |
#!/bin/mksh |
| 2 |
# $MirOS: src/scripts/mnt-cvsroot,v 1.19 2016/10/21 21:09:59 tg Exp $ |
| 3 |
#- |
| 4 |
# Copyright © 2005, 2008, 2011 |
| 5 |
# mirabilos <[email protected]> |
| 6 |
# |
| 7 |
# Provided that these terms and disclaimer and all copyright notices |
| 8 |
# are retained or reproduced in an accompanying document, permission |
| 9 |
# is granted to deal in this work without restriction, including un‐ |
| 10 |
# limited rights to use, publicly perform, distribute, sell, modify, |
| 11 |
# merge, give away, or sublicence. |
| 12 |
# |
| 13 |
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to |
| 14 |
# the utmost extent permitted by applicable law, neither express nor |
| 15 |
# implied; without malicious intent or gross negligence. In no event |
| 16 |
# may a licensor, author or contributor be held liable for indirect, |
| 17 |
# direct, other damage, loss, or other issues arising in any way out |
| 18 |
# of dealing in the work, even if advised of the possibility of such |
| 19 |
# damage or existence of a defect, except proven that it results out |
| 20 |
# of said person’s immediate fault when using the work as intended. |
| 21 |
#- |
| 22 |
# Change CVSROOT of a checked out tree (and save space with it) |
| 23 |
# With option -T: change Tag instead of Root |
| 24 |
|
| 25 |
me=${0##*/} |
| 26 |
function die { |
| 27 |
print -ru2 -- "$me: $*" |
| 28 |
exit 1 |
| 29 |
} |
| 30 |
|
| 31 |
if [[ $1 = -T ]]; then |
| 32 |
tagmode=-T |
| 33 |
fn=Tag |
| 34 |
shift |
| 35 |
else |
| 36 |
tagmode= |
| 37 |
fn=Root |
| 38 |
fi |
| 39 |
newroot=$1 |
| 40 |
useroot=0 |
| 41 |
if [[ $newroot = - ]]; then |
| 42 |
shift |
| 43 |
newroot=$(realpath "$1") |
| 44 |
[[ -d $newroot ]] && if [[ -d $newroot/CVS ]]; then |
| 45 |
newroot=$newroot/CVS/$fn |
| 46 |
else |
| 47 |
newroot=$newroot/$fn |
| 48 |
fi |
| 49 |
useroot=1 |
| 50 |
fi |
| 51 |
if [[ -z $newroot || $newroot = -? ]]; then |
| 52 |
print -u2 "Syntax: $me newroot [dir [...]]" |
| 53 |
print -u2 "\t$me - .../CVS/Root [dir [...]]" |
| 54 |
print -u2 "\t$me -T - .../CVS/Tag [dir [...]]" |
| 55 |
exit 1 |
| 56 |
fi |
| 57 |
shift |
| 58 |
|
| 59 |
[[ -n $1 ]] || set -- . |
| 60 |
|
| 61 |
# realpath(2)ise arguments |
| 62 |
set -A arg |
| 63 |
i=0 |
| 64 |
for name in "$@"; do |
| 65 |
arg[i++]=$(realpath "$name") |
| 66 |
done |
| 67 |
|
| 68 |
T="$(mktemp ${arg[0]}/$me.XXXXXXXXXX)" || die fatal: cannot mktemp |
| 69 |
|
| 70 |
if (( useroot )); then |
| 71 |
rm -f "$T" |
| 72 |
ln "$newroot" "$T" || cp "$newroot" "$T" || \ |
| 73 |
die "fatal: cannot copy '$newroot' to temporary '$T'" |
| 74 |
fi |
| 75 |
|
| 76 |
trap 'rm -f "$T"; exit 0' 0 |
| 77 |
trap 'rm -f "$T"; trap - EXIT; exit 1' 1 2 3 5 13 15 |
| 78 |
|
| 79 |
if (( !useroot )); then |
| 80 |
chmod 664 "$T" |
| 81 |
print -r -- "$newroot" >"$T" |
| 82 |
fi |
| 83 |
|
| 84 |
let rv=0 |
| 85 |
find "${arg[@]}" -path \*/CVS/$fn -print0 |& |
| 86 |
while IFS= read -d '' -pr name; do |
| 87 |
rm "$name" || die "error: cannot rm <$name>" |
| 88 |
ln -f "$T" "$name" || if ! U="$(mktemp ${arg[0]}/$me.XXXXXXXXXX)"; then |
| 89 |
cp "$T" "$name" |
| 90 |
elif cat "$T" >"$U" && ln -f "$U" "$name"; then |
| 91 |
rm -f "$T" |
| 92 |
T="$U" |
| 93 |
else |
| 94 |
rm -f "$U" |
| 95 |
cp "$T" "$name" |
| 96 |
fi |
| 97 |
done |
| 98 |
|
| 99 |
exit 0 |