From: xf0r3m Date: Sat, 23 Sep 2023 18:54:09 +0000 (+0200) Subject: Dodanie komentarzy do kodu skryptu i poprawienie literówki nazwie funkcji. X-Git-Url: https://gitweb.morketsmerke.org/?a=commitdiff_plain;h=7b245bd31443e0076eed7163ca861cce5bc30723;p=sync.git Dodanie komentarzy do kodu skryptu i poprawienie literówki nazwie funkcji. --- diff --git a/sync.sh b/sync.sh index 7e9b71a..f1855ec 100644 --- a/sync.sh +++ b/sync.sh @@ -2,24 +2,30 @@ #Config file: source ~/.sync.d/sync.conf; + #Script file: + +# Check there is a notify-send program installed on the system which notify-send isNotifySend=$? #Function section: +# Check is local directory is a Git repository function is_ldir_a_git_repo() { cd ${LDIR} git status > /dev/null 2>&1; return $?; } +# Check is local directory is a clone of remote directory (just git clone) function is_ldir_a_rdir_clone() { cd ${LDIR}; git remote get-url origin | grep -q ${RDIR} return $?; } +# Push changes to remote repo function update_rdir() { cd ${LDIR}; git add --all; @@ -34,6 +40,7 @@ function update_rdir() { fi } +# Create git repo on local directory function initialize_ldir_git() { cd ${LDIR}; git init -b main; @@ -41,6 +48,7 @@ function initialize_ldir_git() { return $?; } +# Clone local directory from remote repo. Just clone. function clone_rdir() { git clone ssh://${RUSER}@${RSERVER}${RDIR} ${LDIR} if [ $? -eq 0 ]; then @@ -50,23 +58,27 @@ function clone_rdir() { fi } +# Getting info about local dir updates, before pull function get_update_info() { cd ${LDIR}; git remote update > /dev/null 2>&1; } +# Determining on git status hints, there updates for local directory function is_ldir_need_to_update() { cd ${LDIR}; git status | grep -q 'git pull'; return $?; } +# Here is the same as above, but to other side function is_rdir_need_to_update() { cd ${LDIR}; git status | grep -Eq 'git add|git push' return $?; } +# Pulling commits from remote repo function update_ldir() { cd ${LDIR}; git pull > /dev/null 2>&1; @@ -77,7 +89,9 @@ function update_ldir() { fi } -function is_the_conflict() { +# Hard to get this, if u using repos in normal way. The most popular way to get +# this isn't even implemented in this script. For future use, maybe. +function is_ther_conflict() { cd ${LDIR}; git push -u origin main | grep -q 'rejected'; if [ $? -eq 0 ]; then @@ -87,6 +101,8 @@ function is_the_conflict() { fi } +# Simple way to comunicate with user. If u have notify-send command, you get +# notifications, if not just type messages in stdout in terminal. function output() { argv1=$@; icon=$(echo $argv1 | sed 's,\ ,\n,g' | tail -1); @@ -107,47 +123,63 @@ function output() { fi } +# Authentication with PKI is required, for this script, so if don't point any +# key in config file, script will generate one pair and try upload them to the +# server. if [ ! "$KEYFILE" ]; then ssh-keygen -f ${HOME}/id_rsa ssh-copy-id ${SSHOPTS} -i ${HOME}/id_rsa ${RUSER}@${RSERVER} fi +# Check there is a remote directory ssh ${SSHOPTS} ${RUSER}@${RSERVER} "[ -d ${RDIR} ]"; if [ $? -ne 0 ]; then + # If not, create the hole path and initialize remote dir as Git repository. ssh ${SSHOPTS} ${RUSER}@${RSERVER} "mkdir -p ${RDIR}" ssh ${SSHOPTS} ${RUSER}@${RSERVER} "cd ${RDIR} && git init --bare -b main"; + # Empty repo flag empty=0 fi +# Initializing local directory if [ ! -d ${LDIR} ] && [ "$empty" ]; then + # Just create dir structures and initialize them as git repos 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 + # Local dir already exist is_ldir_a_git_repo; if [ $? -eq 0 ]; then + # Local dir is git repo is_ldir_a_rdir_clone; if [ $? -eq 0 ]; then + # Local dir is remote repo clone. Push dir content to remote repo. update_rdir; exit 0; else + # Local dir is other repo. Refusing to use it. output "Local directory is other repository than remote directory." "bad"; exit 1; fi else + # Local dir isn't a repo, initialize them and push first commit. initialize_ldir_git; update_rdir; exit 0; fi elif [ ! -d ${LDIR} ] && [ ! "$empty" ]; then + # Local directory doesn't exist, but remote dir isn't empty. Clone them. clone_rdir; exit 0; fi - +# Getting update info from remote repo get_update_info; +# Determining that need to pull commits is_ldir_need_to_update; ldir_update=$?; +# or push to remote is_rdir_need_to_update; rdir_update=$?; if [ $ldir_update -eq 0 ]; then @@ -158,5 +190,6 @@ elif [ $rdir_update -eq 0 ]; then is_ther_conflict; fi else + # If everything is up to date, nothing to do. output "Everything is up to date." "ok"; fi