freedombone-utils-git 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. fi
  92. fi
  93. fi
  94. echo "0"
  95. }
  96. # This ensures that a given repo is on a given commit
  97. # If it isn't then it attempts to upgrade
  98. function set_repo_commit {
  99. repo_dir=$1
  100. repo_commit_name=$2
  101. repo_commit=$3
  102. repo_url=$4
  103. if [[ $(commit_has_changed $repo_dir $repo_commit_name $repo_commit) == "1" ]]; then
  104. cd $repo_dir
  105. git_pull $repo_url $repo_commit
  106. # application specific stuff after updating the repo
  107. if [[ $repo_dir == *"www"* ]]; then
  108. chown -R www-data:www-data $repo_dir
  109. fi
  110. if [[ $repo_dir == *"gpgit" ]]; then
  111. cp gpgit.pl /usr/bin/gpgit.pl
  112. fi
  113. if [[ $repo_dir == *"cleanup-maildir" ]]; then
  114. cp $INSTALL_DIR/cleanup-maildir/cleanup-maildir /usr/bin
  115. fi
  116. if [[ $repo_dir == *"nginx_ensite" ]]; then
  117. make install
  118. fi
  119. if [[ $repo_dir == *"inadyn" ]]; then
  120. ./configure
  121. USE_OPENSSL=1 make
  122. make install
  123. systemctl restart inadyn
  124. fi
  125. function_check set_completion_param
  126. set_completion_param "${repo_commit_name}" "${repo_commit}"
  127. fi
  128. }
  129. function configure_firewall_for_git {
  130. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  131. return
  132. fi
  133. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  134. # docker does its own firewalling
  135. return
  136. fi
  137. if [[ $ONION_ONLY != "no" ]]; then
  138. return
  139. fi
  140. firewall_add Git 9418 tcp
  141. mark_completed $FUNCNAME
  142. }
  143. # NOTE: deliberately no exit 0