xref: /OK3568_Linux_fs/buildroot/support/download/bzr (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/usr/bin/env bash
2
3# We want to catch any unexpected failure, and exit immediately
4set -e
5
6# Download helper for bzr, to be called from the download wrapper script
7#
8# Options:
9#   -q          Be quiet
10#   -o FILE     Generate archive in FILE.
11#   -u URI      Clone from repository at URI.
12#   -c CSET     Use changeset (or revision) CSET.
13#   -n NAME     Use basename NAME.
14#
15# Environment:
16#   BZR      : the bzr command to call
17
18
19# HACK: Avoid printing logs to output file
20quiet=-q
21while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
22    case "${OPT}" in
23    q)  quiet=-q;;
24    o)  output="${OPTARG}";;
25    u)  uri="${OPTARG}";;
26    c)  cset="${OPTARG}";;
27    n)  basename="${OPTARG}";;
28    :)  printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
29    \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
30    esac
31done
32
33shift $((OPTIND-1)) # Get rid of our options
34
35# Caller needs to single-quote its arguments to prevent them from
36# being expanded a second time (in case there are spaces in them)
37_bzr() {
38    if [ -z "${quiet}" ]; then
39        printf '%s ' ${BZR} "${@}"; printf '\n'
40    fi
41    _plain_bzr "$@"
42}
43# Note: please keep command below aligned with what is printed above
44_plain_bzr() {
45    eval ${BZR} "${@}"
46}
47
48# --per-file-timestamps comes with bzr-2.2 (released August 2010),
49# so only pass it if bzr is recent enough. We compute versions as:
50# major*1000 + minor
51bzr_min_version=2002
52bzr_version=$(($(bzr --version |
53                 sed -r -n 's/^Bazaar \(bzr\) ([[:digit:]]+)\.([[:digit:]]+)\..*$/\1*1000+\2/p')
54             ))
55
56# If the version is recent enough, we can generate reproducible
57# archives; otherwise, we just hope for the best (as it would
58# be downloaded from the BR mirror if what we generate here does
59# not match the hash we have for it).
60if [ ${bzr_version} -ge ${bzr_min_version} ]; then
61    timestamp_opt="--per-file-timestamps"
62fi
63
64_bzr export ${quiet} --root="'${basename}/'" --format=tgz \
65    ${timestamp_opt} - "${@}" "'${uri}'" -r "'${cset}'" \
66    >"${output}"
67