diff --git a/src/freedombone-utils-git b/src/freedombone-utils-git new file mode 100755 index 00000000..eceebd0a --- /dev/null +++ b/src/freedombone-utils-git @@ -0,0 +1,134 @@ +#!/bin/bash +# +# .---. . . +# | | | +# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. +# | | (.-' (.-' ( | ( )| | | | )( )| | (.-' +# ' ' --' --' -' - -' ' ' -' -' -' ' - --' +# +# Freedom in the Cloud +# +# Useful git functions +# +# License +# ======= +# +# Copyright (C) 2014-2016 Bob Mottram +# +# 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 . + +NO_OF_ARGS=$# + +PROJECT_NAME='freedombone' + +export TEXTDOMAIN=$PROJECT_NAME-utils-git +export TEXTDOMAINDIR="/usr/share/locale" + +# An optional configuration file which overrides some of these variables +CONFIGURATION_FILE="${PROJECT_NAME}.cfg" + +if [ -f ~/${PROJECT_NAME}.cfg ]; then + CONFIGURATION_FILE=~/${PROJECT_NAME}.cfg +fi + +# friends repo mirrors +FRIENDS_MIRRORS_SERVER= +FRIENDS_MIRRORS_PASSWORD= +FRIENDS_MIRRORS_SSH_PORT= + +function get_friends_servers { + if [ -f $CONFIGURATION_FILE ]; then + if grep -q "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE; then + FRIENDS_MIRRORS_SERVER=$(grep "FRIENDS_MIRRORS_SERVER" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE; then + FRIENDS_MIRRORS_PASSWORD=$(grep "FRIENDS_MIRRORS_PASSWORD" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE; then + FRIENDS_MIRRORS_SSH_PORT=$(grep "FRIENDS_MIRRORS_SSH_PORT" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + fi + + if [ ! $FRIENDS_MIRRORS_SERVER ]; then + echo "1" + return + fi + if [ ${#FRIENDS_MIRRORS_SERVER} -lt 2 ]; then + echo "1" + return + fi + echo "0" +} + +function git_clone { + repo_url="$1" + destination_dir="$2" + + if [[ "$repo_url" == "ssh:"* ]]; then + retval=$(get_friends_servers) + if [[ $retval == "0" ]]; then + if [ "${FRIENDS_MIRRORS_SERVER}" ]; then + if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then + if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then + if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then + echo "sshpass -p \"$FRIENDS_MIRRORS_PASSWORD\" git clone $repo_url $destination_dir" + sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git clone "$repo_url" "$destination_dir" + return + fi + fi + fi + fi + fi + fi + echo "git clone $repo_url $destination_dir" + git clone "$repo_url" "$destination_dir" +} + +function git_pull { + if [ ! $1 ]; then + echo $'git_pull no repo specified' + fi + + git stash + git remote set-url origin $1 + git checkout master + get_friends_servers + if [ "${FRIENDS_MIRRORS_SERVER}" ]; then + if [ ${#FRIENDS_MIRRORS_SERVER} -gt 2 ]; then + if [ "$FRIENDS_MIRRORS_PASSWORD" ]; then + if [ ${#FRIENDS_MIRRORS_PASSWORD} -gt 2 ]; then + sshpass -p "$FRIENDS_MIRRORS_PASSWORD" git pull + if [ $2 ]; then + git checkout $2 -b $2 + fi + return + fi + fi + fi + fi + git pull + + if [ $2 ]; then + # delete any existing branch + git branch -D $2 + # check out the new branch + git checkout $2 -b $2 + if [ ! "$?" = "0" ]; then + echo $"Unable to checkout $1 $2" + exit 72357 + fi + fi +} + +# NOTE: deliberately no exit 0