git 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. n) basename="${OPTARG}";;
  26. :) printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
  27. \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
  28. esac
  29. done
  30. shift $((OPTIND-1)) # Get rid of our options
  31. # Caller needs to single-quote its arguments to prevent them from
  32. # being expanded a second time (in case there are spaces in them)
  33. _git() {
  34. eval ${GIT} "${@}"
  35. }
  36. # Try a shallow clone, since it is faster than a full clone - but that only
  37. # works if the version is a ref (tag or branch). Before trying to do a shallow
  38. # clone we check if ${cset} is in the list provided by git ls-remote. If not
  39. # we fall back on a full clone.
  40. #
  41. # Messages for the type of clone used are provided to ease debugging in case of
  42. # problems
  43. git_done=0
  44. if [ -n "$(_git ls-remote "'${uri}'" "'${cset}'" 2>&1)" ]; then
  45. printf "Doing shallow clone\n"
  46. if _git clone ${verbose} "${@}" --depth 1 -b "'${cset}'" "'${uri}'" "'${basename}'"; then
  47. git_done=1
  48. else
  49. printf "Shallow clone failed, falling back to doing a full clone\n"
  50. fi
  51. fi
  52. if [ ${git_done} -eq 0 ]; then
  53. printf "Doing full clone\n"
  54. _git clone ${verbose} "${@}" "'${uri}'" "'${basename}'"
  55. fi
  56. pushd "${basename}" >/dev/null
  57. # Try to get the special refs exposed by some forges (pull-requests for
  58. # github, changes for gerrit...). There is no easy way to know whether
  59. # the cset the user passed us is such a special ref or a tag or a sha1
  60. # or whatever else. We'll eventually fail at checking out that cset,
  61. # below, if there is an issue anyway. Since most of the cset we're gonna
  62. # have to clone are not such special refs, consign the output to oblivion
  63. # so as not to alarm unsuspecting users, but still trace it as a warning.
  64. if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
  65. printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
  66. fi
  67. # Checkout the required changeset, so that we can update the required
  68. # submodules.
  69. _git checkout -q "'${cset}'"
  70. # Get date of commit to generate a reproducible archive.
  71. # %cD is RFC2822, so it's fully qualified, with TZ and all.
  72. date="$( _git log -1 --pretty=format:%cD )"
  73. # There might be submodules, so fetch them.
  74. if [ ${recurse} -eq 1 ]; then
  75. _git submodule update --init --recursive
  76. fi
  77. # We do not want the .git dir; we keep other .git files, in case they
  78. # are the only files in their directory.
  79. # The .git dir would generate non reproducible tarballs as it depends on
  80. # the state of the remote server. It also would generate large tarballs
  81. # (gigabytes for some linux trees) when a full clone took place.
  82. rm -rf .git
  83. popd >/dev/null
  84. # Generate the archive, sort with the C locale so that it is reproducible
  85. find "${basename}" -not -type d >"${basename}.list"
  86. LC_ALL=C sort <"${basename}.list" >"${basename}.list.sorted"
  87. # Create GNU-format tarballs, since that's the format of the tarballs on
  88. # sources.buildroot.org and used in the *.hash files
  89. tar cf - --numeric-owner --owner=0 --group=0 --mtime="${date}" --format=gnu \
  90. -T "${basename}.list.sorted" >"${output}.tar"
  91. gzip -6 -n <"${output}.tar" >"${output}"