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 # Initialise a repository in the git cache. If the repository already
47 # existed, this is a noop, unless the repository was broken, in which
48 # case this magically restores it to working conditions. In the latter
49 # case, we might be missing blobs, but that's not a problem: we'll
50 # fetch what we need later anyway.
52 # We can still go through the wrapper, because 'init' does not use the
53 # path pointed to by GIT_DIR, but really uses the directory passed as
55 _git init "'${git_cache}'"
57 pushd "${git_cache}" >/dev/null
59 # Ensure the repo has an origin (in case a previous run was killed).
60 if ! _git remote |grep -q -E '^origin$'; then
61 _git remote add origin "'${uri}'"
64 _git remote set-url origin "'${uri}'"
66 # Try to fetch with limited depth, since it is faster than a full clone - but
67 # that only works if the version is a ref (tag or branch). Before trying to do
68 # a shallow clone we check if ${cset} is in the list provided by git ls-remote.
69 # If not we fallback to a full fetch.
71 # Messages for the type of clone used are provided to ease debugging in
74 if [ -n "$(_git ls-remote origin "'${cset}'" 2>&1)" ]; then
75 printf "Doing a shallow fetch\n"
76 if _git fetch "${@}" --depth 1 origin "'${cset}'"; then
79 printf "Shallow fetch failed, falling back to fetching all refs\n"
82 if [ ${git_done} -eq 0 ]; then
83 printf "Fetching all references\n"
88 # Try to get the special refs exposed by some forges (pull-requests for
89 # github, changes for gerrit...). There is no easy way to know whether
90 # the cset the user passed us is such a special ref or a tag or a sha1
91 # or whatever else. We'll eventually fail at checking out that cset,
92 # below, if there is an issue anyway. Since most of the cset we're gonna
93 # have to clone are not such special refs, consign the output to oblivion
94 # so as not to alarm unsuspecting users, but still trace it as a warning.
95 if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
96 printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
99 # Checkout the required changeset, so that we can update the required
101 _git checkout -q "'${cset}'"
103 # Get date of commit to generate a reproducible archive.
104 # %cD is RFC2822, so it's fully qualified, with TZ and all.
105 date="$( _git log -1 --pretty=format:%cD )"
107 # There might be submodules, so fetch them.
108 if [ ${recurse} -eq 1 ]; then
109 _git submodule update --init --recursive
112 # Generate the archive, sort with the C locale so that it is reproducible.
113 # We do not want the .git dir; we keep other .git files, in case they are the
114 # only files in their directory.
115 # The .git dir would generate non reproducible tarballs as it depends on
116 # the state of the remote server. It also would generate large tarballs
117 # (gigabytes for some linux trees) when a full clone took place.
118 find . -not -type d \
119 -and -not -path "./.git/*" >"${output}.list"
120 LC_ALL=C sort <"${output}.list" >"${output}.list.sorted"
122 # Create GNU-format tarballs, since that's the format of the tarballs on
123 # sources.buildroot.org and used in the *.hash files
124 tar cf - --transform="s#^\./#${basename}/#" \
125 --numeric-owner --owner=0 --group=0 --mtime="${date}" --format=gnu \
126 -T "${output}.list.sorted" >"${output}.tar"
127 gzip -6 -n <"${output}.tar" >"${output}"
129 rm -f "${output}.list"
130 rm -f "${output}.list.sorted"