1#!/usr/bin/env bash
2
3# NOTE: if the output of this backend has to change (e.g. we change what gets
4# included in the archive, or we change the format of the archive (e.g. tar
5# options, compression ratio or method)), we MUST update the format version
6# in the variable BR_FTM_VERSION_svn, in package/pkg-download.mk.
7
8# We want to catch any unexpected failure, and exit immediately
9set -e
10
11# Download helper for svn, to be called from the download wrapper script
12#
13# Options:
14#   -q          Be quiet.
15#   -o FILE     Generate archive in FILE.
16#   -u URI      Checkout from repository at URI.
17#   -c REV      Use revision REV.
18#   -n NAME     Use basename NAME.
19#   -r          Recursive, i.e. use externals
20#
21# Environment:
22#   SVN      : the svn command to call
23
24# shellcheck disable=SC1090 # Only provides mk_tar_gz()
25. "${0%/*}/helpers"
26
27quiet=
28externals=--ignore-externals
29while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
30    case "${OPT}" in
31    q)  quiet=-q;;
32    o)  output="${OPTARG}";;
33    u)  uri="${OPTARG}";;
34    c)  rev="${OPTARG}";;
35    n)  basename="${OPTARG}";;
36    r)  externals=;;
37    :)  printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
38    \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
39    esac
40done
41
42shift $((OPTIND-1)) # Get rid of our options
43
44# Caller needs to single-quote its arguments to prevent them from
45# being expanded a second time (in case there are spaces in them)
46_svn() {
47    if [ -z "${quiet}" ]; then
48        printf '%s ' "${SVN}" "${@}"; printf '\n'
49    fi
50    _plain_svn "$@"
51}
52# Note: please keep command below aligned with what is printed above
53_plain_svn() {
54    # shellcheck disable=SC2086 # We want word-splitting for SVN
55    eval ${SVN} "${@}"
56}
57
58# shellcheck disable=SC2086 # externals and quiet may be empty
59_svn export --ignore-keywords ${quiet} ${externals} "${@}" "'${uri}@${rev}'" "'${basename}'"
60
61# For 'svn info', we only need the credentials, if any; other options
62# would be invalid, as they are intended for 'svn export'.
63# We can also consume the positional parameters, as we'll no longer
64# be calling any other remote-reaching svn command.
65creds=
66while [ ${#} -gt 0 ]; do
67    case "${1}" in
68    --username=*)   creds+=" ${1}"; shift;;
69    --password=*)   creds+=" ${1}"; shift;;
70    --username)     creds+=" ${1} ${2}"; shift 2;;
71    --password)     creds+=" ${1} ${2}"; shift 2;;
72    *)              shift;;
73    esac
74done
75
76# Get the date of the revision, to generate reproducible archives.
77# The output format is YYYY-MM-DDTHH:MM:SS.mmmuuuZ (i.e. always in the
78# UTC timezone), which we can feed as-is to the --mtime option for tar.
79# In case there is a redirection (e.g. http -> https), just keep the
80# last line (svn outputs everything on stdout)
81# shellcheck disable=SC2086 # creds may be empty
82date="$( _plain_svn info ${creds} "'${uri}@${rev}'" \
83        |sed -r -e '/^Last Changed Date: /!d; s///'
84       )"
85
86# Generate the archive.
87# We did a 'svn export' above, so it's not a working copy (there is no .svn
88# directory or file to ignore).
89mk_tar_gz "${basename}" "${basename}" "${date}" "${output}"
90