Created
October 12, 2015 17:33
-
-
Save hilotech/6423897e6491a3d70bc9 to your computer and use it in GitHub Desktop.
auto_push
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
#!/bin/bash | |
# /usr/local/bin/auto_push | |
# Usage: | |
# # auto_push init /path/to/working/dir git@git:path/to.git | |
# # auto_push /path/to/working/dir git@git:path/to.git & | |
# SETUP: | |
# Add a line like below to: /etc/rc.d/rc.local ( a+x ) | |
# auto_push /path/to/working/dir git@git:path/to.git & | |
# Then | |
# # systemctl start rc-local | |
init_f=0 | |
if [[ "${1-}" = 'init' ]]; then | |
init_f=1 | |
shift | |
fi | |
if [[ -n "${1-}" && -d "${1-}" ]]; then | |
WORKING_DIR="${1-}" | |
shift | |
fi | |
if [[ -n "${1-}" ]]; then | |
REMOTE_URI="${1-}" | |
shift | |
fi | |
if [[ -z "${WORKING_DIR-}" || -z "${REMOTE_URI-}" ]]; then | |
echo 'ERROR: insufficient arguments...' | |
exit 1 | |
fi | |
INTERVAL=30 | |
SHORT_NAME="${REMOTE_URI##*/}" | |
SHORT_NAME="${SHORT_NAME%.git}" | |
ME=$( readlink -f $0 ) | |
ME="${ME##*/}" | |
echo "${ME} : spawned a job at $(LANG=C date)" | logger | |
function _init() { | |
[[ ! -d /var/git-dir ]] && mkdir /var/git-dir | |
cd ${WORKING_DIR-} | |
[[ -f .git || -d .git ]] && /bin/rm -rf .git | |
[[ -d /var/git-dir/${SHORT_NAME-}.git ]] \ | |
&& /bin/rm -rf /var/git-dir/${SHORT_NAME-}.git | |
git init --separate-git-dir=/var/git-dir/${SHORT_NAME-}.git ${WORKING_DIR-} | |
git remote add origin ${REMOTE_URI-} | |
} | |
function _push() { | |
cd ${WORKING_DIR-} | |
[[ ! -f .git && ! -d .git ]] && _init | |
git add -A | |
git commit -m "$(LANG=C date)" | |
git push -u origin master | |
} | |
function _do() { | |
_push | |
} | |
if [[ ${init_f-} = 1 ]]; then | |
_init | |
exit | |
fi | |
CR=' | |
' | |
inotifywait \ | |
-e CLOSE_WRITE,MODIFY,ATTRIB,MOVE,MOVED_TO,MOVED_FROM,DELETE \ | |
-mrq ${WORKING_DIR-} \ | |
| while true | |
do | |
buffer='' | |
while read -t ${INTERVAL-} line | |
do | |
buffer="${buffer-}${line-}${CR-}" | |
done | |
if [[ -z "${buffer-}" ]]; then | |
continue | |
fi | |
echo "${ME} : started a job at $(LANG=C date)" | logger | |
( | |
_do "${buffer-}" 1> /dev/null | |
) 2>&1 | logger | |
echo "${ME} : finished a job at $(LANG=C date)" | logger | |
done | |
_EOF_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment