freedombone-utils-git 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Useful git functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. function git_clone {
  31. repo_url="$1"
  32. destination_dir="$2"
  33. echo "git clone $repo_url $destination_dir"
  34. git clone --recursive "$repo_url" "$destination_dir"
  35. }
  36. function git_pull {
  37. if [ ! "$1" ]; then
  38. echo $'git_pull no repo specified'
  39. fi
  40. git merge --abort
  41. git stash
  42. git remote set-url origin "$1"
  43. git checkout master
  44. git pull
  45. if [ "$2" ]; then
  46. # delete any existing branch
  47. git branch -D "$2"
  48. # check out the new branch
  49. if ! git checkout "$2" -b "$2"; then
  50. echo $"Unable to checkout $1 $2"
  51. exit 72357
  52. fi
  53. fi
  54. }
  55. function commit_has_changed {
  56. repo_dir=$1
  57. repo_commit_name=$2
  58. repo_commit=$3
  59. if [ -d "$repo_dir" ]; then
  60. if grep -q "$repo_commit_name" "$COMPLETION_FILE"; then
  61. CURRENT_REPO_COMMIT=$(get_completion_param "$repo_commit_name")
  62. if [[ "$CURRENT_REPO_COMMIT" != "$repo_commit" ]]; then
  63. echo "1"
  64. return
  65. fi
  66. else
  67. echo "1"
  68. return
  69. fi
  70. fi
  71. echo "0"
  72. }
  73. # This ensures that a given repo is on a given commit
  74. # If it isn't then it attempts to upgrade
  75. function set_repo_commit {
  76. repo_dir=$1
  77. repo_commit_name=$2
  78. repo_commit=$3
  79. repo_url=$4
  80. if [[ $(commit_has_changed "$repo_dir" "$repo_commit_name" "$repo_commit") == "1" ]]; then
  81. cd "$repo_dir" || exit 3856835
  82. git_pull "$repo_url" "$repo_commit"
  83. # application specific stuff after updating the repo
  84. if [[ $repo_dir == *"www"* ]]; then
  85. chown -R www-data:www-data "$repo_dir"
  86. fi
  87. if [[ $repo_dir == *"gpgit" ]]; then
  88. if [ ! -f /usr/bin/gpgit.pl ]; then
  89. cp gpgit.pl /usr/bin/gpgit.pl
  90. else
  91. HASH1=$(sha256sum gpgit.pl | awk -F ' ' '{print $1}')
  92. HASH2=$(sha256sum /usr/bin/gpgit.pl | awk -F ' ' '{print $1}')
  93. if [[ "$HASH1" != "$HASH2" ]]; then
  94. cp gpgit.pl /usr/bin/gpgit.pl
  95. fi
  96. fi
  97. fi
  98. if [[ $repo_dir == *"cleanup-maildir" ]]; then
  99. if [ ! -f /usr/bin/cleanup-maildir ]; then
  100. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  101. else
  102. HASH1=$(sha256sum "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" | awk -F ' ' '{print $1}')
  103. HASH2=$(sha256sum /usr/bin/cleanup-maildir | awk -F ' ' '{print $1}')
  104. if [[ "$HASH1" != "$HASH2" ]]; then
  105. cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
  106. fi
  107. fi
  108. fi
  109. if [[ $repo_dir == *"nginx_ensite" ]]; then
  110. if [ ! -f /usr/local/bin/nginx_ensite ]; then
  111. make install
  112. fi
  113. fi
  114. if [[ $repo_dir == *"inadyn" ]]; then
  115. ./configure
  116. USE_OPENSSL=1 make
  117. make install
  118. systemctl restart inadyn
  119. fi
  120. function_check set_completion_param
  121. set_completion_param "${repo_commit_name}" "${repo_commit}"
  122. fi
  123. }
  124. function configure_firewall_for_git {
  125. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  126. return
  127. fi
  128. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  129. # docker does its own firewalling
  130. return
  131. fi
  132. if [[ $ONION_ONLY != "no" ]]; then
  133. return
  134. fi
  135. firewall_add Git 9418 tcp
  136. mark_completed "${FUNCNAME[0]}"
  137. }
  138. # NOTE: deliberately no exit 0