git.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. # use greadlink instead of readlink on osx
  3. if [[ "$(uname)" == "Darwin" ]]; then
  4. rdlk=greadlink
  5. else
  6. rdlk=readlink
  7. fi
  8. ROOT_FOLDER="$($rdlk -f $(dirname "${BASH_SOURCE[0]}")/..)"
  9. show_help() {
  10. cat <<EOF
  11. app 0.0.1
  12. CLI for managing Tipi apps
  13. Usage: git <command> <repo> [<arguments>]
  14. Commands:
  15. clone Clones a repo in the repo folder
  16. update Updates the repo folder
  17. get_hash Gets the local hash of the repo
  18. EOF
  19. }
  20. # Get a static hash based on the repo url
  21. function get_hash() {
  22. url="${1}"
  23. echo $(echo -n "${url}" | sha256sum | awk '{print $1}')
  24. }
  25. if [ -z ${1+x} ]; then
  26. command=""
  27. else
  28. command="$1"
  29. fi
  30. # Clone a repo
  31. if [[ "$command" = "clone" ]]; then
  32. repo="$2"
  33. repo_hash=$(get_hash "${repo}")
  34. echo "Cloning ${repo} to ${ROOT_FOLDER}/repos/${repo_hash}"
  35. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  36. if [ -d "${repo_dir}" ]; then
  37. echo "Repo already exists"
  38. exit 0
  39. fi
  40. echo "Cloning ${repo} to ${repo_dir}"
  41. git clone "${repo}" "${repo_dir}"
  42. echo "Done"
  43. exit
  44. fi
  45. # Update a repo
  46. if [[ "$command" = "update" ]]; then
  47. repo="$2"
  48. repo_hash=$(get_hash "${repo}")
  49. repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
  50. if [ ! -d "${repo_dir}" ]; then
  51. echo "Repo does not exist"
  52. exit 0
  53. fi
  54. echo "Updating ${repo} in ${repo_hash}"
  55. cd "${repo_dir}"
  56. git pull origin master
  57. echo "Done"
  58. exit
  59. fi
  60. if [[ "$command" = "get_hash" ]]; then
  61. repo="$2"
  62. echo $(get_hash "${repo}")
  63. exit
  64. fi