]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - support/download/git
download/git: ensure we always work in the expected repository
[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 # If the cache directory doesn't exists, init a new repo, which will be
47 # fetch'ed later.
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
51     # passed as argument.
52     _git init "'${git_cache}'"
53 fi
54
55 pushd "${git_cache}" >/dev/null
56
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}'"
60 fi
61
62 _git remote set-url origin "'${uri}'"
63
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.
68 #
69 # Messages for the type of clone used are provided to ease debugging in
70 # case of problems
71 git_done=0
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
75         git_done=1
76     else
77         printf "Shallow fetch failed, falling back to fetching all refs\n"
78     fi
79 fi
80 if [ ${git_done} -eq 0 ]; then
81     printf "Fetching all references\n"
82     _git fetch origin -t
83 fi
84
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}"
94 fi
95
96 # Checkout the required changeset, so that we can update the required
97 # submodules.
98 _git checkout -q "'${cset}'"
99
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 )"
103
104 # There might be submodules, so fetch them.
105 if [ ${recurse} -eq 1 ]; then
106     _git submodule update --init --recursive
107 fi
108
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"
118
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}"
125
126 rm -f "${output}.list"
127 rm -f "${output}.list.sorted"
128
129 popd >/dev/null