git 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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=-v
  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. # Try a shallow clone, since it is faster than a full clone - but that only
  24. # works if the version is a ref (tag or branch). Before trying to do a shallow
  25. # clone we check if ${cset} is in the list provided by git ls-remote. If not
  26. # we fall back on a full clone.
  27. #
  28. # Messages for the type of clone used are provided to ease debugging in case of
  29. # problems
  30. git_done=0
  31. if [ -n "$(${GIT} ls-remote "${repo}" "${cset}" 2>&1)" ]; then
  32. printf "Doing shallow clone\n"
  33. if ${GIT} clone ${verbose} --depth 1 -b "${cset}" --bare "${repo}" "${basename}"; then
  34. git_done=1
  35. else
  36. printf "Shallow clone failed, falling back to doing a full clone\n"
  37. fi
  38. fi
  39. if [ ${git_done} -eq 0 ]; then
  40. printf "Doing full clone\n"
  41. ${GIT} clone ${verbose} --bare "${repo}" "${basename}"
  42. fi
  43. GIT_DIR="${basename}" \
  44. ${GIT} archive --prefix="${basename}/" -o "${output}.tmp" --format=tar "${cset}"
  45. gzip <"${output}.tmp" >"${output}"