3 # We want to catch any unexpected failure, and exit immediately
6 # Download helper for git, to be called from the download wrapper script
10 # -r Clone and archive sub-modules.
11 # -o FILE Generate archive in FILE.
12 # -u URI Clone from repository at URI.
13 # -c CSET Use changeset CSET.
14 # -n NAME Use basename NAME.
17 # GIT : the git command to call
21 while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
23 q) verbose=-q; exec >/dev/null;;
25 o) output="${OPTARG}";;
28 d) dl_dir="${OPTARG}";;
29 n) basename="${OPTARG}";;
30 :) printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
31 \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
35 shift $((OPTIND-1)) # Get rid of our options
37 # We want to check if a cache of the git clone of this repo already exists.
38 git_cache="${dl_dir}/git"
40 # Caller needs to single-quote its arguments to prevent them from
41 # being expanded a second time (in case there are spaces in them)
43 eval GIT_DIR="${git_cache}/.git" ${GIT} "${@}"
46 # If the cache directory doesn't exists, init a new repo, which will be
48 if [ ! -d "${git_cache}" ]; then
49 # We can still go through the wrapper, because 'init' does not use
50 # the path pointed to by GIT_DIR, but really uses the directory
52 _git init "'${git_cache}'"
55 pushd "${git_cache}" >/dev/null
57 # Ensure the repo has an origin (in case a previous run was killed).
58 if ! _git remote |grep -q -E '^origin$'; then
59 _git remote add origin "'${uri}'"
62 _git remote set-url origin "'${uri}'"
64 # Try to fetch with limited depth, since it is faster than a full clone - but
65 # that only works if the version is a ref (tag or branch). Before trying to do
66 # a shallow clone we check if ${cset} is in the list provided by git ls-remote.
67 # If not we fallback to a full fetch.
69 # Messages for the type of clone used are provided to ease debugging in
72 if [ -n "$(_git ls-remote origin "'${cset}'" 2>&1)" ]; then
73 printf "Doing a shallow fetch\n"
74 if _git fetch "${@}" --depth 1 origin "'${cset}'"; then
77 printf "Shallow fetch failed, falling back to fetching all refs\n"
80 if [ ${git_done} -eq 0 ]; then
81 printf "Fetching all references\n"
85 # Try to get the special refs exposed by some forges (pull-requests for
86 # github, changes for gerrit...). There is no easy way to know whether
87 # the cset the user passed us is such a special ref or a tag or a sha1
88 # or whatever else. We'll eventually fail at checking out that cset,
89 # below, if there is an issue anyway. Since most of the cset we're gonna
90 # have to clone are not such special refs, consign the output to oblivion
91 # so as not to alarm unsuspecting users, but still trace it as a warning.
92 if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
93 printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
96 # Checkout the required changeset, so that we can update the required
98 _git checkout -q "'${cset}'"
100 # Get date of commit to generate a reproducible archive.
101 # %cD is RFC2822, so it's fully qualified, with TZ and all.
102 date="$( _git log -1 --pretty=format:%cD )"
104 # There might be submodules, so fetch them.
105 if [ ${recurse} -eq 1 ]; then
106 _git submodule update --init --recursive
109 # Generate the archive, sort with the C locale so that it is reproducible.
110 # We do not want the .git dir; we keep other .git files, in case they are the
111 # only files in their directory.
112 # The .git dir would generate non reproducible tarballs as it depends on
113 # the state of the remote server. It also would generate large tarballs
114 # (gigabytes for some linux trees) when a full clone took place.
115 find . -not -type d \
116 -and -not -path "./.git/*" >"${output}.list"
117 LC_ALL=C sort <"${output}.list" >"${output}.list.sorted"
119 # Create GNU-format tarballs, since that's the format of the tarballs on
120 # sources.buildroot.org and used in the *.hash files
121 tar cf - --transform="s/^\.\//${basename}\//" \
122 --numeric-owner --owner=0 --group=0 --mtime="${date}" --format=gnu \
123 -T "${output}.list.sorted" >"${output}.tar"
124 gzip -6 -n <"${output}.tar" >"${output}"
126 rm -f "${output}.list"
127 rm -f "${output}.list.sorted"