From b14878e1cae70e64280029a3c10cf46f3c2581f3 Mon Sep 17 00:00:00 2001 From: xf0r3m Date: Sat, 23 Sep 2023 17:32:40 +0200 Subject: [PATCH 1/1] First commit --- sync.d/sync.conf | 11 ++++ sync.sh | 162 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 sync.d/sync.conf create mode 100644 sync.sh diff --git a/sync.d/sync.conf b/sync.d/sync.conf new file mode 100644 index 0000000..94ebdd3 --- /dev/null +++ b/sync.d/sync.conf @@ -0,0 +1,11 @@ +#!/bin/bash + +#Config file: +LDIR=""; +RDIR=""; +RUSER=""; +RSERVER=""; +SSHOPTS=""; +KEYFILE=""; +export GIT_SSH_COMMAND="ssh ${SSHOPTS}"; + diff --git a/sync.sh b/sync.sh new file mode 100644 index 0000000..7e9b71a --- /dev/null +++ b/sync.sh @@ -0,0 +1,162 @@ +#!/bin/bash + +#Config file: +source ~/.sync.d/sync.conf; +#Script file: +which notify-send +isNotifySend=$? + +#Function section: + +function is_ldir_a_git_repo() { + cd ${LDIR} + git status > /dev/null 2>&1; + return $?; +} + +function is_ldir_a_rdir_clone() { + cd ${LDIR}; + git remote get-url origin | grep -q ${RDIR} + return $?; +} + +function update_rdir() { + cd ${LDIR}; + git add --all; + git commit -m "Pushing changes."; + git push -u origin main; + if [ $? -eq 0 ]; then + output "Remote directory is now up to date." "ok"; + return 0; + else + output "Problem ocurred when trying update remote directory" "warn"; + return 1; + fi +} + +function initialize_ldir_git() { + cd ${LDIR}; + git init -b main; + git remote add origin ssh://${RUSER}@${RSERVER}${RDIR}; + return $?; +} + +function clone_rdir() { + git clone ssh://${RUSER}@${RSERVER}${RDIR} ${LDIR} + if [ $? -eq 0 ]; then + output "Local directory was already cloned from remote directory." "ok"; + else + output "Problem ocurred when trying to clone remote directory." "warn"; + fi +} + +function get_update_info() { + cd ${LDIR}; + git remote update > /dev/null 2>&1; +} + +function is_ldir_need_to_update() { + cd ${LDIR}; + git status | grep -q 'git pull'; + return $?; +} + +function is_rdir_need_to_update() { + cd ${LDIR}; + git status | grep -Eq 'git add|git push' + return $?; +} + +function update_ldir() { + cd ${LDIR}; + git pull > /dev/null 2>&1; + if [ $? -eq 0 ]; then + output "Local directory now is up to date." "ok"; + else + output "Problem occured when trying update local directory." "warn"; + fi +} + +function is_the_conflict() { + cd ${LDIR}; + git push -u origin main | grep -q 'rejected'; + if [ $? -eq 0 ]; then + output "Conflict ocurred. There are significant diffrences betwen local and remote dirs. Move changes outside local directory and delete him. Try synchronize dirs once again and put changes back" "bad"; + else + output "Problem ocurred when trying update remote directory." "warn"; + fi +} + +function output() { + argv1=$@; + icon=$(echo $argv1 | sed 's,\ ,\n,g' | tail -1); + msg=$(echo $argv1 | sed -e "s,\ $icon,,g" -e 's/^[[:space:]]*//g'); + + if [ "$icon" = "ok" ]; then + nsIcon="emblem-synchronizing"; + elif [ "$icon" = "warn" ]; then + nsIcon="dialog-warning"; + elif [ "$icon" = "bad" ]; then + nsIcon="process-stop"; + fi + + if [ $isNotifySend -eq 0 ]; then + notify-send "Sync" "$msg" --icon=$nsIcon + else + echo "$msg"; + fi +} + +if [ ! "$KEYFILE" ]; then + ssh-keygen -f ${HOME}/id_rsa + ssh-copy-id ${SSHOPTS} -i ${HOME}/id_rsa ${RUSER}@${RSERVER} +fi + +ssh ${SSHOPTS} ${RUSER}@${RSERVER} "[ -d ${RDIR} ]"; +if [ $? -ne 0 ]; then + ssh ${SSHOPTS} ${RUSER}@${RSERVER} "mkdir -p ${RDIR}" + ssh ${SSHOPTS} ${RUSER}@${RSERVER} "cd ${RDIR} && git init --bare -b main"; + empty=0 +fi + +if [ ! -d ${LDIR} ] && [ "$empty" ]; then + mkdir -p ${LDIR} + initialize_ldir_git; + output "Local directory was already created. Remote directory seems to be empty. Nothing to do. Exiting." "warn"; + exit 0; +elif [ -d ${LDIR} ] && [ "$empty" ]; then + is_ldir_a_git_repo; + if [ $? -eq 0 ]; then + is_ldir_a_rdir_clone; + if [ $? -eq 0 ]; then + update_rdir; + exit 0; + else + output "Local directory is other repository than remote directory." "bad"; + exit 1; + fi + else + initialize_ldir_git; + update_rdir; + exit 0; + fi +elif [ ! -d ${LDIR} ] && [ ! "$empty" ]; then + clone_rdir; + exit 0; +fi + +get_update_info; +is_ldir_need_to_update; +ldir_update=$?; +is_rdir_need_to_update; +rdir_update=$?; +if [ $ldir_update -eq 0 ]; then + update_ldir; +elif [ $rdir_update -eq 0 ]; then + update_rdir; + if [ $? -ne 0 ]; then + is_ther_conflict; + fi +else + output "Everything is up to date." "ok"; +fi -- 2.39.5