freedombone/src/freedombone-utils-git

150 lines
4.5 KiB
Plaintext
Raw Normal View History

2016-06-30 16:00:50 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-06-30 16:00:50 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-06-30 16:00:50 +02:00
#
# Useful git functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-06-30 16:00:50 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function git_clone {
repo_url="$1"
destination_dir="$2"
2016-06-30 16:00:50 +02:00
echo "git clone $repo_url $destination_dir"
2016-11-06 15:03:51 +01:00
git clone --recursive "$repo_url" "$destination_dir"
2016-06-30 16:00:50 +02:00
}
function git_pull {
2018-02-25 23:15:36 +01:00
if [ ! "$1" ]; then
echo $'git_pull no repo specified'
fi
2016-06-30 16:00:50 +02:00
2016-11-12 11:37:48 +01:00
git merge --abort
git stash
2018-02-25 23:15:36 +01:00
git remote set-url origin "$1"
git checkout master
git pull
2016-06-30 16:00:50 +02:00
2018-02-25 23:15:36 +01:00
if [ "$2" ]; then
# delete any existing branch
2018-02-25 23:15:36 +01:00
git branch -D "$2"
# check out the new branch
2018-02-25 23:15:36 +01:00
if ! git checkout "$2" -b "$2"; then
echo $"Unable to checkout $1 $2"
exit 72357
fi
fi
2016-06-30 16:00:50 +02:00
}
2016-10-14 00:13:43 +02:00
function commit_has_changed {
repo_dir=$1
repo_commit_name=$2
repo_commit=$3
2018-02-25 23:15:36 +01:00
if [ -d "$repo_dir" ]; then
if grep -q "$repo_commit_name" "$COMPLETION_FILE"; then
2016-10-16 20:50:56 +02:00
CURRENT_REPO_COMMIT=$(get_completion_param "$repo_commit_name")
if [[ "$CURRENT_REPO_COMMIT" != "$repo_commit" ]]; then
2016-10-14 00:13:43 +02:00
echo "1"
2016-12-21 19:44:26 +01:00
return
2016-10-14 00:13:43 +02:00
fi
2016-12-21 19:44:26 +01:00
else
echo "1"
return
2016-10-14 00:13:43 +02:00
fi
fi
echo "0"
}
2016-10-14 00:13:43 +02:00
# This ensures that a given repo is on a given commit
# If it isn't then it attempts to upgrade
function set_repo_commit {
repo_dir=$1
repo_commit_name=$2
repo_commit=$3
repo_url=$4
2018-02-25 23:15:36 +01:00
if [[ $(commit_has_changed "$repo_dir" "$repo_commit_name" "$repo_commit") == "1" ]]; then
cd "$repo_dir" || exit 3856835
git_pull "$repo_url" "$repo_commit"
2016-10-14 00:13:43 +02:00
# application specific stuff after updating the repo
if [[ $repo_dir == *"www"* ]]; then
2018-02-25 23:15:36 +01:00
chown -R www-data:www-data "$repo_dir"
2016-10-14 00:13:43 +02:00
fi
if [[ $repo_dir == *"gpgit" ]]; then
2017-08-05 22:16:37 +02:00
if [ ! -f /usr/bin/gpgit.pl ]; then
cp gpgit.pl /usr/bin/gpgit.pl
else
HASH1=$(sha256sum gpgit.pl | awk -F ' ' '{print $1}')
HASH2=$(sha256sum /usr/bin/gpgit.pl | awk -F ' ' '{print $1}')
if [[ "$HASH1" != "$HASH2" ]]; then
cp gpgit.pl /usr/bin/gpgit.pl
fi
fi
2016-10-14 00:13:43 +02:00
fi
if [[ $repo_dir == *"cleanup-maildir" ]]; then
2017-08-05 22:16:37 +02:00
if [ ! -f /usr/bin/cleanup-maildir ]; then
2018-02-25 23:15:36 +01:00
cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
2017-08-05 22:16:37 +02:00
else
2018-02-25 23:15:36 +01:00
HASH1=$(sha256sum "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" | awk -F ' ' '{print $1}')
2017-08-05 22:16:37 +02:00
HASH2=$(sha256sum /usr/bin/cleanup-maildir | awk -F ' ' '{print $1}')
if [[ "$HASH1" != "$HASH2" ]]; then
2018-02-25 23:15:36 +01:00
cp "$INSTALL_DIR/cleanup-maildir/cleanup-maildir" /usr/bin
2017-08-05 22:16:37 +02:00
fi
fi
2016-10-14 00:13:43 +02:00
fi
if [[ $repo_dir == *"nginx_ensite" ]]; then
2017-08-05 22:16:37 +02:00
if [ ! -f /usr/local/bin/nginx_ensite ]; then
make install
fi
2016-10-14 00:13:43 +02:00
fi
if [[ $repo_dir == *"inadyn" ]]; then
./configure
USE_OPENSSL=1 make
make install
systemctl restart inadyn
fi
2016-10-16 20:50:56 +02:00
function_check set_completion_param
set_completion_param "${repo_commit_name}" "${repo_commit}"
fi
}
function configure_firewall_for_git {
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
return
fi
if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
# docker does its own firewalling
return
fi
if [[ $ONION_ONLY != "no" ]]; then
return
fi
firewall_add Git 9418 tcp
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
}
2016-06-30 16:00:50 +02:00
# NOTE: deliberately no exit 0