2019-03-06 21:38:13 +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
|
|
|
|
#
|
2018-11-30 20:15:56 +00:00
|
|
|
# Copyright (C) 2013-2018 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
|
2014-07-05 14:58:54 +00:00
|
|
|
# - Phil Thompson
|
2018-11-30 20:15:56 +00:00
|
|
|
# - Dave Musicant
|
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/>.
|
|
|
|
#############################################################################
|
|
|
|
#
|
2018-12-31 21:52:11 +00:00
|
|
|
# Idea and original code taken from http://stackoverflow.com/a/965274
|
|
|
|
# original work by Lester Buck
|
2012-10-20 11:49:53 +00:00
|
|
|
# (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 ),
|
2013-04-24 08:22:35 +00:00
|
|
|
# and (obviously) git.
|
|
|
|
# Will check the availability of both commands using the `which` command
|
|
|
|
# and will abort if either command (or `which`) is not found.
|
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"
|
2018-12-30 04:33:10 +00:00
|
|
|
LISTCHANGES=-1
|
|
|
|
LISTCHANGES_COLOR="--color=always"
|
2019-01-09 00:40:11 +00:00
|
|
|
GIT_DIR=""
|
2012-11-28 00:02:11 +00:00
|
|
|
|
2013-11-13 09:57:29 +00:00
|
|
|
# Print a message about how to use this script
|
|
|
|
shelp () {
|
2012-11-30 23:58:17 +00:00
|
|
|
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>]]"
|
2018-12-30 04:33:10 +00:00
|
|
|
echo " [-m <msg>] [-l|-L <lines>] <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 ""
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -s <secs> After detecting a change to the watched file or directory,"
|
2013-04-15 21:01:37 +00:00
|
|
|
echo " wait <secs> seconds until committing, to allow for more"
|
|
|
|
echo " write actions of the same batch to finish; default is 2sec"
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -d <fmt> The format string used for the timestamp in the commit"
|
2013-04-16 16:26:07 +00:00
|
|
|
echo " message; see 'man date' for details; default is "
|
2013-04-18 19:27:06 +00:00
|
|
|
echo " \"+%Y-%m-%d %H:%M:%S\""
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -r <remote> If given and non-empty, a 'git push' to the given <remote>"
|
2014-07-05 15:05:05 +00:00
|
|
|
echo " is done after every commit; default is empty, i.e. no push"
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -b <branch> The branch which should be pushed automatically;"
|
2013-04-18 20:15:29 +00:00
|
|
|
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)"
|
2014-07-05 15:05:05 +00:00
|
|
|
echo " if no remote was defined with -r, this option has no effect"
|
2019-01-09 00:40:11 +00:00
|
|
|
echo " -g <path> Location of the .git directory, if stored elsewhere in"
|
|
|
|
echo " a remote location. This specifies the --git-dir parameter"
|
2019-02-11 09:25:55 +00:00
|
|
|
echo " -l <lines> Log the actual changes made in this commit, up to a given"
|
2018-12-30 04:33:10 +00:00
|
|
|
echo " number of lines, or all lines if 0 is given"
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -L <lines> Same as -l but without colored formatting"
|
2019-02-11 09:25:55 +00:00
|
|
|
echo " -m <msg> The commit message used for each commit; all occurrences of"
|
2013-04-18 19:27:06 +00:00
|
|
|
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\""
|
2018-12-30 10:32:31 +00:00
|
|
|
echo " -e <events> Events passed to inotifywait to watch (defaults to "
|
2014-07-05 15:05:05 +00:00
|
|
|
echo " '$EVENTS')"
|
2014-07-02 13:47:17 +00:00
|
|
|
echo " (useful when using inotify-win, e.g. -e modify,delete,move)"
|
2019-01-12 19:10:24 +00:00
|
|
|
echo " (currently ignored on Mac, which only uses default values)"
|
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!"
|
2019-02-11 09:25:55 +00:00
|
|
|
echo "It is therefore recommended to terminate the script before changing the repo's"
|
2013-04-18 20:15:29 +00:00
|
|
|
echo "config and restarting it afterwards."
|
2013-04-25 15:05:55 +00:00
|
|
|
echo ""
|
2020-12-08 21:25:15 +00:00
|
|
|
echo "By default, gitwatch tries to use the binaries \"git\", \"inotifywait\", and"
|
|
|
|
echo "\"readline\", expecting to find them in the PATH (it uses 'which' to check this"
|
|
|
|
echo "and will abort with an error if they cannot be found). If you want to use"
|
|
|
|
echo "binaries that are named differently and/or located outside of your PATH, you can"
|
|
|
|
echo "define replacements in the environment variables GW_GIT_BIN, GW_INW_BIN, and"
|
|
|
|
echo "GW_RL_BIN for git, inotifywait, and readline, respectively."
|
2012-11-27 20:03:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 09:57:29 +00:00
|
|
|
# print all arguments to stderr
|
2013-04-24 14:22:51 +00:00
|
|
|
stderr () {
|
2013-04-25 21:12:50 +00:00
|
|
|
echo $@ >&2
|
2013-04-24 14:22:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 09:57:29 +00:00
|
|
|
# clean up at end of program, killing the remaining sleep process if it still exists
|
2013-04-25 21:12:50 +00:00
|
|
|
cleanup () {
|
2019-02-15 09:43:24 +00:00
|
|
|
if [[ -n "$SLEEP_PID" ]] && kill -0 "$SLEEP_PID" &>/dev/null; then
|
|
|
|
kill "$SLEEP_PID" &>/dev/null
|
2013-04-25 21:12:50 +00:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2013-11-13 09:57:29 +00:00
|
|
|
# Tests for the availability of a command
|
|
|
|
is_command () {
|
2019-07-26 11:29:22 +00:00
|
|
|
hash "$1" 2>/dev/null
|
2013-11-13 09:57:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
###############################################################################
|
|
|
|
|
2019-02-11 09:29:47 +00:00
|
|
|
while getopts b:d:h:g:L:l:m:p:r:s:e: option # Process command line options
|
|
|
|
do
|
|
|
|
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;;
|
2019-01-09 00:40:11 +00:00
|
|
|
g) GIT_DIR=${OPTARG};;
|
2018-12-30 04:33:10 +00:00
|
|
|
l) LISTCHANGES=${OPTARG};;
|
|
|
|
L) LISTCHANGES=${OPTARG}; LISTCHANGES_COLOR="";;
|
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};;
|
2014-07-02 13:47:17 +00:00
|
|
|
e) EVENTS=${OPTARG};;
|
2019-12-10 18:25:04 +00:00
|
|
|
*) stderr "Error: Option '${option}' does not exist."; shelp; exit 1;;
|
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
|
|
|
|
|
2020-12-08 21:25:15 +00:00
|
|
|
# if custom bin names are given for git, inotifywait, or readlink, use those; otherwise fall back to "git", "inotifywait", and "readlink"
|
2013-04-24 14:17:24 +00:00
|
|
|
if [ -z "$GW_GIT_BIN" ]; then GIT="git"; else GIT="$GW_GIT_BIN"; fi
|
2019-01-12 19:10:24 +00:00
|
|
|
|
|
|
|
if [ -z "$GW_INW_BIN" ]; then
|
|
|
|
# if Mac, use fswatch
|
|
|
|
if [ "$(uname)" != "Darwin" ]; then
|
|
|
|
INW="inotifywait";
|
2020-05-29 23:34:17 +00:00
|
|
|
EVENTS="${EVENTS:-close_write,move,move_self,delete,create,modify}"
|
2019-01-12 19:10:24 +00:00
|
|
|
else
|
|
|
|
INW="fswatch";
|
|
|
|
# default events specified via a mask, see
|
|
|
|
# https://emcrisostomo.github.io/fswatch/doc/1.14.0/fswatch.html/Invoking-fswatch.html#Numeric-Event-Flags
|
|
|
|
# default of 414 = MovedTo + MovedFrom + Renamed + Removed + Updated + Created
|
|
|
|
# = 256 + 128+ 16 + 8 + 4 + 2
|
2020-03-04 11:30:36 +00:00
|
|
|
EVENTS="${EVENTS:---event=414}"
|
2019-01-12 19:10:24 +00:00
|
|
|
fi;
|
|
|
|
else
|
|
|
|
INW="$GW_INW_BIN";
|
|
|
|
fi
|
2013-04-24 14:17:24 +00:00
|
|
|
|
2020-12-08 21:25:15 +00:00
|
|
|
if [ -z "$GW_RL_BIN" ]; then RL="readlink"; else RL="$GW_RL_BIN"; fi
|
|
|
|
|
2013-04-24 14:22:51 +00:00
|
|
|
# Check availability of selected binaries and die if not met
|
2013-04-24 14:17:24 +00:00
|
|
|
for cmd in "$GIT" "$INW"; do
|
2020-12-08 21:26:07 +00:00
|
|
|
is_command "$cmd" || { stderr "Error: Required command '$cmd' not found." ; exit 2; }
|
2012-12-03 19:17:27 +00:00
|
|
|
done
|
|
|
|
unset cmd
|
2012-11-27 09:04:20 +00:00
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
SLEEP_PID="" # pid of timeout subprocess
|
|
|
|
|
|
|
|
trap "cleanup" EXIT # make sure the timeout is killed when exiting script
|
|
|
|
|
|
|
|
|
2013-04-24 08:22:35 +00:00
|
|
|
# Expand the path to the target to absolute path
|
2019-01-12 19:10:24 +00:00
|
|
|
if [ "$(uname)" != "Darwin" ]; then
|
2020-12-08 21:25:15 +00:00
|
|
|
IN=$($RL -f "$1")
|
2019-01-12 19:10:24 +00:00
|
|
|
else
|
2019-07-26 11:49:44 +00:00
|
|
|
if is_command "greadlink"; then
|
|
|
|
IN=$(greadlink -f "$1")
|
|
|
|
else
|
2020-12-08 21:25:15 +00:00
|
|
|
IN=$($RL -f "$1")
|
2019-07-26 11:49:44 +00:00
|
|
|
if [ $? -eq 1 ]; then
|
|
|
|
echo "Seems like your readlink doesn't support '-f'. Running without. Please 'brew install coreutils'."
|
2020-12-08 21:25:15 +00:00
|
|
|
IN=$($RL "$1")
|
2019-07-26 11:49:44 +00:00
|
|
|
fi
|
|
|
|
fi;
|
2019-01-12 19:10:24 +00:00
|
|
|
fi;
|
2012-10-20 11:49:53 +00:00
|
|
|
|
2019-02-11 09:29:47 +00:00
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
if [ -d "$1" ]; then # if the target is a directory
|
2019-01-12 19:10:24 +00:00
|
|
|
|
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
|
2019-01-12 19:10:24 +00:00
|
|
|
# construct inotifywait-commandline
|
|
|
|
if [ "$(uname)" != "Darwin" ]; then
|
2020-08-13 17:25:04 +00:00
|
|
|
INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"")
|
2019-01-12 19:10:24 +00:00
|
|
|
else
|
|
|
|
# still need to fix EVENTS since it wants them listed one-by-one
|
2020-12-08 21:23:35 +00:00
|
|
|
INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"")
|
2019-01-12 19:10:24 +00:00
|
|
|
fi;
|
2013-11-13 10:02:56 +00:00
|
|
|
GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index
|
|
|
|
GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure
|
2019-01-12 19:10:24 +00:00
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
elif [ -f "$1" ]; then # if the target is a single file
|
2019-01-12 19:10:24 +00:00
|
|
|
|
2012-12-03 19:00:08 +00:00
|
|
|
TARGETDIR=$(dirname "$IN") # dir to CD into before using git commands: extract from file name
|
2019-01-12 19:10:24 +00:00
|
|
|
# construct inotifywait-commandline
|
|
|
|
if [ "$(uname)" != "Darwin" ]; then
|
2019-02-15 09:49:19 +00:00
|
|
|
INW_ARGS=("-qm" "-e" "$EVENTS" "$IN")
|
2019-01-12 19:10:24 +00:00
|
|
|
else
|
2019-02-15 09:49:19 +00:00
|
|
|
INW_ARGS=("$EVENTS" "$IN")
|
|
|
|
fi
|
2019-01-12 19:10:24 +00:00
|
|
|
|
2013-04-25 15:05:55 +00:00
|
|
|
GIT_ADD_ARGS="$IN" # add only the selected file to index
|
2013-04-24 08:22:35 +00:00
|
|
|
GIT_COMMIT_ARGS="" # no need to add anything more to "commit" call
|
2012-10-20 11:49:53 +00:00
|
|
|
else
|
2013-04-24 14:22:51 +00:00
|
|
|
stderr "Error: The target is neither a regular file nor a directory."
|
2020-12-08 21:26:07 +00:00
|
|
|
exit 3
|
2012-10-20 11:49:53 +00:00
|
|
|
fi
|
|
|
|
|
2019-01-12 18:38:21 +00:00
|
|
|
# If $GIT_DIR is set, verify that it is a directory, and then add parameters to
|
|
|
|
# git command as need be
|
|
|
|
if [ -n "$GIT_DIR" ]; then
|
|
|
|
|
|
|
|
if [ ! -d "$GIT_DIR" ]; then
|
|
|
|
stderr ".git location is not a directory: $GIT_DIR";
|
2020-12-08 21:26:07 +00:00
|
|
|
exit 4;
|
2019-01-12 18:38:21 +00:00
|
|
|
fi
|
|
|
|
|
2020-06-06 16:25:43 +00:00
|
|
|
GIT="$GIT --no-pager --work-tree $TARGETDIR --git-dir $GIT_DIR"
|
2019-01-09 00:40:11 +00:00
|
|
|
fi
|
|
|
|
|
2013-04-24 14:35:42 +00:00
|
|
|
# Check if commit message needs any formatting (date splicing)
|
2019-02-11 09:25:55 +00:00
|
|
|
if ! grep "%d" > /dev/null <<< "$COMMITMSG"; then # if commitmsg didn't contain %d, grep returns non-zero
|
2013-04-24 14:35:42 +00:00
|
|
|
DATE_FMT="" # empty date format (will disable splicing in the main loop)
|
|
|
|
FORMATTED_COMMITMSG="$COMMITMSG" # save (unchanging) commit message
|
|
|
|
fi
|
|
|
|
|
2019-02-15 10:30:20 +00:00
|
|
|
# CD into right dir
|
2020-12-08 21:26:07 +00:00
|
|
|
cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 5; }
|
2013-04-18 20:15:29 +00:00
|
|
|
|
2013-04-24 14:35:42 +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 ?
|
2019-01-09 00:40:11 +00:00
|
|
|
PUSH_CMD="$GIT push $REMOTE" # Branch not set, push to remote without a branch
|
2013-04-24 14:35:42 +00:00
|
|
|
else
|
|
|
|
# check if we are on a detached HEAD
|
2019-02-15 09:43:24 +00:00
|
|
|
if HEADREF=$($GIT symbolic-ref HEAD 2> /dev/null); then # HEAD is not detached
|
2019-01-09 00:40:11 +00:00
|
|
|
PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH"
|
2013-04-24 14:35:42 +00:00
|
|
|
else # HEAD is detached
|
2019-01-09 00:40:11 +00:00
|
|
|
PUSH_CMD="$GIT push $REMOTE $BRANCH"
|
2013-04-24 14:35:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
PUSH_CMD="" # if not remote is selected, make sure push command is empty
|
2013-04-18 20:15:29 +00:00
|
|
|
fi
|
|
|
|
|
2018-12-30 04:33:10 +00:00
|
|
|
# A function to reduce git diff output to the actual changed content, and insert file line numbers.
|
2018-12-31 21:52:11 +00:00
|
|
|
# Based on "https://stackoverflow.com/a/12179492/199142" by John Mellor
|
2018-12-30 04:33:10 +00:00
|
|
|
diff-lines() {
|
|
|
|
local path=
|
|
|
|
local line=
|
2018-12-30 11:35:55 +00:00
|
|
|
local previous_path=
|
2018-12-30 04:33:10 +00:00
|
|
|
while read; do
|
|
|
|
esc=$'\033'
|
2018-12-30 11:35:55 +00:00
|
|
|
if [[ $REPLY =~ ---\ (a/)?([^[:blank:]$esc]+).* ]]; then
|
|
|
|
previous_path=${BASH_REMATCH[2]}
|
2018-12-30 04:33:10 +00:00
|
|
|
continue
|
|
|
|
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; then
|
|
|
|
path=${BASH_REMATCH[2]}
|
|
|
|
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then
|
|
|
|
line=${BASH_REMATCH[2]}
|
|
|
|
elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then
|
2018-12-30 10:29:48 +00:00
|
|
|
REPLY=${REPLY:0:150} # limit the line width, so it fits in a single line in most git log outputs
|
2018-12-30 11:35:55 +00:00
|
|
|
if [[ "$path" == "/dev/null" ]]; then
|
|
|
|
echo "File $previous_path deleted or moved."
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
echo "$path:$line: $REPLY"
|
|
|
|
fi
|
2018-12-30 04:33:10 +00:00
|
|
|
if [[ ${BASH_REMATCH[2]} != - ]]; then
|
|
|
|
((line++))
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
###############################################################################
|
|
|
|
|
2013-04-24 08:22:35 +00:00
|
|
|
# main program loop: wait for changes and commit them
|
2013-11-13 09:57:29 +00:00
|
|
|
# whenever inotifywait reports a change, we spawn a timer (sleep process) that gives the writing
|
|
|
|
# process some time (in case there are a lot of changes or w/e); if there is already a timer
|
|
|
|
# running when we receive an event, we kill it and start a new one; thus we only commit if there
|
|
|
|
# have been no changes reported during a whole timeout period
|
2019-02-15 09:49:19 +00:00
|
|
|
eval "$INW" "${INW_ARGS[@]}" | while read -r line; do
|
2013-04-25 21:12:50 +00:00
|
|
|
# is there already a timeout process running?
|
|
|
|
if [[ -n "$SLEEP_PID" ]] && kill -0 $SLEEP_PID &>/dev/null; then
|
|
|
|
# kill it and wait for completion
|
|
|
|
kill $SLEEP_PID &>/dev/null || true
|
|
|
|
wait $SLEEP_PID &>/dev/null || true
|
2013-04-18 19:10:48 +00:00
|
|
|
fi
|
2012-11-28 00:02:11 +00:00
|
|
|
|
2013-04-25 21:12:50 +00:00
|
|
|
# start timeout process
|
|
|
|
(
|
2019-02-15 09:43:24 +00:00
|
|
|
sleep "$SLEEP_TIME" # wait some more seconds to give apps time to write out all changes
|
2013-04-25 21:12:50 +00:00
|
|
|
|
|
|
|
if [ -n "$DATE_FMT" ]; then
|
|
|
|
FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message
|
|
|
|
fi
|
2018-12-30 04:33:10 +00:00
|
|
|
|
|
|
|
if [[ "$LISTCHANGES" -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed
|
2019-01-09 00:40:11 +00:00
|
|
|
DIFF_COMMITMSG="$($GIT diff -U0 $LISTCHANGES_COLOR | diff-lines)"
|
2018-12-30 04:33:10 +00:00
|
|
|
LENGTH_DIFF_COMMITMSG=0
|
|
|
|
if [[ "$LISTCHANGES" -ge 1 ]]; then
|
|
|
|
LENGTH_DIFF_COMMITMSG=$(echo -n "$DIFF_COMMITMSG" | grep -c '^')
|
|
|
|
fi
|
|
|
|
if [[ "$LENGTH_DIFF_COMMITMSG" -le $LISTCHANGES ]]; then
|
2018-12-30 11:35:55 +00:00
|
|
|
# Use git diff as the commit msg, unless if files were added or deleted but not modified
|
|
|
|
if [ -n "$DIFF_COMMITMSG" ]; then
|
|
|
|
FORMATTED_COMMITMSG="$DIFF_COMMITMSG"
|
|
|
|
else
|
2019-01-09 00:40:11 +00:00
|
|
|
FORMATTED_COMMITMSG="New files added: $($GIT status -s)"
|
2018-12-30 11:35:55 +00:00
|
|
|
fi
|
2018-12-30 04:33:10 +00:00
|
|
|
else
|
|
|
|
#FORMATTED_COMMITMSG="Many lines were modified. $FORMATTED_COMMITMSG"
|
2019-01-09 00:40:11 +00:00
|
|
|
FORMATTED_COMMITMSG=$($GIT diff --stat | grep '|')
|
2018-12-30 04:33:10 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-02-15 10:30:20 +00:00
|
|
|
# CD into right dir
|
2020-12-08 21:26:07 +00:00
|
|
|
cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 6; }
|
2019-01-02 15:06:54 +00:00
|
|
|
STATUS=$($GIT status -s)
|
|
|
|
if [ -n "$STATUS" ]; then # only commit if status shows tracked changes.
|
2019-01-09 00:40:11 +00:00
|
|
|
$GIT add $GIT_ADD_ARGS # add file(s) to index
|
|
|
|
$GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit
|
2013-04-25 21:12:50 +00:00
|
|
|
|
2019-01-02 15:06:54 +00:00
|
|
|
if [ -n "$PUSH_CMD" ]; then
|
2019-01-09 00:40:11 +00:00
|
|
|
echo "Push command is $PUSH_CMD";
|
2019-01-02 15:06:54 +00:00
|
|
|
eval $PUSH_CMD;
|
|
|
|
fi
|
2018-12-30 10:23:23 +00:00
|
|
|
fi
|
2013-11-13 09:57:29 +00:00
|
|
|
) & # and send into background
|
2013-04-25 21:12:50 +00:00
|
|
|
|
|
|
|
SLEEP_PID=$! # and remember its PID
|
2014-07-03 08:35:30 +00:00
|
|
|
done
|