2019-05-20 01:08:25 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
GIT_REPO="."
|
2019-05-20 02:36:19 +02:00
|
|
|
WORK_DIR=$HOME"/.gitAutoPull"
|
2019-05-20 01:08:25 +02:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2019-05-20 02:36:19 +02:00
|
|
|
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
|
2019-05-20 01:08:25 +02:00
|
|
|
echo $GIT_REPO" is not a git repository"
|
|
|
|
exit -2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d $WORK_DIR ]; then
|
2019-05-20 02:36:19 +02:00
|
|
|
mkdir $WORK_DIR > /dev/null
|
2019-05-20 01:08:25 +02:00
|
|
|
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
|
|
|
|
|
2019-05-20 02:36:19 +02:00
|
|
|
remoteStatus="$(git fetch --dry-run 2>&1)" # Empty if there are no commits to fetch
|
2019-05-20 01:08:25 +02:00
|
|
|
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
|
2019-05-20 02:36:19 +02:00
|
|
|
git push 2>&1 | wall
|
2019-05-20 01:08:25 +02:00
|
|
|
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
|
2019-05-20 02:36:19 +02:00
|
|
|
git fetch --all 2>&1 | wall
|
|
|
|
git remote update 2>&1 | wall
|
2019-05-20 01:08:25 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|