freedombone-utils-git 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2016 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. if [[ "$repo_url" == 'ssh:'* ]]; then
  34. if [ "${FRIENDS_MIRRORS_SERVER}" ]; then
  35. if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then
  36. if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then
  37. if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then
  38. echo "sshpass -p \"$FRIENDS_MIRRORS_PASSWORD\" git clone $repo_url $destination_dir"
  39. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone "$repo_url" "$destination_dir"
  40. return
  41. fi
  42. fi
  43. fi
  44. fi
  45. fi
  46. echo "git clone $repo_url $destination_dir"
  47. git clone --recursive "$repo_url" "$destination_dir"
  48. }
  49. function git_pull {
  50. if [ ! $1 ]; then
  51. echo $'git_pull no repo specified'
  52. fi
  53. git merge --abort
  54. git stash
  55. git remote set-url origin $1
  56. git checkout master
  57. if [ "${FRIENDS_MIRRORS_SERVER}" ]; then
  58. if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then
  59. if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then
  60. if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then
  61. sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git pull
  62. if [ $2 ]; then
  63. git checkout $2 -b $2
  64. fi
  65. return
  66. fi
  67. fi
  68. fi
  69. fi
  70. git pull
  71. if [ $2 ]; then
  72. # delete any existing branch
  73. git branch -D $2
  74. # check out the new branch
  75. git checkout $2 -b $2
  76. if [ ! "$?" = "0" ]; then
  77. echo $"Unable to checkout $1 $2"
  78. exit 72357
  79. fi
  80. fi
  81. }
  82. function commit_has_changed {
  83. repo_dir=$1
  84. repo_commit_name=$2
  85. repo_commit=$3
  86. if [ -d $repo_dir ]; then
  87. if grep -q "$repo_commit_name" $COMPLETION_FILE; then
  88. CURRENT_REPO_COMMIT=$(get_completion_param "$repo_commit_name")
  89. if [[ "$CURRENT_REPO_COMMIT" != "$repo_commit" ]]; then
  90. echo "1"
  91. return
  92. fi
  93. else
  94. echo "1"
  95. return
  96. fi
  97. fi
  98. echo "0"
  99. }
  100. # This ensures that a given repo is on a given commit
  101. # If it isn't then it attempts to upgrade
  102. function set_repo_commit {
  103. repo_dir=$1
  104. repo_commit_name=$2
  105. repo_commit=$3
  106. repo_url=$4
  107. if [[ $(commit_has_changed $repo_dir $repo_commit_name $repo_commit) == "1" ]]; then
  108. cd $repo_dir
  109. git_pull $repo_url $repo_commit
  110. # application specific stuff after updating the repo
  111. if [[ $repo_dir == *"www"* ]]; then
  112. chown -R www-data:www-data $repo_dir
  113. fi
  114. if [[ $repo_dir == *"gpgit" ]]; then
  115. cp gpgit.pl /usr/bin/gpgit.pl
  116. fi
  117. if [[ $repo_dir == *"cleanup-maildir" ]]; then
  118. cp $INSTALL_DIR/cleanup-maildir/cleanup-maildir /usr/bin
  119. fi
  120. if [[ $repo_dir == *"nginx_ensite" ]]; then
  121. make install
  122. fi
  123. if [[ $repo_dir == *"inadyn" ]]; then
  124. ./configure
  125. USE_OPENSSL=1 make
  126. make install
  127. systemctl restart inadyn
  128. fi
  129. function_check set_completion_param
  130. set_completion_param "${repo_commit_name}" "${repo_commit}"
  131. fi
  132. }
  133. function configure_firewall_for_git {
  134. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  135. return
  136. fi
  137. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  138. # docker does its own firewalling
  139. return
  140. fi
  141. if [[ $ONION_ONLY != "no" ]]; then
  142. return
  143. fi
  144. firewall_add Git 9418 tcp
  145. mark_completed $FUNCNAME
  146. }
  147. # NOTE: deliberately no exit 0