support/misc: Adding Vagrant file for provisioning
[buildroot-gz.git] / support / download / git
blobe342ed31aa0bf0361dff93676c1fbabb98f10f99
1 #!/usr/bin/env bash
3 # We want to catch any unexpected failure, and exit immediately
4 set -e
6 # Download helper for git, to be called from the download wrapper script
8 # Call it as:
9 # .../git [-q] OUT_FILE REPO_URL CSET BASENAME
11 # Environment:
12 # GIT : the git command to call
14 verbose=
15 while getopts :q OPT; do
16 case "${OPT}" in
17 q) verbose=-q; exec >/dev/null;;
18 \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
19 esac
20 done
21 shift $((OPTIND-1))
23 output="${1}"
24 repo="${2}"
25 cset="${3}"
26 basename="${4}"
28 # Caller needs to single-quote its arguments to prevent them from
29 # being expanded a second time (in case there are spaces in them)
30 _git() {
31 eval ${GIT} "${@}"
34 # Try a shallow clone, since it is faster than a full clone - but that only
35 # works if the version is a ref (tag or branch). Before trying to do a shallow
36 # clone we check if ${cset} is in the list provided by git ls-remote. If not
37 # we fall back on a full clone.
39 # Messages for the type of clone used are provided to ease debugging in case of
40 # problems
41 git_done=0
42 if [ -n "$(_git ls-remote "'${repo}'" "'${cset}'" 2>&1)" ]; then
43 printf "Doing shallow clone\n"
44 if _git clone ${verbose} --depth 1 -b "'${cset}'" --bare "'${repo}'" "'${basename}'"; then
45 git_done=1
46 else
47 printf "Shallow clone failed, falling back to doing a full clone\n"
50 if [ ${git_done} -eq 0 ]; then
51 printf "Doing full clone\n"
52 _git clone ${verbose} --mirror "'${repo}'" "'${basename}'"
55 GIT_DIR="${basename}" \
56 _git archive --prefix="'${basename}/'" -o "'${output}.tmp'" --format=tar "'${cset}'"
58 gzip <"${output}.tmp" >"${output}"