summaryrefslogtreecommitdiff
path: root/rvi
blob: 4b4e72dce695f148e5d636755e0c4fd186008e76 (plain)
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
#!/bin/sh

# Copyright (c) Peter Palfrader <peter@palfrader.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# wraper for rcs to edit files.  Checks out a file, spawns an editor, and then
# commits the file again.

if [ "$#" = 0 ]; then
	echo "Usage: $0 <file>" >&2
	exit 1
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	echo "Usage: $0 <file>"
	exit 0
fi

if ! [ -e "$1" ] ; then
	echo "$1 does not exist." >&2
	exit 1
fi

if command -v sensible-editor >/dev/null 2>&1; then
	editor=sensible-editor
elif command -v editor >/dev/null 2>&1; then
	editor=editor
elif command -v vi >/dev/null 2>&1; then
	editor=vi
else
	echo "Cannot find an editor!" >&2
	exit 1
fi

DN="`dirname "$1"`"
BN="`basename "$1"`"
if ! [ -e "$1,v" ] && ! [ -e "$DN/RCS/$BN,v" ] ; then
	echo "Neither $1,v nor $DN/RCS/$BN,v do exist.  Maybe you want to create it with 'ci -u $1'." >&2
	exit 1
fi

owner="`stat -c "%u:%g" "$1"`"
group=$(stat -c '%g' "$1")
rcsdiff -u "$1"
if [ $? -ne 0 ] ; then
	echo
	echo "Differences detected"
	echo "run 'co $1' to overwrite with last committed version"
	echo "or 'rcs -l $1 && ci -u $1' to commit"
	exit 1
fi
rm -f "$1.rvi.diff"

co -l "$1"
if [ $? -ne 0 ] ; then
	echo "Check out failed."
	echo "Maybe someone else is currently editing this file."
	echo "Aborting rvi."
	exit 1
fi
if [ "$(id -u)" = "0" ]; then
	chown "$owner" "$1"
elif ! chgrp "$group" "$1"; then
	echo >&2 "Warning: group ownership was $group, but this could not be restored."
fi
$editor "$1"
ci -u "$1"
if [ "$(id -u)" = "0" ]; then
    chown "$owner" "$1"
fi