git 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. # If the cache directory doesn't exists, init a new repo, which will be
  40. # fetch'ed later.
  41. if [ ! -d "${git_cache}" ]; then
  42. # We can still go through the wrapper, because 'init' does not use
  43. # the path pointed to by GIT_DIR, but really uses the directory
  44. # passed as argument.
  45. _git init "'${git_cache}'"
  46. fi
  47. pushd "${git_cache}" >/dev/null
  48. # Ensure the repo has an origin (in case a previous run was killed).
  49. if ! _git remote |grep -q -E '^origin$'; then
  50. _git remote add origin "'${uri}'"
  51. fi
  52. _git remote set-url origin "'${uri}'"
  53. # Try to fetch with limited depth, since it is faster than a full clone - but
  54. # that only works if the version is a ref (tag or branch). Before trying to do
  55. # a shallow clone we check if ${cset} is in the list provided by git ls-remote.
  56. # If not we fallback to a full fetch.
  57. #
  58. # Messages for the type of clone used are provided to ease debugging in
  59. # case of problems
  60. git_done=0
  61. if [ -n "$(_git ls-remote origin "'${cset}'" 2>&1)" ]; then
  62. printf "Doing a shallow fetch\n"
  63. if _git fetch "${@}" --depth 1 origin "'${cset}'"; then
  64. git_done=1
  65. else
  66. printf "Shallow fetch failed, falling back to fetching all refs\n"
  67. fi
  68. fi
  69. if [ ${git_done} -eq 0 ]; then
  70. printf "Fetching all references\n"
  71. _git fetch origin -t
  72. fi
  73. # Try to get the special refs exposed by some forges (pull-requests for
  74. # github, changes for gerrit...). There is no easy way to know whether
  75. # the cset the user passed us is such a special ref or a tag or a sha1
  76. # or whatever else. We'll eventually fail at checking out that cset,
  77. # below, if there is an issue anyway. Since most of the cset we're gonna
  78. # have to clone are not such special refs, consign the output to oblivion
  79. # so as not to alarm unsuspecting users, but still trace it as a warning.
  80. if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
  81. printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
  82. fi
  83. # Checkout the required changeset, so that we can update the required
  84. # submodules.
  85. _git checkout -q "'${cset}'"
  86. # Get date of commit to generate a reproducible archive.
  87. # %cD is RFC2822, so it's fully qualified, with TZ and all.
  88. date="$( _git log -1 --pretty=format:%cD )"
  89. # There might be submodules, so fetch them.
  90. if [ ${recurse} -eq 1 ]; then
  91. _git submodule update --init --recursive
  92. fi
  93. # Generate the archive, sort with the C locale so that it is reproducible.
  94. # We do not want the .git dir; we keep other .git files, in case they are the
  95. # only files in their directory.
  96. # The .git dir would generate non reproducible tarballs as it depends on
  97. # the state of the remote server. It also would generate large tarballs
  98. # (gigabytes for some linux trees) when a full clone took place.
  99. find . -not -type d \
  100. -and -not -path "./.git/*" >"${output}.list"
  101. LC_ALL=C sort <"${output}.list" >"${output}.list.sorted"
  102. # Create GNU-format tarballs, since that's the format of the tarballs on
  103. # sources.buildroot.org and used in the *.hash files
  104. tar cf - --transform="s/^\.\//${basename}\//" \
  105. --numeric-owner --owner=0 --group=0 --mtime="${date}" --format=gnu \
  106. -T "${output}.list.sorted" >"${output}.tar"
  107. gzip -6 -n <"${output}.tar" >"${output}"
  108. rm -f "${output}.list"
  109. rm -f "${output}.list.sorted"
  110. popd >/dev/null