git 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # We want to catch any command failure, and exit immediately
  3. set -e
  4. # Download helper for git
  5. # Call it with:
  6. # $1: git repo
  7. # $2: git cset
  8. # $3: package's basename (eg. foobar-1.2.3)
  9. # $4: output file
  10. # And this environment:
  11. # GIT : the git command to call
  12. # BUILD_DIR: path to Buildroot's build dir
  13. repo="${1}"
  14. cset="${2}"
  15. basename="${3}"
  16. output="${4}"
  17. repodir="${basename}.tmp-git-checkout"
  18. tmp_tar="$( mktemp "${BUILD_DIR}/.XXXXXX" )"
  19. tmp_output="$( mktemp "${output}.XXXXXX" )"
  20. # Play tic-tac-toe with temp files
  21. # - first, we download to a trashable location (the build-dir)
  22. # - then we create the uncomporessed tarball in tht same trashable location
  23. # - then we create a temporary compressed tarball in the final location, so
  24. # it is on the same filesystem as the final file
  25. # - finally, we atomically rename to the final file
  26. cd "${BUILD_DIR}"
  27. # Remove leftovers from a previous failed run
  28. rm -rf "${repodir}"
  29. git_done=0
  30. if [ -n "$(${GIT} ls-remote "${repo}" "${cset}" 2>&1)" ]; then
  31. printf "Doing shallow clone\n"
  32. if ${GIT} clone --depth 1 -b "${cset}" --bare "${repo}" "${repodir}"; then
  33. git_done=1
  34. fi
  35. fi
  36. if [ ${git_done} -eq 0 ]; then
  37. printf "Doing full clone\n"
  38. ${GIT} clone --bare "${repo}" "${repodir}"
  39. fi
  40. ret=1
  41. pushd "${repodir}" >/dev/null
  42. if ${GIT} archive --prefix="${basename}/" -o "${tmp_tar}" \
  43. --format=tar "${cset}"; then
  44. if gzip -c "${tmp_tar}" >"${tmp_output}"; then
  45. mv "${tmp_output}" "${output}"
  46. ret=0
  47. fi
  48. fi
  49. popd >/dev/null
  50. rm -rf "${repodir}" "${tmp_tar}" "${tmp_output}"
  51. exit ${ret}