]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - support/download/git
download/git: fix transform regexp for older tar versions
[coffee/buildroot.git] / support / download / git
1 #!/usr/bin/env bash
2
3 # We want to catch any unexpected failure, and exit immediately
4 set -e
5
6 # Download helper for git, to be called from the download wrapper script
7 #
8 # Options:
9 #   -q          Be quiet.
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.
15 #
16 # Environment:
17 #   GIT      : the git command to call
18
19 verbose=
20 recurse=0
21 while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
22     case "${OPT}" in
23     q)  verbose=-q; exec >/dev/null;;
24     r)  recurse=1;;
25     o)  output="${OPTARG}";;
26     u)  uri="${OPTARG}";;
27     c)  cset="${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;;
32     esac
33 done
34
35 shift $((OPTIND-1)) # Get rid of our options
36
37 # We want to check if a cache of the git clone of this repo already exists.
38 git_cache="${dl_dir}/git"
39
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)
42 _git() {
43     eval GIT_DIR="${git_cache}/.git" ${GIT} "${@}"
44 }
45
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.
51 #
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
54 # argument.
55 _git init "'${git_cache}'"
56
57 pushd "${git_cache}" >/dev/null
58
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}'"
62 fi
63
64 _git remote set-url origin "'${uri}'"
65
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.
70 #
71 # Messages for the type of clone used are provided to ease debugging in
72 # case of problems
73 git_done=0
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
77         git_done=1
78     else
79         printf "Shallow fetch failed, falling back to fetching all refs\n"
80     fi
81 fi
82 if [ ${git_done} -eq 0 ]; then
83     printf "Fetching all references\n"
84     _git fetch origin
85     _git fetch origin -t
86 fi
87
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}"
97 fi
98
99 # Checkout the required changeset, so that we can update the required
100 # submodules.
101 _git checkout -q "'${cset}'"
102
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 )"
106
107 # There might be submodules, so fetch them.
108 if [ ${recurse} -eq 1 ]; then
109     _git submodule update --init --recursive
110 fi
111
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"
121
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}"
128
129 rm -f "${output}.list"
130 rm -f "${output}.list.sorted"
131
132 popd >/dev/null