You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
819 B

  1. #!/bin/sh
  2. #
  3. # USAGE: git-short-commit.sh <GIT_URL> <GIT_REF> <GIT_DIR>
  4. #
  5. set -e
  6. error() {
  7. echo "ERROR: ${*}" >&2
  8. exit 1
  9. }
  10. GIT_URL="${1}"
  11. if [ -z "${GIT_URL}" ]; then
  12. error "Git URL not specified"
  13. fi
  14. GIT_REF="${2}"
  15. if [ -z "${GIT_REF}" ]; then
  16. error "Git reference not specified"
  17. fi
  18. GIT_DIR="${3}"
  19. if [ -z "${GIT_DIR}" ]; then
  20. error "Git clone directory not specified"
  21. fi
  22. clean_up() {
  23. rm --force --recursive "${GIT_DIR}"
  24. }
  25. trap clean_up EXIT
  26. git init --quiet "${GIT_DIR}"
  27. (
  28. cd "${GIT_DIR}"
  29. for PREFIX in "" "https://" "http://" "git@"; do
  30. echo "Trying remote '${PREFIX}${GIT_URL}'" >&2
  31. git remote add origin "${PREFIX}${GIT_URL}"
  32. if git fetch --depth 1 origin "${GIT_REF}"; then
  33. git checkout --detach FETCH_HEAD --
  34. git rev-parse --short HEAD
  35. break
  36. fi
  37. git remote remove origin
  38. done
  39. )