freedombone-utils-git 4.6KB

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