1#!/bin/mksh
2# $MirOS: src/scripts/editdiff,v 1.12 2009/03/29 13:04:20 tg Exp $
3#-
4# Copyright (c) 2004, 2006, 2007, 2008
5#	Thorsten Glaser <tg@mirbsd.de>
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
22unset CVSREADONLYFS
23
24f=$1; shift
25rev=$1
26if [[ $rev = ask ]]; then
27	read rev?'Revision: '
28	[[ -z $rev ]] && exit 0
29	shift
30elif [[ -n $rev ]]; then
31	shift
32fi
33if [[ $rev = +(-) ]]; then
34	rev=
35elif [[ -n $rev && $rev != -* ]]; then
36	rev=-r$rev
37fi
38cp=
39dp=
40x=cp
41while [[ $# -gt 0 ]]; do
42	if [[ $1 = -- ]]; then
43		x=dp
44	elif [[ $x = cp ]]; then
45		cp="$cp $1"
46	else
47		dp="$dp $1"
48	fi
49	shift
50done
51
52T=$(mktemp /tmp/editdiff.XXXXXXXXXX) || exit 1
53trap 'rm -f $T ${T}.{lock,orig} ; exit 0' 0
54trap 'rm -f $T ${T}.{lock,orig} ; exit 1' 1 2 3 5 13 15
55
56# heuristics for finding out if CVSREADONLYFS is supposedly supported:
57# we assume it is on local access or if :ext: is skipped but it is not
58# if :ext: or :pserver: are used
59[[ -s CVS/Root && "$(<CVS/Root)" != :* ]] && export CVSREADONLYFS=1
60
61eval cvs -q $cp diff -upa $EXTRA_DIFF $rev $dp "$f" >$T 2>&1
62touch ${T}.lock
63
64if ! patch -Rlt -o ${T}.orig "$f" $T; then
65	print continue?
66	read
67fi
68${EDITOR:-ed} $T
69
70if [[ $T -nt ${T}.lock ]]; then
71	mv "$f" "$f".orig
72	patch -l -o "$f" ${T}.orig $T
73	rv=$?
74	if [[ ! -f $f ]]; then
75		cp ${T}.orig "$f"
76	fi
77	exit $rv
78fi
79
80print Ignoring patch.
81exit 0
82