freedombone-utils-git 4.5KB

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