2012-12-02 22:26:02 +00:00
|
|
|
#!/usr/bin/env bash
|
2012-10-20 11:49:53 +00:00
|
|
|
#
|
|
|
|
# gitwatch - watch file or directory and git commit all changes as they happen
|
|
|
|
#
|
2013-04-18 19:10:48 +00:00
|
|
|
# Copyright (C) 2013 Patrick Lehner
|
2012-11-27 19:10:16 +00:00
|
|
|
# with modifications and contributions by:
|
|
|
|
# - Matthew McGowan
|
2012-12-03 21:30:15 +00:00
|
|
|
# - Dominik D. Geyer
|
2012-10-20 11:49:53 +00:00
|
|
|
#
|
|
|
|
#############################################################################
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#############################################################################
|
|
|
|
#
|
|
|
|
# Idea and original code taken from http://stackoverflow.com/a/965274
|
|
|
|
# (but heavily modified by now)
|
|
|
|
#
|
|
|
|
# Requires the command 'inotifywait' to be available, which is part of
|
2012-11-30 23:58:17 +00:00
|
|
|
# the inotify-tools (See https://github.com/rvoicilas/inotify-tools ),
|
|
|
|
# and (obviously) git
|
2012-10-20 11:49:53 +00:00
|
|
|
#
|
|
|
|
|
2012-11-28 00:02:11 +00:00
|
|
|
REMOTE=""
|
2013-04-18 20:15:29 +00:00
|
|
|
BRANCH=""
|
2013-04-15 21:01:37 +00:00
|
|
|
SLEEP_TIME=2
|
2013-04-16 16:26:07 +00:00
|
|
|
DATE_FMT="+%Y-%m-%d %H:%M:%S"
|
2013-04-18 19:10:48 +00:00
|
|
|
COMMITMSG="Scripted auto-commit on change (%d) by gitwatch.sh"
|
2012-11-28 00:02:11 +00:00
|
|
|
|
2012-11-30 23:58:17 +00:00
|
|
|
shelp () { # Print a message about how to use this script
|
|
|
|
echo "gitwatch - watch file or directory and git commit all changes as they happen"
|
|
|
|
echo ""
|
|
|
|
echo "Usage:"
|
2013-04-18 19:27:06 +00:00
|
|
|
echo "${0##*/} [-s <secs>] [-d <fmt>] [-r <remote> [-b <branch>]]"
|
|
|
|
echo " [-m <msg>] <target>"
|
2012-11-30 23:58:17 +00:00
|
|
|
echo ""
|
|
|
|
echo "Where <target> is the file or folder which should be watched. The target needs"
|
2013-04-18 19:27:06 +00:00
|
|
|
echo "to be in a Git repository, or in the case of a folder, it may also be the top"
|
2012-11-30 23:58:17 +00:00
|
|
|
echo "folder of the repo."
|
2013-04-15 21:01:37 +00:00
|
|
|
echo ""
|
|
|
|
echo " -s <secs> after detecting a change to the watched file or directory,"
|
|
|
|
echo " wait <secs> seconds until committing, to allow for more"
|
|
|
|
echo " write actions of the same batch to finish; default is 2sec"
|
2013-04-16 16:26:07 +00:00
|
|
|
echo " -d <fmt> the format string used for the timestamp in the commit"
|
|
|
|
echo " message; see 'man date' for details; default is "
|
2013-04-18 19:27:06 +00:00
|
|
|
echo " \"+%Y-%m-%d %H:%M:%S\""
|
2013-04-16 16:47:35 +00:00
|
|
|
echo " -r <remote> if defined, a 'git push' to the given <remote> is done after"
|
|
|
|
echo " every commit"
|
2013-04-18 20:15:29 +00:00
|
|
|
echo " -b <branch> the branch which should be pushed automatically;"
|
|
|
|
echo " - if not given, the push command used is 'git push <remote>',"
|
|
|
|
echo " thus doing a default push (see git man pages for details)"
|
|
|
|
echo " - if given and"
|
|
|
|
echo " + repo is in a detached HEAD state (at launch)"
|
|
|
|
echo " then the command used is 'git push <remote> <branch>'"
|
|
|
|
echo " + repo is NOT in a detached HEAD state (at launch)"
|
|
|
|
echo " then the command used is"
|
|
|
|
echo " 'git push <remote> <current branch>:<branch>' where"
|
|
|
|
echo " <current branch> is the target of HEAD (at launch)"
|
|
|
|
echo " if no remote was define with -r, this option has no effect"
|
2013-04-18 19:27:06 +00:00
|
|
|
echo " -m <msg> the commit message used for each commit; all occurences of"
|
|
|
|
echo " %d in the string will be replaced by the formatted date/time"
|
|
|
|
echo " (unless the <fmt> specified by -d is empty, in which case %d"
|
|
|
|
echo " is replaced by an empty string); the default message is:"
|
|
|
|
echo " \"Scripted auto-commit on change (%d) by gitwatch.sh\""
|
2013-04-18 20:15:29 +00:00
|
|
|
echo ""
|
|
|
|
echo "As indicated, several conditions are only checked once at launch of the"
|
|
|
|
echo "script. You can make changes to the repo state and configurations even while"
|
|
|
|
echo "the script is running, but that may lead to undefined and unpredictable (even"
|
|
|
|
echo "destructive) behavior!"
|
|
|
|
echo "It is therefore recommended to terminate the script before changin the repo's"
|
|
|
|
echo "config and restarting it afterwards."
|
2012-11-27 20:03:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 19:27:06 +00:00
|
|
|
while getopts b:d:hm:p:r:s: option # Process command line options
|
2012-11-28 00:02:11 +00:00
|
|
|
do
|
2012-11-30 23:30:57 +00:00
|
|
|
case "${option}" in
|
2012-11-30 23:35:08 +00:00
|
|
|
b) BRANCH=${OPTARG};;
|
2013-04-16 16:26:07 +00:00
|
|
|
d) DATE_FMT=${OPTARG};;
|
2012-11-30 23:30:57 +00:00
|
|
|
h) shelp; exit;;
|
2013-04-18 19:27:06 +00:00
|
|
|
m) COMMITMSG=${OPTARG};;
|
2013-04-16 16:47:35 +00:00
|
|
|
p|r) REMOTE=${OPTARG};;
|
2013-04-15 21:01:37 +00:00
|
|
|
s) SLEEP_TIME=${OPTARG};;
|
2012-11-28 00:02:11 +00:00
|
|
|
esac
|
2012-11-30 23:15:48 +00:00
|
|
|
done
|
2012-11-28 00:02:11 +00:00
|
|
|
|
2012-11-30 23:35:08 +00:00
|
|
|
shift $((OPTIND-1)) # Shift the input arguments, so that the input file (last arg) is $1 in the code below
|
2012-11-30 23:30:57 +00:00
|
|
|
|
2012-12-03 21:30:15 +00:00
|
|
|
if [ $# -ne 1 ]; then # If no command line arguments are left (that's bad: no target was passed)
|
|
|
|
shelp # print usage help
|
|
|
|
exit # and exit
|
2012-12-02 23:20:49 +00:00
|
|
|
fi
|
|
|
|
|
2012-12-03 19:17:27 +00:00
|
|
|
is_command () { # Tests for the availability of a command
|
|
|
|
which $1 &>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check dependencies and die if not met
|
|
|
|
for cmd in git inotifywait; do
|
|
|
|
is_command $cmd || { echo "Error: Required command '$cmd' not found." >&2; exit 1; }
|
|
|
|
done
|
|
|
|
unset cmd
|
2012-11-27 09:04:20 +00:00
|
|
|
|
2012-10-20 11:49:53 +00:00
|
|
|
IN=$(readlink -f "$1")
|
|
|
|
|
|
|
|
if [ -d $1 ]; then
|
2012-12-02 23:29:26 +00:00
|
|
|
TARGETDIR=$(sed -e "s/\/*$//" <<<"$IN") # dir to CD into before using git commands: trim trailing slash, if any
|
2013-04-15 21:13:14 +00:00
|
|
|
INCOMMAND="inotifywait --exclude=\"^${TARGETDIR}/.git\" -qqr -e close_write,move,delete,create $TARGETDIR" # construct inotifywait-commandline
|
2012-11-30 23:35:08 +00:00
|
|
|
GITADD="." # add "." (CWD) recursively to index
|
2012-12-02 22:49:38 +00:00
|
|
|
GITINCOMMAND="-a" # add -a switch to "commit" call just to be sure
|
2012-10-20 11:49:53 +00:00
|
|
|
elif [ -f $1 ]; then
|
2012-12-03 19:00:08 +00:00
|
|
|
TARGETDIR=$(dirname "$IN") # dir to CD into before using git commands: extract from file name
|
2013-04-15 21:13:14 +00:00
|
|
|
INCOMMAND="inotifywait -qq -e close_write,move,delete $IN" # construct inotifywait-commandline
|
2012-11-30 23:35:08 +00:00
|
|
|
GITADD="$IN" # add only the selected file to index
|
|
|
|
GITINCOMMAND="" # no need to add anything more to "commit" call
|
2012-10-20 11:49:53 +00:00
|
|
|
else
|
2012-12-02 23:03:51 +00:00
|
|
|
echo >&2 "Error: The target is neither a regular file nor a directory."
|
|
|
|
exit 1
|
2012-10-20 11:49:53 +00:00
|
|
|
fi
|
|
|
|
|
2013-04-18 20:15:29 +00:00
|
|
|
cd $TARGETDIR # CD into right dir
|
|
|
|
|
|
|
|
#check if we are on a detached HEAD
|
|
|
|
HEADREF=$(git symbolic-ref HEAD 2> /dev/null)
|
|
|
|
if [ $? -eq 0 ]; then # HEAD is not detached
|
|
|
|
PUSH_BRANCH_EXPR="$(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH"
|
|
|
|
else # HEAD is detached
|
|
|
|
PUSH_BRANCH_EXPR="$BRANCH"
|
|
|
|
fi
|
|
|
|
|
2012-10-20 11:49:53 +00:00
|
|
|
while true; do
|
2012-11-30 23:35:08 +00:00
|
|
|
$INCOMMAND # wait for changes
|
2013-04-15 21:01:37 +00:00
|
|
|
sleep $SLEEP_TIME # wait some more seconds to give apps time to write out all changes
|
2013-04-18 19:10:48 +00:00
|
|
|
if [ -n "$DATE_FMT" ]; then
|
|
|
|
DATE=$(date "$DATE_FMT") # construct date-time string
|
|
|
|
fi
|
2012-11-30 23:35:08 +00:00
|
|
|
cd $TARGETDIR # CD into right dir
|
|
|
|
git add $GITADD # add file(s) to index
|
2013-04-18 19:10:48 +00:00
|
|
|
git commit $GITINCOMMAND -m"$(sed "s/%d/$DATE/" <<< "$COMMITMSG")" # construct commit message and commit
|
2012-11-28 00:02:11 +00:00
|
|
|
|
2012-11-30 23:35:08 +00:00
|
|
|
if [ -n "$REMOTE" ]; then # are we pushing to a remote?
|
|
|
|
if [ -z "$BRANCH" ]; then # Do we have a branch set to push to ?
|
2012-12-02 22:51:42 +00:00
|
|
|
git push $REMOTE # Branch not set, push to remote without a branch
|
|
|
|
else
|
2013-04-18 20:15:29 +00:00
|
|
|
git push $REMOTE $PUSH_BRANCH_EXPR # Branch set, push to the remote with the given branch
|
2012-11-28 00:02:11 +00:00
|
|
|
fi
|
|
|
|
fi
|
2012-10-20 11:49:53 +00:00
|
|
|
done
|
2012-11-30 23:35:08 +00:00
|
|
|
|