From ab71845f55ec4b17ae0c6676da6d62f1f79477fb Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Mon, 20 May 2019 01:08:25 +0200 Subject: [PATCH] =?UTF-8?q?Script=20de=20mise=20=C3=A0=20jour=20pour=20d?= =?UTF-8?q?=C3=A9p=C3=B4ts=20bare?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Perso/INTech/gitUpdate.sh | 60 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 Perso/INTech/gitUpdate.sh diff --git a/Perso/INTech/gitUpdate.sh b/Perso/INTech/gitUpdate.sh new file mode 100755 index 0000000..4feb0fe --- /dev/null +++ b/Perso/INTech/gitUpdate.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +GIT_REPO="." +WORK_DIR="~/.gitAutoPull" + +if [ $# -gt 0 ]; then + GIT_REPO="$1" +fi + +GIT_REPO_NAME="$(echo '$GIT_REPO' | tr '/' '_' | tr -d '~')" +CHECK_TIMER_FILE=$WORK_DIR/localCommitsCheck"$GIT_REPO_NAME" + +if [ ! -d "$GIT_REPO/.git/" ]; then + echo $GIT_REPO" is not a git repository" + exit -2 +fi + +if [ ! -d $WORK_DIR ]; then + mkdir $WORK_DIR +fi + +if [ ! -d $WORK_DIR ]; then + echo "Could not create work directory, aborting" + exit -4 +fi + +cd $GIT_REPO + +if [ -e $CHECK_TIMER_FILE ]; then + read count < $CHECK_TIMER_FILE + if [ $cout -ne 0 ]; then + echo $(( $count - 1 )) > $CHECK_TIMER_FILE + exit -1 + else + rm -f $CHECK_TIMER_FILE + fi +fi + +remoteStatus="$(git fetch --all --dry-run)" # Empty if there are no commits to fetch +localStatus="$(git push -n --porcelain | grep -e'up to date')" # Empty if there are commits to push + +if [ -z "$remoteStatus" ]; then + if [ -z "$localStatus" ]; then + echo "Pushing "$GIT_REPO" updates to origin" | wall + git push | wall + exit 1 + fi +else + if [ -z "localStatus" ]; then + echo "Local and remote diverged, stopping auto-update for 30 iterations..." | wall + echo 30 > $WORK_DIR/localCommitsCheck"$GIT_REPO_NAME" + else + echo "Updating "$GIT_REPO" with remote commits" | wall + git fetch --all | wall + git remote update | wall + exit 1 + fi +fi + +exit 0 \ No newline at end of file