freedombone-utils-git 3.6KB

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