git.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. source "${BASH_SOURCE%/*}/common.sh"
  3. ensure_pwd
  4. ROOT_FOLDER="${PWD}"
  5. # Get a static hash based on the repo url
  6. function get_hash() {
  7. url="${1}"
  8. echo -n "${url}" | sha256sum | awk '{print $1}'
  9. }
  10. if [ -z ${1+x} ]; then
  11. command=""
  12. else
  13. command="$1"
  14. fi
  15. # Clone a repo
  16. if [[ "$command" = "clone" ]]; then
  17. repo="$2"
  18. repo_hash=$(get_hash "${repo}")
  19. write_log "Cloning ${repo} to ${ROOT_FOLDER}/repos/${repo_hash}"
  20. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  21. if [ -d "${repo_dir}" ]; then
  22. write_log "Repo already exists"
  23. exit 0
  24. fi
  25. write_log "Cloning ${repo} to ${repo_dir}"
  26. if ! git clone "${repo}" "${repo_dir}"; then
  27. write_log "Failed to clone repo"
  28. exit 1
  29. fi
  30. write_log "Done"
  31. exit 0
  32. fi
  33. # Update a repo
  34. if [[ "$command" = "update" ]]; then
  35. repo="$2"
  36. repo_hash=$(get_hash "${repo}")
  37. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  38. if [ ! -d "${repo_dir}" ]; then
  39. write_log "Repo does not exist"
  40. exit 1
  41. fi
  42. write_log "Updating ${repo} in ${repo_hash}"
  43. cd "${repo_dir}" || exit
  44. if ! git pull origin master; then
  45. write_log "Failed to update repo"
  46. exit 1
  47. fi
  48. cd "${ROOT_FOLDER}" || exit
  49. write_log "Done"
  50. exit 0
  51. fi
  52. if [[ "$command" = "get_hash" ]]; then
  53. repo="$2"
  54. get_hash "${repo}"
  55. exit
  56. fi