git 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if [ -n "$(${GIT} ls-remote "${repo}" "${cset}" 2>&1)" ]; then
  30. printf "Doing shallow clone\n"
  31. ${GIT} clone --depth 1 -b "${cset}" --bare "${repo}" "${repodir}"
  32. else
  33. printf "Doing full clone\n"
  34. ${GIT} clone --bare "${repo}" "${repodir}"
  35. fi
  36. ret=1
  37. pushd "${repodir}" >/dev/null
  38. if ${GIT} archive --prefix="${basename}/" -o "${tmp_tar}" \
  39. --format=tar "${cset}"; then
  40. if gzip -c "${tmp_tar}" >"${tmp_output}"; then
  41. mv "${tmp_output}" "${output}"
  42. ret=0
  43. fi
  44. fi
  45. popd >/dev/null
  46. rm -rf "${repodir}" "${tmp_tar}" "${tmp_output}"
  47. exit ${ret}