git 4.3 KB

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