summaryrefslogtreecommitdiff
path: root/Tools/scripts/psvn
blob: 96f8e84f05e999bd03357082e77888921a8feef1 (plain) (blame)
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
#!/bin/sh -eu
#
# psvn - Wrapper to set Subversion properties automatically
#
# Copyright (c) 2012 Beat Gaetzi <beat@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#
# $FreeBSD$
#
# MAINTAINER=	mandree@FreeBSD.org
# beat@ has implicit approval to change this script.
#

#
# The psvn wrapper checkes from replaced, conflicting, missing or
# untracked files. When committing it adds the needed Subversion
# properties and removes unneeded ones.
# There is also adds a check subcommand which just executes the
# checks.
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:${PATH}
export PATH

SVN="$(which svn)"
LF="$(printf '\nX')"
LF="${LF%X}"

VERSION=$("${SVN}" --version --quiet | sed -e 's,^\(.*\)\.\(.*\)\..*,\1\2,')
if [ ${VERSION} -lt 17 ] ;
then
	echo "===> Please consider upgrading to Subversion 1.7 (or newer)"
fi

checkstatus () {
	local IFS _error _file _status _statusline -
	_error=0
	eval "set -- $@"
	IFS="$LF"
	set -- $("${SVN}" status -- "$@")

	for _statusline
	do
		_status="$(printf '%.7s' "${_statusline}")"
		_file="${_statusline##????????}"

		case "${_status}" in
			R*)
				printf >&2 '===> Do not replace files as this will break the CVS exporter: "%s"\n' "${_file}"
				_error=1
			;;
			C*|?C*)
				printf >&2 '===> Conflict detected: \"%s\"\n' "${_file}"
				_error=1
			;;
			\~*)
				printf >&2 '===> Versioned item \"%s\" obstructed.\n' "${_file}"
				_error=1
			;;
			\?*)
				printf >&2 '===> Untracked new file "%s". Consider svn adding or deleting it.\n' "${_file}"
				_error=1
			;;
			\!*)
				printf >&2 '===> Missing file "%s". Consider re-adding or svn deleting it.\n' "${_file}"
				_error=1
			;;
		esac
	done

	if [ ${_error} -ne 0 ] ;
	then
		exit 1
	fi
}

setprop () {
	local _file -
	eval "set -- $1"

	for _file
	do
		if [ -d "${_file}" -o ! -e "${_file}" ] ;
		then
			continue
		fi
		printf >&2 '=> Adding svn keywords to "%s"\n' "${_file}"
		case $(egrep -- '\$FreeBSD\$|\$[BDFSer]+:' "${_file}" >/dev/null || echo $?) in
		"")	# matched pattern
			"${SVN}" -q -- propset svn:keywords "FreeBSD=%H" "${_file}"
			"${SVN}" -q -- propdel fbsd:nokeywords "${_file}"
			;;
		1)	# no match
			"${SVN}" -q -- propset fbsd:nokeywords yes "${_file}"
			"${SVN}" -q -- propdel svn:keywords "${_file}"
			;;
		*)	# egrep failed
			exit 1
			;;
		esac
		if [ "${_file##/*}" != "bsd.port.mk" ] ; then
			"${SVN}" -q -- propset svn:eol-style native "${_file}"
		fi
		"${SVN}" -q -- propset svn:mime-type text/plain "${_file}"
		"${SVN}" -q -- propdel cvs2svn:cvs-rev "${_file}"
	done
}

# taken from "Rich's sh (POSIX shell) tricks",
# a "Programming Guide[...]" at http://www.etalabs.net/sh_tricks.html
savearray() {
    for i do
	printf %s\\n "$i" | sed -e "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/"
    done
    echo " "
}

getfilequotedarray() {
	local varname IFS
	varname="$1"
	shift
	IFS="$LF"
	set -- $("${SVN}" status -- "$@" | sed 's/^....... //')
	eval "$varname=\$(savearray "\$@")"
}

for opt ; do
    case "${opt}" in
	-*) continue ;;
    esac
    case "${opt}" in
	check)
		shift
		if [ $# -gt 0 ] ; then
		    echo >&2 "===> Unsupported option before, or garbage after command"
		    exit 1
		fi
		getfilequotedarray "files" "$@"
		checkstatus "${files}"
		exit 0
	;;
	ci|commit)
		savedargs=$(savearray "$@")
		shift
		while getopts :qm:F: opt
		do
			case "$opt" in
				q) ;;
				m) ;;
				F) ;;
				\?) echo >&2 "===> Unsupported option -$OPTARG encountered. Abort."
				    exit 1 ;;
				:)  echo >&2 "===> Missing argument to option -$OPTARG. Abort."
				    exit 1 ;;
			esac
		done
		shift $(($OPTIND - 1))

		getfilequotedarray "files" "$@"
		checkstatus "${files}"
		setprop "${files}"

		eval "set -- $savedargs"
		exec "${SVN}" "$@"
	;;
	*)
		exec "${SVN}" "$@"
	;;
    esac
done