diff --git a/Perso/INTech/gitUpdate.sh b/Perso/INTech/gitUpdate.sh index 24e9e4f..629642d 100755 --- a/Perso/INTech/gitUpdate.sh +++ b/Perso/INTech/gitUpdate.sh @@ -1,65 +1,71 @@ #!/bin/bash - -GIT_REPO="." +export LC_ALL=POSIX WORK_DIR=$HOME"/.gitAutoPull" +work_array="." + if [ $# -gt 0 ]; then - GIT_REPO="$1" + work_array=$@ fi -GIT_REPO_NAME="$(echo '$GIT_REPO' | tr '/' '_' | tr -d '~')" -CHECK_TIMER_FILE=$WORK_DIR/localCommitsCheck"$GIT_REPO_NAME" +for GIT_REPO in $work_array ; do -cd $GIT_REPO > /dev/null - -if [ $? -ne 0 ]; then - echo $GIT_REPO" Does not exist or is not a directory" - exit -2 -fi - -if [ -z "$(git rev-parse --git-dir 2>/dev/null)" ]; then - echo $GIT_REPO" is not a git repository" - exit -2 -fi - -if [ ! -d $WORK_DIR ]; then - mkdir $WORK_DIR > /dev/null -fi - -if [ ! -d $WORK_DIR ]; then - echo "Could not create work directory, aborting" - exit -4 -fi - -if [ -e $CHECK_TIMER_FILE ]; then - read count < $CHECK_TIMER_FILE - if [ $cout -ne 0 ]; then - echo $(( $count - 1 )) > $CHECK_TIMER_FILE - exit -1 + GIT_REPO_NAME="$(echo '$GIT_REPO' | tr '/' '_' | tr -d '~')" + CHECK_TIMER_FILE=$WORK_DIR/localCommitsCheck"$GIT_REPO_NAME" + + cd $GIT_REPO > /dev/null + + if [ $? -ne 0 ]; then + echo $GIT_REPO" Does not exist or is not a directory" + exit -2 + fi + + if [ -z "$(git rev-parse --git-dir 2>/dev/null)" ]; then + echo $GIT_REPO" is not a git repository" + exit -2 + fi + + if [ ! -d $WORK_DIR ]; then + mkdir $WORK_DIR > /dev/null + fi + + if [ ! -d $WORK_DIR ]; then + echo "Could not create work directory, aborting" + exit -4 + fi + + 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 --dry-run 2>&1 | tail -n 1 | grep -v -e'+')" # 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" + git push 2>&1 + exit 1 + fi else - rm -f $CHECK_TIMER_FILE - fi -fi + if [ -z "localStatus" ]; then + echo "Local and remote diverged, stopping auto-update for 30 iterations..." + echo 30 > $WORK_DIR/localCommitsCheck"$GIT_REPO_NAME" + else + echo "Updating "$GIT_REPO" with remote commits" + git fetch --all 2>&1 + git remote update 2>&1 + exit 1 + fi + fi } | wall -remoteStatus="$(git fetch --dry-run 2>&1)" # 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 2>&1 | 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 2>&1 | wall - git remote update 2>&1 | wall - exit 1 - fi -fi +done exit 0 \ No newline at end of file