git 4.6 KB

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