git.sh 1.3 KB

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