Created
September 13, 2018 09:02
-
-
Save fcharmy/3f60d1b13ad8272d8a5559a476ea1997 to your computer and use it in GitHub Desktop.
auto sync github repository for whole repo or given certain directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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