git 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. # Call it as:
  7. # .../git [-q] OUT_FILE REPO_URL CSET BASENAME
  8. #
  9. # Environment:
  10. # GIT : the git command to call
  11. verbose=
  12. while getopts :q OPT; do
  13. case "${OPT}" in
  14. q) verbose=-q; exec >/dev/null;;
  15. \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
  16. esac
  17. done
  18. shift $((OPTIND-1))
  19. output="${1}"
  20. repo="${2}"
  21. cset="${3}"
  22. basename="${4}"
  23. # Caller needs to single-quote its arguments to prevent them from
  24. # being expanded a second time (in case there are spaces in them)
  25. _git() {
  26. eval ${GIT} "${@}"
  27. }
  28. # Try a shallow clone, since it is faster than a full clone - but that only
  29. # works if the version is a ref (tag or branch). Before trying to do a shallow
  30. # clone we check if ${cset} is in the list provided by git ls-remote. If not
  31. # we fall back on a full clone.
  32. #
  33. # Messages for the type of clone used are provided to ease debugging in case of
  34. # problems
  35. git_done=0
  36. if [ -n "$(_git ls-remote "'${repo}'" "'${cset}'" 2>&1)" ]; then
  37. printf "Doing shallow clone\n"
  38. if _git clone ${verbose} --depth 1 -b "'${cset}'" --bare "'${repo}'" "'${basename}'"; then
  39. git_done=1
  40. else
  41. printf "Shallow clone failed, falling back to doing a full clone\n"
  42. fi
  43. fi
  44. if [ ${git_done} -eq 0 ]; then
  45. printf "Doing full clone\n"
  46. _git clone ${verbose} --mirror "'${repo}'" "'${basename}'"
  47. fi
  48. GIT_DIR="${basename}" \
  49. _git archive --prefix="'${basename}/'" -o "'${output}.tmp'" --format=tar "'${cset}'"
  50. gzip <"${output}.tmp" >"${output}"