From 45c629ce6be9bf86b42f73489cad88b064868fb4 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Tue, 12 Dec 2023 10:40:36 -0600 Subject: [PATCH] Updated feature for notify ignore --- gitwatch.sh | 17 +++++++++--- tests/notify-ignore.bats | 55 +++++++++++++++++++++++++++++++++++++ tests/startup-shutdown.bash | 2 ++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/notify-ignore.bats diff --git a/gitwatch.sh b/gitwatch.sh index c9c3910..a8eb89a 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -51,7 +51,7 @@ shelp() { echo "" echo "Usage:" echo "${0##*/} [-s ] [-d ] [-r [-b ]]" - echo " [-m ] [-l|-L ] [-M] " + echo " [-m ] [-l|-L ] [-x ] [-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" @@ -91,6 +91,7 @@ shelp() { 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 " -x Pattern to exclude from inotifywait" 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" @@ -132,7 +133,7 @@ is_merging () { ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e:M option; do # Process command line options +while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -150,6 +151,7 @@ while getopts b:d:h:g:L:l:m:p:r:s:e:M option; do # Process command line options M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; s) SLEEP_TIME=${OPTARG} ;; + x) EXCLUDE_PATTERN=${OPTARG} ;; e) EVENTS=${OPTARG} ;; *) stderr "Error: Option '${option}' does not exist." @@ -221,12 +223,19 @@ fi if [ -d "$1" ]; then # if the target is a directory TARGETDIR=$(sed -e "s/\/*$//" <<< "$IN") # dir to CD into before using git commands: trim trailing slash, if any + + if [ -z $EXCLUDE_PATTERN ]; then + EXCLUDE_OPTS="'(\.git/|\.git$)'" + else + EXCLUDE_OPTS="'(\.git/|\.git$|$EXCLUDE_PATTERN)'" + fi + # construct inotifywait-commandline if [ "$(uname)" != "Darwin" ]; then - INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") + INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") else # still need to fix EVENTS since it wants them listed one-by-one - INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") + INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") fi GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure diff --git a/tests/notify-ignore.bats b/tests/notify-ignore.bats new file mode 100644 index 0000000..4c39e07 --- /dev/null +++ b/tests/notify-ignore.bats @@ -0,0 +1,55 @@ +#!/usr/bin/env bats + +# This is a testscript using the bats testing framework: +# https://github.com/sstephenson/bats +# To run it, at a command prompt: +# bats testscript.bats + +load startup-shutdown + + +# Test for exclude from notifications. Verify that a subdirectory is ignored from notification. + +function notify_ignore { #@test + + # Start up gitwatch and capture its output + ${BATS_TEST_DIRNAME}/../gitwatch.sh -x test_subdir "$testdir/local/remote" > "$testdir/output.txt" 3>&- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # Create a file, verify that it hasn't been added yet, then commit + cd remote + mkdir test_subdir + + # According to inotify documentation, a race condition results if you write + # to directory too soon after it has been created; hence, a short wait. + sleep 1 + echo "line1" >> file1.txt + + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep $WAITTIME + + # Add second file that we plan to ignore + cd test_subdir + echo "line2" >> file2.txt + + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep $WAITTIME + + cat "$testdir/output.txt" + run git log --name-status --oneline + echo $output + + # Look for files in log: file1 should be there, file2 should not be + run grep "file1.txt" $testdir/output.txt + [ $status -eq 0 ] + + run grep "file2.txt" $testdir/output.txt + [ $status -ne 0 ] +} + + diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash index d141047..943679b 100755 --- a/tests/startup-shutdown.bash +++ b/tests/startup-shutdown.bash @@ -29,6 +29,8 @@ teardown() { kill -9 %1 fg + killall inotifywait + # Also make sure to kill fswatch if on Mac killall fswatch # Make sure gitwatch script gets killed if script stopped background