1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: subversion.eclass
7 # Akinori Hattori <hattya@gentoo.org>
9 # Original Author: Akinori Hattori <hattya@gentoo.org>
10 # @BLURB: The subversion eclass is written to fetch software sources from subversion repositories
12 # The subversion eclass provides functions to fetch, patch and bootstrap
13 # software sources from subversion repositories.
21 EXPORT_FUNCTIONS src_unpack pkg_preinst
22 DEPEND="dev-vcs/subversion"
25 EXPORT_FUNCTIONS src_unpack src_prepare pkg_preinst
26 DEPEND="|| ( dev-vcs/subversion[http] dev-vcs/subversion[webdav-neon] dev-vcs/subversion[webdav-serf] )"
30 DEPEND+=" net-misc/rsync"
32 # @ECLASS-VARIABLE: ESVN_STORE_DIR
34 # subversion sources store directory. Users may override this in /etc/portage/make.conf
35 [[ -z ${ESVN_STORE_DIR} ]] && ESVN_STORE_DIR="${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/svn-src"
37 # @ECLASS-VARIABLE: ESVN_FETCH_CMD
39 # subversion checkout command
40 ESVN_FETCH_CMD="svn checkout"
42 # @ECLASS-VARIABLE: ESVN_UPDATE_CMD
44 # subversion update command
45 ESVN_UPDATE_CMD="svn update"
47 # @ECLASS-VARIABLE: ESVN_SWITCH_CMD
49 # subversion switch command
50 ESVN_SWITCH_CMD="svn switch"
52 # @ECLASS-VARIABLE: ESVN_OPTIONS
54 # the options passed to checkout or update. If you want a specific revision see
55 # ESVN_REPO_URI instead of using -rREV.
56 ESVN_OPTIONS="${ESVN_OPTIONS:-}"
58 # @ECLASS-VARIABLE: ESVN_REPO_URI
62 # e.g. http://foo/trunk, svn://bar/trunk, svn://bar/branch/foo@1234
64 # supported URI schemes:
71 # to peg to a specific revision, append @REV to the repo's uri
72 ESVN_REPO_URI="${ESVN_REPO_URI:-}"
74 # @ECLASS-VARIABLE: ESVN_REVISION
76 # User configurable revision checkout or update to from the repository
78 # Useful for live svn or trunk svn ebuilds allowing the user to peg
79 # to a specific revision
81 # Note: This should never be set in an ebuild!
82 ESVN_REVISION="${ESVN_REVISION:-}"
84 # @ECLASS-VARIABLE: ESVN_USER
87 ESVN_USER="${ESVN_USER:-}"
89 # @ECLASS-VARIABLE: ESVN_PASSWORD
92 ESVN_PASSWORD="${ESVN_PASSWORD:-}"
94 # @ECLASS-VARIABLE: ESVN_PROJECT
96 # project name of your ebuild (= name space)
98 # subversion eclass will check out the subversion repository like:
100 # ${ESVN_STORE_DIR}/${ESVN_PROJECT}/${ESVN_REPO_URI##*/}
102 # so if you define ESVN_REPO_URI as http://svn.collab.net/repo/svn/trunk or
103 # http://svn.collab.net/repo/svn/trunk/. and PN is subversion-svn.
104 # it will check out like:
106 # ${ESVN_STORE_DIR}/subversion/trunk
108 # this is not used in order to declare the name of the upstream project.
109 # so that you can declare this like:
111 # # jakarta commons-loggin
112 # ESVN_PROJECT=commons/logging
114 # default: ${PN/-svn}.
115 ESVN_PROJECT="${ESVN_PROJECT:-${PN/-svn}}"
117 # @ECLASS-VARIABLE: ESVN_BOOTSTRAP
119 # bootstrap script or command like autogen.sh or etc..
120 ESVN_BOOTSTRAP="${ESVN_BOOTSTRAP:-}"
122 # @ECLASS-VARIABLE: ESVN_PATCHES
124 # subversion eclass can apply patches in subversion_bootstrap().
125 # you can use regexp in this variable like *.diff or *.patch or etc.
126 # NOTE: patches will be applied before ESVN_BOOTSTRAP is processed.
128 # Patches are searched both in ${PWD} and ${FILESDIR}, if not found in either
129 # location, the installation dies.
130 ESVN_PATCHES="${ESVN_PATCHES:-}"
132 # @ECLASS-VARIABLE: ESVN_RESTRICT
134 # this should be a space delimited list of subversion eclass features to
137 # don't export the working copy to S.
138 ESVN_RESTRICT="${ESVN_RESTRICT:-}"
140 # @ECLASS-VARIABLE: ESVN_OFFLINE
142 # Set this variable to a non-empty value to disable the automatic updating of
143 # an svn source tree. This is intended to be set outside the subversion source
145 ESVN_OFFLINE="${ESVN_OFFLINE:-${EVCS_OFFLINE}}"
147 # @ECLASS-VARIABLE: ESVN_UMASK
149 # Set this variable to a custom umask. This is intended to be set by users.
150 # By setting this to something like 002, it can make life easier for people
151 # who do development as non-root (but are in the portage group), and then
152 # switch over to building with FEATURES=userpriv. Or vice-versa. Shouldn't
153 # be a security issue here as anyone who has portage group write access
154 # already can screw the system over in more creative ways.
155 ESVN_UMASK="${ESVN_UMASK:-${EVCS_UMASK}}"
157 # @ECLASS-VARIABLE: ESVN_UP_FREQ
159 # Set the minimum number of hours between svn up'ing in any given svn module. This is particularly
160 # useful for split KDE ebuilds where we want to ensure that all submodules are compiled for the same
161 # revision. It should also be kept user overrideable.
162 ESVN_UP_FREQ="${ESVN_UP_FREQ:=}"
164 # @ECLASS-VARIABLE: ESCM_LOGDIR
166 # User configuration variable. If set to a path such as e.g. /var/log/scm any
167 # package inheriting from subversion.eclass will record svn revision to
168 # ${CATEGORY}/${PN}.log in that path in pkg_preinst. This is not supposed to be
169 # set by ebuilds/eclasses. It defaults to empty so users need to opt in.
170 ESCM_LOGDIR="${ESCM_LOGDIR:=}"
172 # @FUNCTION: subversion_fetch
173 # @USAGE: [repo_uri] [destination]
175 # Wrapper function to fetch sources from subversion via svn checkout or svn update,
176 # depending on whether there is an existing working copy in ${ESVN_STORE_DIR}.
178 # Can take two optional parameters:
179 # repo_uri - a repository URI. default is ESVN_REPO_URI.
180 # destination - a check out path in S.
182 local repo_uri="$(subversion__get_repository_uri "${1:-${ESVN_REPO_URI}}")"
183 local revision="$(subversion__get_peg_revision "${1:-${ESVN_REPO_URI}}")"
186 if [[ -z ${repo_uri} ]]; then
187 die "${ESVN}: ESVN_REPO_URI (or specified URI) is empty."
190 [[ -n "${ESVN_REVISION}" ]] && revision="${ESVN_REVISION}"
192 # check for the scheme
193 local scheme="${repo_uri%%:*}"
202 die "${ESVN}: fetch from '${scheme}' is not yet implemented."
206 addread "/etc/subversion"
207 addwrite "${ESVN_STORE_DIR}"
209 if [[ -n "${ESVN_UMASK}" ]]; then
210 eumask_push "${ESVN_UMASK}"
213 if [[ ! -d ${ESVN_STORE_DIR} ]]; then
214 debug-print "${FUNCNAME}: initial checkout. creating subversion directory"
215 mkdir -m 775 -p "${ESVN_STORE_DIR}" || die "${ESVN}: can't mkdir ${ESVN_STORE_DIR}."
218 pushd "${ESVN_STORE_DIR}" >/dev/null || die "${ESVN}: can't chdir to ${ESVN_STORE_DIR}"
220 local wc_path="$(subversion__get_wc_path "${repo_uri}")"
221 local options="${ESVN_OPTIONS} --config-dir ${ESVN_STORE_DIR}/.subversion"
223 [[ -n "${revision}" ]] && options="${options} -r ${revision}"
225 if [[ "${ESVN_OPTIONS}" = *-r* ]]; then
226 ewarn "\${ESVN_OPTIONS} contains -r, this usage is unsupported. Please"
227 ewarn "see \${ESVN_REPO_URI}"
230 if has_version ">=dev-vcs/subversion-1.6.0"; then
231 options="${options} --config-option=config:auth:password-stores="
234 debug-print "${FUNCNAME}: wc_path = \"${wc_path}\""
235 debug-print "${FUNCNAME}: ESVN_OPTIONS = \"${ESVN_OPTIONS}\""
236 debug-print "${FUNCNAME}: options = \"${options}\""
238 if [[ ! -d ${wc_path}/.svn ]]; then
239 if [[ -n ${ESVN_OFFLINE} ]]; then
240 ewarn "ESVN_OFFLINE cannot be used when there is no existing checkout."
243 einfo "subversion check out start -->"
244 einfo " repository: ${repo_uri}${revision:+@}${revision}"
246 debug-print "${FUNCNAME}: ${ESVN_FETCH_CMD} ${options} ${repo_uri}"
248 mkdir -m 775 -p "${ESVN_PROJECT}" || die "${ESVN}: can't mkdir ${ESVN_PROJECT}."
249 cd "${ESVN_PROJECT}" || die "${ESVN}: can't chdir to ${ESVN_PROJECT}"
250 if [[ -n "${ESVN_USER}" ]]; then
251 ${ESVN_FETCH_CMD} ${options} --username "${ESVN_USER}" --password "${ESVN_PASSWORD}" "${repo_uri}" || die "${ESVN}: can't fetch to ${wc_path} from ${repo_uri}."
253 ${ESVN_FETCH_CMD} ${options} "${repo_uri}" || die "${ESVN}: can't fetch to ${wc_path} from ${repo_uri}."
256 elif [[ -n ${ESVN_OFFLINE} ]]; then
257 svn upgrade "${wc_path}" &>/dev/null
258 svn cleanup "${wc_path}" &>/dev/null
259 subversion_wc_info "${repo_uri}" || die "${ESVN}: unknown problem occurred while accessing working copy."
261 if [[ -n ${ESVN_REVISION} && ${ESVN_REVISION} != ${ESVN_WC_REVISION} ]]; then
262 die "${ESVN}: You requested off-line updating and revision ${ESVN_REVISION} but only revision ${ESVN_WC_REVISION} is available locally."
264 einfo "Fetching disabled: Using existing repository copy at revision ${ESVN_WC_REVISION}."
266 svn upgrade "${wc_path}" &>/dev/null
267 svn cleanup "${wc_path}" &>/dev/null
268 subversion_wc_info "${repo_uri}" || die "${ESVN}: unknown problem occurred while accessing working copy."
271 if [[ -n ${ESVN_UP_FREQ} ]]; then
272 if [[ -n ${ESVN_UP_FREQ//[[:digit:]]} ]]; then
273 die "${ESVN}: ESVN_UP_FREQ must be an integer value corresponding to the minimum number of hours between svn up."
274 elif [[ -z $(find "${wc_path}/.svn/entries" -mmin "+$((ESVN_UP_FREQ*60))") ]]; then
275 einfo "Fetching disabled since ${ESVN_UP_FREQ} hours has not passed since last update."
276 einfo "Using existing repository copy at revision ${ESVN_WC_REVISION}."
277 esvn_up_freq=no_update
281 if [[ -z ${esvn_up_freq} ]]; then
282 if [[ ${ESVN_WC_UUID} != $(subversion__svn_info "${repo_uri}" "Repository UUID") ]]; then
283 # UUID mismatch. Delete working copy and check out it again.
284 einfo "subversion recheck out start -->"
285 einfo " old UUID: ${ESVN_WC_UUID}"
286 einfo " new UUID: $(subversion__svn_info "${repo_uri}" "Repository UUID")"
287 einfo " repository: ${repo_uri}${revision:+@}${revision}"
289 rm -fr "${ESVN_PROJECT}" || die
291 debug-print "${FUNCNAME}: ${ESVN_FETCH_CMD} ${options} ${repo_uri}"
293 mkdir -m 775 -p "${ESVN_PROJECT}" || die "${ESVN}: can't mkdir ${ESVN_PROJECT}."
294 cd "${ESVN_PROJECT}" || die "${ESVN}: can't chdir to ${ESVN_PROJECT}"
295 if [[ -n "${ESVN_USER}" ]]; then
296 ${ESVN_FETCH_CMD} ${options} --username "${ESVN_USER}" --password "${ESVN_PASSWORD}" "${repo_uri}" || die "${ESVN}: can't fetch to ${wc_path} from ${repo_uri}."
298 ${ESVN_FETCH_CMD} ${options} "${repo_uri}" || die "${ESVN}: can't fetch to ${wc_path} from ${repo_uri}."
300 elif [[ ${ESVN_WC_URL} != $(subversion__get_repository_uri "${repo_uri}") ]]; then
301 einfo "subversion switch start -->"
302 einfo " old repository: ${ESVN_WC_URL}@${ESVN_WC_REVISION}"
303 einfo " new repository: ${repo_uri}${revision:+@}${revision}"
305 debug-print "${FUNCNAME}: ${ESVN_SWITCH_CMD} ${options} ${repo_uri}"
307 cd "${wc_path}" || die "${ESVN}: can't chdir to ${wc_path}"
308 if [[ -n "${ESVN_USER}" ]]; then
309 ${ESVN_SWITCH_CMD} ${options} --username "${ESVN_USER}" --password "${ESVN_PASSWORD}" ${repo_uri} || die "${ESVN}: can't update ${wc_path} from ${repo_uri}."
311 ${ESVN_SWITCH_CMD} ${options} ${repo_uri} || die "${ESVN}: can't update ${wc_path} from ${repo_uri}."
314 # update working copy
315 einfo "subversion update start -->"
316 einfo " repository: ${repo_uri}${revision:+@}${revision}"
318 debug-print "${FUNCNAME}: ${ESVN_UPDATE_CMD} ${options}"
320 cd "${wc_path}" || die "${ESVN}: can't chdir to ${wc_path}"
321 if [[ -n "${ESVN_USER}" ]]; then
322 ${ESVN_UPDATE_CMD} ${options} --username "${ESVN_USER}" --password "${ESVN_PASSWORD}" || die "${ESVN}: can't update ${wc_path} from ${repo_uri}."
324 ${ESVN_UPDATE_CMD} ${options} || die "${ESVN}: can't update ${wc_path} from ${repo_uri}."
328 # export updated information for the working copy
329 subversion_wc_info "${repo_uri}" || die "${ESVN}: unknown problem occurred while accessing working copy."
333 if [[ -n "${ESVN_UMASK}" ]]; then
337 einfo " working copy: ${wc_path}"
339 if ! has "export" ${ESVN_RESTRICT}; then
340 cd "${wc_path}" || die "${ESVN}: can't chdir to ${wc_path}"
342 local S="${S}/${S_dest}"
345 # export to the ${WORKDIR}
346 #* "svn export" has a bug. see https://bugs.gentoo.org/119236
347 #* svn export . "${S}" || die "${ESVN}: can't export to ${S}."
348 rsync -rlpgo --exclude=".svn/" . "${S}" || die "${ESVN}: can't export to ${S}."
355 # @FUNCTION: subversion_bootstrap
357 # Apply patches in ${ESVN_PATCHES} and run ${ESVN_BOOTSTRAP} if specified.
358 subversion_bootstrap() {
359 if has "export" ${ESVN_RESTRICT}; then
365 if [[ -n ${ESVN_PATCHES} ]]; then
367 einfo "apply patches -->"
368 for patch in ${ESVN_PATCHES}; do
369 if [[ -f ${patch} ]]; then
372 for fpatch in ${FILESDIR}/${patch}; do
373 if [[ -f ${fpatch} ]]; then
376 die "${ESVN}: ${patch} not found"
384 if [[ -n ${ESVN_BOOTSTRAP} ]]; then
385 einfo "begin bootstrap -->"
386 if [[ -f ${ESVN_BOOTSTRAP} && -x ${ESVN_BOOTSTRAP} ]]; then
387 einfo " bootstrap with a file: ${ESVN_BOOTSTRAP}"
388 eval "./${ESVN_BOOTSTRAP}" || die "${ESVN}: can't execute ESVN_BOOTSTRAP."
390 einfo " bootstrap with command: ${ESVN_BOOTSTRAP}"
391 eval "${ESVN_BOOTSTRAP}" || die "${ESVN}: can't eval ESVN_BOOTSTRAP."
396 # @FUNCTION: subversion_wc_info
398 # @RETURN: ESVN_WC_URL, ESVN_WC_ROOT, ESVN_WC_UUID, ESVN_WC_REVISION and ESVN_WC_PATH
400 # Get svn info for the specified repo_uri. The default repo_uri is ESVN_REPO_URI.
402 # The working copy information on the specified repository URI are set to
403 # ESVN_WC_* variables.
404 subversion_wc_info() {
405 local repo_uri="$(subversion__get_repository_uri "${1:-${ESVN_REPO_URI}}")"
406 local wc_path="$(subversion__get_wc_path "${repo_uri}")"
408 debug-print "${FUNCNAME}: repo_uri = ${repo_uri}"
409 debug-print "${FUNCNAME}: wc_path = ${wc_path}"
411 if [[ ! -d ${wc_path} ]]; then
415 export ESVN_WC_URL="$(subversion__svn_info "${wc_path}" "URL")"
416 export ESVN_WC_ROOT="$(subversion__svn_info "${wc_path}" "Repository Root")"
417 export ESVN_WC_UUID="$(subversion__svn_info "${wc_path}" "Repository UUID")"
418 export ESVN_WC_REVISION="$(subversion__svn_info "${wc_path}" "Revision")"
419 export ESVN_WC_PATH="${wc_path}"
422 # @FUNCTION: subversion_src_unpack
424 # Default src_unpack. Fetch and, in older EAPIs, bootstrap.
425 subversion_src_unpack() {
426 subversion_fetch || die "${ESVN}: unknown problem occurred in subversion_fetch."
427 if has "${EAPI:-0}" 0 1; then
428 subversion_bootstrap || die "${ESVN}: unknown problem occurred in subversion_bootstrap."
432 # @FUNCTION: subversion_src_prepare
434 # Default src_prepare. Bootstrap.
435 subversion_src_prepare() {
436 subversion_bootstrap || die "${ESVN}: unknown problem occurred in subversion_bootstrap."
439 # @FUNCTION: subversion_pkg_preinst
442 # Log the svn revision of source code. Doing this in pkg_preinst because we
443 # want the logs to stick around if packages are uninstalled without messing with
445 subversion_pkg_preinst() {
446 has "${EAPI:-0}" 0 1 2 && ! use prefix && EROOT="${ROOT}"
447 local pkgdate=$(date "+%Y%m%d %H:%M:%S")
448 if [[ -n ${ESCM_LOGDIR} ]]; then
449 local dir="${EROOT}/${ESCM_LOGDIR}/${CATEGORY}"
450 if [[ ! -d ${dir} ]]; then
451 mkdir -p "${dir}" || eerror "Failed to create '${dir}' for logging svn revision"
453 local logmessage="svn: ${pkgdate} - ${PF}:${SLOT} was merged at revision ${ESVN_WC_REVISION}"
454 if [[ -d ${dir} ]]; then
455 echo "${logmessage}" >>"${dir}/${PN}.log"
457 eerror "Could not log the message '${logmessage}' to '${dir}/${PN}.log'"
462 ## -- Private Functions
464 ## -- subversion__svn_info() ------------------------------------------------- #
466 # param $1 - a target.
467 # param $2 - a key name.
469 subversion__svn_info() {
473 env LC_ALL=C svn info ${options} --username "${ESVN_USER}" --password "${ESVN_PASSWORD}" "${target}" \
474 | grep -i "^${key}" \
478 ## -- subversion__get_repository_uri() --------------------------------------- #
480 # param $1 - a repository URI.
481 subversion__get_repository_uri() {
482 local repo_uri="${1}"
484 debug-print "${FUNCNAME}: repo_uri = ${repo_uri}"
485 if [[ -z ${repo_uri} ]]; then
486 die "${ESVN}: ESVN_REPO_URI (or specified URI) is empty."
488 # delete trailing slash
489 if [[ -z ${repo_uri##*/} ]]; then
490 repo_uri="${repo_uri%/}"
492 repo_uri="${repo_uri%@*}"
497 ## -- subversion__get_wc_path() ---------------------------------------------- #
499 # param $1 - a repository URI.
500 subversion__get_wc_path() {
501 local repo_uri="$(subversion__get_repository_uri "${1}")"
503 debug-print "${FUNCNAME}: repo_uri = ${repo_uri}"
505 echo "${ESVN_STORE_DIR}/${ESVN_PROJECT}/${repo_uri##*/}"
508 ## -- subversion__get_peg_revision() ----------------------------------------- #
510 # param $1 - a repository URI.
511 subversion__get_peg_revision() {
512 local repo_uri="${1}"
515 debug-print "${FUNCNAME}: repo_uri = ${repo_uri}"
516 # repo_uri has peg revision?
517 if [[ ${repo_uri} = *@* ]]; then
518 peg_rev="${repo_uri##*@}"
519 debug-print "${FUNCNAME}: peg_rev = ${peg_rev}"
521 debug-print "${FUNCNAME}: repo_uri does not have a peg revision."