git 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/env bash
  2. # NOTE: if the output of this backend has to change (e.g. we change what gets
  3. # included in the archive (e.g. LFS), or we change the format of the archive
  4. # (e.g. tar options, compression ratio or method)), we MUST update the format
  5. # version in the variable BR_FMT_VERSION_git, in package/pkg-download.mk.
  6. # We want to catch any unexpected failure
  7. set -e
  8. # Download helper for git, to be called from the download wrapper script
  9. #
  10. # Options:
  11. # -q Be quiet.
  12. # -r Clone and archive sub-modules.
  13. # -o FILE Generate archive in FILE.
  14. # -u URI Clone from repository at URI.
  15. # -c CSET Use changeset CSET.
  16. # -n NAME Use basename NAME.
  17. #
  18. # Environment:
  19. # GIT : the git command to call
  20. . "${0%/*}/helpers"
  21. # Save our path and options in case we need to call ourselves again
  22. myname="${0}"
  23. declare -a OPTS=("${@}")
  24. # This function is called when an error occurs. Its job is to attempt a
  25. # clone from scratch (only once!) in case the git tree is borked, or in
  26. # case an unexpected and unsupported situation arises with submodules
  27. # or uncommitted stuff (e.g. if the user manually mucked around in the
  28. # git cache).
  29. _on_error() {
  30. local ret=${?}
  31. printf "Detected a corrupted git cache.\n" >&2
  32. if ${BR_GIT_BACKEND_FIRST_FAULT:-false}; then
  33. printf "This is the second time in a row; bailing out\n" >&2
  34. exit ${ret}
  35. fi
  36. export BR_GIT_BACKEND_FIRST_FAULT=true
  37. printf "Removing it and starting afresh.\n" >&2
  38. popd >/dev/null
  39. rm -rf "${git_cache}"
  40. exec "${myname}" "${OPTS[@]}" || exit ${ret}
  41. }
  42. quiet=
  43. large_file=0
  44. recurse=0
  45. while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
  46. case "${OPT}" in
  47. q) quiet=-q; exec >/dev/null;;
  48. l) large_file=1;;
  49. r) recurse=1;;
  50. o) output="${OPTARG}";;
  51. u) uri="${OPTARG}";;
  52. c) cset="${OPTARG}";;
  53. d) dl_dir="${OPTARG}";;
  54. n) basename="${OPTARG}";;
  55. :) printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
  56. \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
  57. esac
  58. done
  59. shift $((OPTIND-1)) # Get rid of our options
  60. # Create and cd into the directory that will contain the local git cache
  61. git_cache="${dl_dir}/git"
  62. mkdir -p "${git_cache}"
  63. pushd "${git_cache}" >/dev/null
  64. # Any error now should try to recover
  65. trap _on_error ERR
  66. set -E
  67. # Caller needs to single-quote its arguments to prevent them from
  68. # being expanded a second time (in case there are spaces in them)
  69. _git() {
  70. if [ -z "${quiet}" ]; then
  71. printf '%s ' GIT_DIR="${git_cache}/.git" ${GIT} "${@}"; printf '\n'
  72. fi
  73. _plain_git "$@"
  74. }
  75. # Note: please keep command below aligned with what is printed above
  76. _plain_git() {
  77. eval GIT_DIR="${git_cache}/.git" ${GIT} "${@}"
  78. }
  79. # Create a warning file, that the user should not use the git cache.
  80. # It's ours. Our precious.
  81. cat <<-_EOF_ >"${dl_dir}/git.readme"
  82. IMPORTANT NOTE!
  83. The git tree located in this directory is for the exclusive use
  84. by Buildroot, which uses it as a local cache to reduce bandwidth
  85. usage.
  86. Buildroot *will* trash any changes in that tree whenever it needs
  87. to use it. Buildroot may even remove it in case it detects the
  88. repository may have been damaged or corrupted.
  89. Do *not* work in that directory; your changes will eventually get
  90. lost. Do *not* even use it as a remote, or as the source for new
  91. worktrees; your commits will eventually get lost.
  92. _EOF_
  93. # Initialise a repository in the git cache. If the repository already
  94. # existed, this is a noop, unless the repository was broken, in which
  95. # case this magically restores it to working conditions. In the latter
  96. # case, we might be missing blobs, but that's not a problem: we'll
  97. # fetch what we need later anyway.
  98. #
  99. # We can still go through the wrapper, because 'init' does not use the
  100. # path pointed to by GIT_DIR, but really uses the directory passed as
  101. # argument.
  102. _git init .
  103. # Ensure the repo has an origin (in case a previous run was killed).
  104. if ! _plain_git remote |grep -q -E '^origin$'; then
  105. _git remote add origin "'${uri}'"
  106. fi
  107. _git remote set-url origin "'${uri}'"
  108. printf "Fetching all references\n"
  109. _git fetch origin
  110. _git fetch origin -t
  111. # Try to get the special refs exposed by some forges (pull-requests for
  112. # github, changes for gerrit...). There is no easy way to know whether
  113. # the cset the user passed us is such a special ref or a tag or a sha1
  114. # or whatever else. We'll eventually fail at checking out that cset,
  115. # below, if there is an issue anyway. Since most of the cset we're gonna
  116. # have to clone are not such special refs, consign the output to oblivion
  117. # so as not to alarm unsuspecting users, but still trace it as a warning.
  118. if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
  119. printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
  120. fi
  121. # Check that the changeset does exist. If it does not, re-cloning from
  122. # scratch won't help, so we don't want to trash the repository for a
  123. # missing commit. We just exit without going through the ERR trap.
  124. if ! _git rev-parse --quiet --verify "'${cset}^{commit}'" >/dev/null 2>&1; then
  125. printf "Commit '%s' does not exist in this repository.\n" "${cset}"
  126. exit 1
  127. fi
  128. # The new cset we want to checkout might have different submodules, or
  129. # have sub-dirs converted to/from a submodule. So we would need to
  130. # deregister _current_ submodules before we checkout.
  131. #
  132. # Using "git submodule deinit --all" would remove all the files for
  133. # all submodules, including the corresponding .git files or directories.
  134. # However, it was only introduced with git-1.8.3, which is too recent
  135. # for some enterprise-grade distros.
  136. #
  137. # So, we fall-back to just removing all submodules directories. We do
  138. # not need to be recursive, as removing a submodule will de-facto remove
  139. # its own submodules.
  140. #
  141. # For recent git versions, the repository for submodules is stored
  142. # inside the repository of the super repository, so the following will
  143. # only remove the working copies of submodules, effectively caching the
  144. # submodules.
  145. #
  146. # For older versions however, the repository is stored in the .git/ of
  147. # the submodule directory, so the following will effectively remove the
  148. # the working copy as well as the repository, which means submodules
  149. # will not be cached for older versions.
  150. #
  151. cmd='printf "Deregistering submodule \"%s\"\n" "${path}" && cd .. && rm -rf "${path##*/}"'
  152. _git submodule --quiet foreach "'${cmd}'"
  153. # Checkout the required changeset, so that we can update the required
  154. # submodules.
  155. _git checkout -f -q "'${cset}'"
  156. # Get rid of now-untracked directories (in case a git operation was
  157. # interrupted in a previous run, or to get rid of empty directories
  158. # that were parents of submodules removed above).
  159. _git clean -ffdx
  160. # Get date of commit to generate a reproducible archive.
  161. # %ci is ISO 8601, so it's fully qualified, with TZ and all.
  162. date="$( _plain_git log -1 --pretty=format:%ci )"
  163. # There might be submodules, so fetch them.
  164. if [ ${recurse} -eq 1 ]; then
  165. _git submodule update --init --recursive
  166. # Older versions of git will store the absolute path of the git tree
  167. # in the .git of submodules, while newer versions just use relative
  168. # paths. Detect and fix the older variants to use relative paths, so
  169. # that the archives are reproducible across a wider range of git
  170. # versions. However, we can't do that if git is too old and uses
  171. # full repositories for submodules.
  172. cmd='printf "%s\n" "${path}/"'
  173. for module_dir in $( _plain_git submodule --quiet foreach "'${cmd}'" ); do
  174. [ -f "${module_dir}/.git" ] || continue
  175. relative_dir="$( sed -r -e 's,/+,/,g; s,[^/]+/,../,g' <<<"${module_dir}" )"
  176. sed -r -i -e "s:^gitdir\: $(pwd)/:gitdir\: "${relative_dir}":" "${module_dir}/.git"
  177. done
  178. fi
  179. # If there are large files then fetch them.
  180. if [ ${large_file} -eq 1 ]; then
  181. _git lfs install --local
  182. _git lfs fetch
  183. _git lfs checkout
  184. # If there are also submodules, recurse into them,
  185. if [ ${recurse} -eq 1 ]; then
  186. _git submodule foreach --recursive ${GIT} lfs install --local
  187. _git submodule foreach --recursive ${GIT} lfs fetch
  188. _git submodule foreach --recursive ${GIT} lfs checkout
  189. fi
  190. fi
  191. popd >/dev/null
  192. # Generate the archive.
  193. # We do not want the .git dir; we keep other .git files, in case they are the
  194. # only files in their directory.
  195. # The .git dir would generate non reproducible tarballs as it depends on
  196. # the state of the remote server. It also would generate large tarballs
  197. # (gigabytes for some linux trees) when a full clone took place.
  198. mk_tar_gz "${git_cache}" "${basename}" "${date}" "${output}" ".git/*"