kexec-lite: add KEXEC_LITE_LICENSE_FILES
[buildroot-gz.git] / support / download / git
blob792141183523dfb6431ac55a3deafe536ab62bcd
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] [-r] OUT_FILE REPO_URL CSET BASENAME
11 # -q Be quiet.
12 # -r Clone and archive sub-modules.
14 # Environment:
15 # GIT : the git command to call
17 verbose=
18 recurse=0
19 while getopts :qr OPT; do
20 case "${OPT}" in
21 q) verbose=-q; exec >/dev/null;;
22 r) recurse=1;;
23 \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
24 esac
25 done
26 shift $((OPTIND-1))
28 output="${1}"
29 repo="${2}"
30 cset="${3}"
31 basename="${4}"
33 shift 4 # Get rid of our options
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 _git() {
38 eval ${GIT} "${@}"
41 # Try a shallow clone, since it is faster than a full clone - but that only
42 # works if the version is a ref (tag or branch). Before trying to do a shallow
43 # clone we check if ${cset} is in the list provided by git ls-remote. If not
44 # we fall back on a full clone.
46 # Messages for the type of clone used are provided to ease debugging in case of
47 # problems
48 git_done=0
49 if [ -n "$(_git ls-remote "'${repo}'" "'${cset}'" 2>&1)" ]; then
50 printf "Doing shallow clone\n"
51 if _git clone ${verbose} "${@}" --depth 1 -b "'${cset}'" "'${repo}'" "'${basename}'"; then
52 git_done=1
53 else
54 printf "Shallow clone failed, falling back to doing a full clone\n"
57 if [ ${git_done} -eq 0 ]; then
58 printf "Doing full clone\n"
59 _git clone ${verbose} "${@}" "'${repo}'" "'${basename}'"
62 pushd "${basename}" >/dev/null
64 # Try to get the special refs exposed by some forges (pull-requests for
65 # github, changes for gerrit...). There is no easy way to know whether
66 # the cset the user passed us is such a special ref or a tag or a sha1
67 # or whatever else. We'll eventually fail at checking out that cset,
68 # below, if there is an issue anyway. Since most of the cset we're gonna
69 # have to clone are not such special refs, consign the output to oblivion
70 # so as not to alarm unsuspecting users, but still trace it as a warning.
71 if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
72 printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
75 # Checkout the required changeset, so that we can update the required
76 # submodules.
77 _git checkout -q "'${cset}'"
79 # Get date of commit to generate a reproducible archive.
80 # %cD is RFC2822, so it's fully qualified, with TZ and all.
81 date="$( _git log -1 --pretty=format:%cD )"
83 # There might be submodules, so fetch them.
84 if [ ${recurse} -eq 1 ]; then
85 _git submodule update --init --recursive
88 # We do not need the .git dir; we keep other .git files, in case they
89 # are the only files in their directory.
90 rm -rf .git
92 popd >/dev/null
94 # Generate the archive, sort with the C locale so that it is reproducible
95 find "${basename}" -not -type d >"${basename}.list"
96 LC_ALL=C sort <"${basename}.list" >"${basename}.list.sorted"
97 tar cf - --numeric-owner --owner=0 --group=0 --mtime="${date}" \
98 -T "${basename}.list.sorted" >"${output}.tar"
99 gzip -n <"${output}.tar" >"${output}"