From e493af3336d89185da27e6369ea3bd40ec7209c4 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Wed, 2 Jan 2019 09:06:54 -0600 Subject: [PATCH] Status check is back in to prevent extraneous commit attempts; test added as well. --- gitwatch.sh | 11 +++++++---- testscript.bats | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 802035c..a7eb841 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -276,11 +276,14 @@ eval $INCOMMAND | while read -r line; do fi cd "$TARGETDIR" # CD into right dir - "$GIT" add $GIT_ADD_ARGS # add file(s) to index - "$GIT" commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit + STATUS=$($GIT status -s) + if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. + "$GIT" add $GIT_ADD_ARGS # add file(s) to index + "$GIT" commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit - if [ -n "$PUSH_CMD" ]; then - eval $PUSH_CMD; + if [ -n "$PUSH_CMD" ]; then + eval $PUSH_CMD; + fi fi ) & # and send into background diff --git a/testscript.bats b/testscript.bats index 865aa80..8708914 100644 --- a/testscript.bats +++ b/testscript.bats @@ -19,7 +19,6 @@ setup() { } @test "syncing correctly" { - # Start up gitwatch and see if commit and push happen automatically # after waiting two seconds ${BATS_TEST_DIRNAME}/gitwatch.sh -r origin "$testdir/local/remote" 3>- & @@ -84,9 +83,8 @@ setup() { @test "commit log messages working" { - # Start up gitwatch with logging, see if works - ${BATS_TEST_DIRNAME}/gitwatch.sh -l 10 "$testdir/local/remote" 3>- & + ${BATS_TEST_DIRNAME}/gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! # Keeps kill message from printing to screen @@ -110,11 +108,40 @@ setup() { # Check commit log that the diff is in there run git log -1 --oneline - echo "# Output is $output" >&3 [[ $output == *"file1.txt"* ]] +} + +@test "commit only when git status change" { + + # Start up gitwatch and capture its output + ${BATS_TEST_DIRNAME}/gitwatch.sh "$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 + # 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 5 + + # Touch the file, but no change + touch file1.txt + sleep 5 + + run bash -c "grep \"nothing to commit\" \"$testdir/output.txt\" | wc -l" + [[ $output == "0" ]] + } + teardown() { # Remove testing directories cd /tmp