From f895a5db275d8c49674961e42fa02e24f19db017 Mon Sep 17 00:00:00 2001 From: imlzo <56844000+imlzo@users.noreply.github.com> Date: Tue, 3 May 2022 14:22:54 -0700 Subject: [PATCH] 106: Option to skip commit if repo has ongoing merge (#107) --- gitwatch.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 82c8820..c9c3910 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -43,6 +43,7 @@ COMMITMSG="Scripted auto-commit on change (%d) by gitwatch.sh" LISTCHANGES=-1 LISTCHANGES_COLOR="--color=always" GIT_DIR="" +SKIP_IF_MERGING=0 # Print a message about how to use this script shelp() { @@ -50,7 +51,7 @@ shelp() { echo "" echo "Usage:" echo "${0##*/} [-s ] [-d ] [-r [-b ]]" - echo " [-m ] [-l|-L ] " + echo " [-m ] [-l|-L ] [-M] " echo "" echo "Where is the file or folder which should be watched. The target needs" echo "to be in a Git repository, or in the case of a folder, it may also be the top" @@ -89,6 +90,7 @@ shelp() { echo " '$EVENTS')" echo " (useful when using inotify-win, e.g. -e modify,delete,move)" echo " (currently ignored on Mac, which only uses default values)" + echo " -M Prevent commits when there is an ongoing merge in the repo" 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" @@ -123,9 +125,14 @@ is_command() { hash "$1" 2> /dev/null } +# Test whether or not current git directory has ongoign merge +is_merging () { + [ -f "$(git rev-parse --git-dir)"/MERGE_HEAD ] +} + ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e: option; do # Process command line options +while getopts b:d:h:g:L:l:m:p:r:s:e:M option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -140,6 +147,7 @@ while getopts b:d:h:g:L:l:m:p:r:s:e: option; do # Process command line options LISTCHANGES_COLOR="" ;; m) COMMITMSG=${OPTARG} ;; + M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; s) SLEEP_TIME=${OPTARG} ;; e) EVENTS=${OPTARG} ;; @@ -362,6 +370,12 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word splitted # shellcheck disable=SC2086 + + if [ "$SKIP_IF_MERGING" -eq 1 ] && is_merging; then + echo "Skipping commit - repo is merging" + exit 0 + fi + $GIT add $GIT_ADD_ARGS # add file(s) to index # shellcheck disable=SC2086 $GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit