2012-10-20 11:49:53 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# gitwatch - watch file or directory and git commit all changes as they happen
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Patrick Lehner
|
2012-11-27 19:10:16 +00:00
|
|
|
# with modifications and contributions by:
|
|
|
|
# - Matthew McGowan
|
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
|
|
|
|
# the inotify-tools (See https://github.com/rvoicilas/inotify-tools )
|
|
|
|
#
|
|
|
|
|
2012-11-28 00:02:11 +00:00
|
|
|
REMOTE=""
|
|
|
|
BRANCH="master"
|
|
|
|
|
2012-11-30 23:35:08 +00:00
|
|
|
shelp () {
|
2012-11-27 20:03:31 +00:00
|
|
|
echo "usage: $0 target [-p remote [-b branch]]"
|
|
|
|
}
|
|
|
|
|
2012-10-20 11:49:53 +00:00
|
|
|
if [ -z $1 ]; then
|
2012-11-27 20:03:31 +00:00
|
|
|
shelp
|
2012-10-20 11:49:53 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2012-11-28 00:02:11 +00:00
|
|
|
while getopts b:hp: option
|
|
|
|
do
|
2012-11-30 23:30:57 +00:00
|
|
|
case "${option}" in
|
2012-11-30 23:35:08 +00:00
|
|
|
b) BRANCH=${OPTARG};;
|
2012-11-30 23:30:57 +00:00
|
|
|
h) shelp; exit;;
|
2012-11-30 23:35:08 +00:00
|
|
|
p) REMOTE=${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-11-27 09:04:20 +00:00
|
|
|
# Check for both git and inotifywait and generate an error
|
|
|
|
# if either don't exist or you cannot run them
|
|
|
|
|
2012-11-27 18:48:32 +00:00
|
|
|
which git > /dev/null 2>/dev/null
|
2012-11-27 09:04:20 +00:00
|
|
|
if [ $? -eq 1 ]; then
|
|
|
|
echo >&2 "Git not found and it is required to use this script."
|
|
|
|
exit 1;
|
|
|
|
|
|
|
|
fi
|
2012-11-27 18:48:32 +00:00
|
|
|
which inotifywait > /dev/null 2>/dev/null
|
2012-11-27 09:04:20 +00:00
|
|
|
if [ $? -eq 1 ]; then
|
|
|
|
echo >&2 "inotifywait not found and it is required to use this script."
|
|
|
|
exit;
|
|
|
|
fi
|
|
|
|
|
2012-11-30 23:35:08 +00:00
|
|
|
# These two strings are used to construct the commit comment
|
2012-11-19 20:42:52 +00:00
|
|
|
# They're glued together like "<CCPREPEND>(<DATE&TIME>)<CCAPPEND>"
|
2012-11-30 23:35:08 +00:00
|
|
|
# If you don't want to add text before and/or after the date/time, simply
|
2012-11-19 20:42:52 +00:00
|
|
|
# set them to empty strings
|
|
|
|
CCPREPEND="Scripted auto-commit on change "
|
|
|
|
CCAPPEND=" by gitwatch.sh"
|
|
|
|
|
2012-10-20 11:49:53 +00:00
|
|
|
IN=$(readlink -f "$1")
|
|
|
|
|
|
|
|
if [ -d $1 ]; then
|
2012-11-30 23:35:08 +00:00
|
|
|
TARGETDIR=`echo "$IN" | sed -e "s/\/*$//" ` # dir to CD into before using git commands: trim trailing slash, if any
|
|
|
|
INCOMMAND="inotifywait --exclude=\"^${TARGETDIR}/.git\" -qqr -e close_write,moved_to,delete $TARGETDIR" # construct inotifywait-commandline
|
|
|
|
GITADD="." # add "." (CWD) recursively to index
|
|
|
|
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-11-30 23:35:08 +00:00
|
|
|
TARGETDIR=$(dirname $IN) # dir to CD into before using git commands: extract from file name
|
|
|
|
INCOMMAND="inotifywait -qq -e close_write,moved_to,delete $IN" # construct inotifywait-commandline
|
|
|
|
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
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
while true; do
|
2012-11-30 23:35:08 +00:00
|
|
|
$INCOMMAND # wait for changes
|
|
|
|
sleep 2 # wait 2 more seconds to give apps time to write out all changes
|
|
|
|
DATE=`date "+%Y-%m-%d %H:%M:%S"` # construct date-time string
|
|
|
|
cd $TARGETDIR # CD into right dir
|
|
|
|
git add $GITADD # add file(s) to index
|
2012-11-19 20:42:52 +00:00
|
|
|
git commit$GITINCOMMAND -m"${CCPREPEND}(${DATE})${CCAPPEND}" # 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 ?
|
|
|
|
git push $REMOTE # Branch not set, push to remote without a branch
|
2012-11-28 00:02:11 +00:00
|
|
|
else
|
2012-11-30 23:35:08 +00:00
|
|
|
git push $REMOTE $BRANCH # 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
|
|
|
|