Skip to content

Instantly share code, notes, and snippets.

@fcharmy
Created September 13, 2018 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcharmy/3f60d1b13ad8272d8a5559a476ea1997 to your computer and use it in GitHub Desktop.
Save fcharmy/3f60d1b13ad8272d8a5559a476ea1997 to your computer and use it in GitHub Desktop.
auto sync github repository for whole repo or given certain directories
#!/usr/bin/env bash
# To keep updating codes from git repository
#
# feng.charmy@gmail.com
# Nothe: please make sure git config has read permission of repository
#
# Usage ./sync_dags.sh git@github.com:user/repo.git branch_name\
# -t interval_in_seconds\
# -d sparse_checkout_directory(optional) -d more_sparse_checkout_directory ..\
# -e execute_command_after_sync
# -------------------------------------------------------------
# set default value
export REPO=$1
export BRANCH=$2
export SPARSE_CHECKOUT=""
export INTERVAL=600
if [ -z "$REPO" ] || [ -z "$BRANCH" ]; then
echo -e "ERROR\t Repository and branch must be specified!"
exit 1
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i|--interval)
INTERVAL="$2"
shift
shift
;;
-d|--directory)
SPARSE_CHECKOUT=$SPARSE_CHECKOUT"$2\n"
shift
shift
;;
-e|--execute)
CMD="$2"
shift
shift
;;
*)
shift
;;
esac
done
# -----------main--------------
# init git repo and add remote
git init
git remote add origin $REPO
echo -e "SYNC\tAdd Repository $REPO on branch $BRANCH"
# set sparse checkout directory
git config core.sparseCheckout true
echo -e $SPARSE_CHECKOUT > .git/info/sparse-checkout
echo -e "SYNC\tAdd sparse checkout $SPARSE_CHECKOUT"
# keep fetch codes after time interval
while true ; do
git pull -f -q origin $BRANCH > /dev/null
echo $(date) $(git reset --hard $(git log -1 --pretty=format:%h))
bash -c "$CMD"
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment