From 79d13f3388bf308566d590e5f1b4bb5fab257da8 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Wed, 9 Dec 2020 17:14:59 -0600 Subject: [PATCH 01/95] timed out; increased running time --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index caee4fde..317de39c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -7,7 +7,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macOS-latest] - timeout-minutes: 3 + timeout-minutes: 5 steps: - name: Install tools run: | From 819413d38498c0dc7809d0f15a4031823681a3d3 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Sat, 16 Jan 2021 06:34:15 -0600 Subject: [PATCH 02/95] suggestion about .gitignore added --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 61123068..d19620cd 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ Notes: `gitwatch.sh [-r [-b ]] `
It is expected that the watched file/directory are already in a git repository (the script will not create a repository). If a folder is being watched, this will be watched fully recursively; this also means that all files and sub-folders added and removed from the directory will always be added and removed in the next commit. The `.git` folder will be excluded from the `inotifywait` call so changes to it will not cause unnecessary triggering of the script. +If you have any large files in your repository that are changing frequently, you might wish to ignore them with a `.gitignore` file. + ### Starting on Boot If you want to have the script auto-started upon boot, the method to do this depends on your operating system and distribution. If you have a GUI dialog to set up startup launches, you might want to use that, so you can more easily find and change the startup script calls later on. From 9f82d555cf3e4e18fb9975d2595cba318365c8eb Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sat, 27 Feb 2021 19:44:00 -0700 Subject: [PATCH 03/95] made shellcheck quiet --- gitwatch.sh | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 100ca57b..5fa6ed71 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -107,7 +107,7 @@ shelp () { # print all arguments to stderr stderr () { - echo $@ >&2 + echo "$@" >&2 } # clean up at end of program, killing the remaining sleep process if it still exists @@ -257,7 +257,8 @@ if [ -n "$REMOTE" ]; then # are we pushing to a remote? else # check if we are on a detached HEAD if HEADREF=$($GIT symbolic-ref HEAD 2> /dev/null); then # HEAD is not detached - PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH" + #PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH" + PUSH_CMD="$GIT push $REMOTE ${HEADREF#refs/heads/}:$BRANCH" else # HEAD is detached PUSH_CMD="$GIT push $REMOTE $BRANCH" fi @@ -272,7 +273,7 @@ diff-lines() { local path= local line= local previous_path= - while read; do + while read -r; do esc=$'\033' if [[ $REPLY =~ ---\ (a/)?([^[:blank:]$esc]+).* ]]; then previous_path=${BASH_REMATCH[2]} @@ -305,10 +306,10 @@ diff-lines() { # have been no changes reported during a whole timeout period eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # is there already a timeout process running? - if [[ -n "$SLEEP_PID" ]] && kill -0 $SLEEP_PID &>/dev/null; then + 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 + kill "$SLEEP_PID" &>/dev/null || true + wait "$SLEEP_PID" &>/dev/null || true fi # start timeout process @@ -316,11 +317,12 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do sleep "$SLEEP_TIME" # wait some more seconds to give apps time to write out all changes if [ -n "$DATE_FMT" ]; then - FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message + #FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message + FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" # splice the formatted date-time into the commit message fi if [[ "$LISTCHANGES" -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed - DIFF_COMMITMSG="$($GIT diff -U0 $LISTCHANGES_COLOR | diff-lines)" + DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" LENGTH_DIFF_COMMITMSG=0 if [[ "$LISTCHANGES" -ge 1 ]]; then LENGTH_DIFF_COMMITMSG=$(echo -n "$DIFF_COMMITMSG" | grep -c '^') @@ -342,12 +344,12 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 6; } 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 + $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 echo "Push command is $PUSH_CMD"; - eval $PUSH_CMD; + eval "$PUSH_CMD"; fi fi ) & # and send into background From b361dea0d75f59808801f36e105ee7c4e0e89571 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sat, 27 Feb 2021 19:59:57 -0700 Subject: [PATCH 04/95] fix word splitting failure --- gitwatch.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 5fa6ed71..4305d822 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -344,8 +344,11 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 6; } 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 + # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word splitted + # shellcheck disable=SC2086 + $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 if [ -n "$PUSH_CMD" ]; then echo "Push command is $PUSH_CMD"; From 61ca7fb2f05dcad1bfd032bd548c630d630bec6a Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Fri, 5 Mar 2021 13:09:21 -0600 Subject: [PATCH 05/95] modified tests to run on pull request as well --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 317de39c..58273c9e 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,5 +1,5 @@ name: Execution tests -on: [push] +on: [push, pull_request] jobs: gitwatch-tests: From 8680fc47b61af857ab5a2a8f1ea6a07f368ac6f1 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sat, 6 Mar 2021 08:10:59 -0700 Subject: [PATCH 06/95] ran gitwatch.sh through shfmt (options -i 2 -bn -ci -sr) --- gitwatch.sh | 470 +++++++++++++++++++++++++++------------------------- 1 file changed, 243 insertions(+), 227 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 4305d822..8804df25 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -45,135 +45,147 @@ LISTCHANGES_COLOR="--color=always" GIT_DIR="" # Print a message about how to use this script -shelp () { - echo "gitwatch - watch file or directory and git commit all changes as they happen" - echo "" - echo "Usage:" - echo "${0##*/} [-s ] [-d ] [-r [-b ]]" - echo " [-m ] [-l|-L ] " - 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" - echo "folder of the repo." - echo "" - echo " -s After detecting a change to the watched file or directory," - echo " wait seconds until committing, to allow for more" - echo " write actions of the same batch to finish; default is 2sec" - echo " -d The format string used for the timestamp in the commit" - echo " message; see 'man date' for details; default is " - echo " \"+%Y-%m-%d %H:%M:%S\"" - echo " -r If given and non-empty, a 'git push' to the given " - echo " is done after every commit; default is empty, i.e. no push" - echo " -b The branch which should be pushed automatically;" - echo " - if not given, the push command used is 'git push '," - 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 '" - echo " + repo is NOT in a detached HEAD state (at launch)" - echo " then the command used is" - echo " 'git push :' where" - echo " is the target of HEAD (at launch)" - echo " if no remote was defined with -r, this option has no effect" - echo " -g Location of the .git directory, if stored elsewhere in" - echo " a remote location. This specifies the --git-dir parameter" - echo " -l Log the actual changes made in this commit, up to a given" - echo " number of lines, or all lines if 0 is given" - echo " -L Same as -l but without colored formatting" - echo " -m The commit message used for each commit; all occurrences of" - echo " %d in the string will be replaced by the formatted date/time" - echo " (unless the 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\"" - echo " -e Events passed to inotifywait to watch (defaults to " - 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 "" - 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!" - echo "It is therefore recommended to terminate the script before changing the repo's" - echo "config and restarting it afterwards." - echo "" - 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." +shelp() { + echo "gitwatch - watch file or directory and git commit all changes as they happen" + echo "" + echo "Usage:" + echo "${0##*/} [-s ] [-d ] [-r [-b ]]" + echo " [-m ] [-l|-L ] " + 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" + echo "folder of the repo." + echo "" + echo " -s After detecting a change to the watched file or directory," + echo " wait seconds until committing, to allow for more" + echo " write actions of the same batch to finish; default is 2sec" + echo " -d The format string used for the timestamp in the commit" + echo " message; see 'man date' for details; default is " + echo " \"+%Y-%m-%d %H:%M:%S\"" + echo " -r If given and non-empty, a 'git push' to the given " + echo " is done after every commit; default is empty, i.e. no push" + echo " -b The branch which should be pushed automatically;" + echo " - if not given, the push command used is 'git push '," + 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 '" + echo " + repo is NOT in a detached HEAD state (at launch)" + echo " then the command used is" + echo " 'git push :' where" + echo " is the target of HEAD (at launch)" + echo " if no remote was defined with -r, this option has no effect" + echo " -g Location of the .git directory, if stored elsewhere in" + echo " a remote location. This specifies the --git-dir parameter" + echo " -l Log the actual changes made in this commit, up to a given" + echo " number of lines, or all lines if 0 is given" + echo " -L Same as -l but without colored formatting" + echo " -m The commit message used for each commit; all occurrences of" + echo " %d in the string will be replaced by the formatted date/time" + echo " (unless the 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\"" + echo " -e Events passed to inotifywait to watch (defaults to " + 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 "" + 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!" + echo "It is therefore recommended to terminate the script before changing the repo's" + echo "config and restarting it afterwards." + echo "" + 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." } # print all arguments to stderr -stderr () { - echo "$@" >&2 +stderr() { + echo "$@" >&2 } # clean up at end of program, killing the remaining sleep process if it still exists -cleanup () { - if [[ -n "$SLEEP_PID" ]] && kill -0 "$SLEEP_PID" &>/dev/null; then - kill "$SLEEP_PID" &>/dev/null - fi - exit 0 +cleanup() { + if [[ -n "$SLEEP_PID" ]] && kill -0 "$SLEEP_PID" &> /dev/null; then + kill "$SLEEP_PID" &> /dev/null + fi + exit 0 } # Tests for the availability of a command -is_command () { - hash "$1" 2>/dev/null +is_command() { + hash "$1" 2> /dev/null } ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e: option # Process command line options -do - case "${option}" in - b) BRANCH=${OPTARG};; - d) DATE_FMT=${OPTARG};; - h) shelp; exit;; - g) GIT_DIR=${OPTARG};; - l) LISTCHANGES=${OPTARG};; - L) LISTCHANGES=${OPTARG}; LISTCHANGES_COLOR="";; - m) COMMITMSG=${OPTARG};; - p|r) REMOTE=${OPTARG};; - s) SLEEP_TIME=${OPTARG};; - e) EVENTS=${OPTARG};; - *) stderr "Error: Option '${option}' does not exist."; shelp; exit 1;; - esac +while getopts b:d:h:g:L:l:m:p:r:s:e: option; do # Process command line options + case "${option}" in + b) BRANCH=${OPTARG} ;; + d) DATE_FMT=${OPTARG} ;; + h) + shelp + exit + ;; + g) GIT_DIR=${OPTARG} ;; + l) LISTCHANGES=${OPTARG} ;; + L) + LISTCHANGES=${OPTARG} + LISTCHANGES_COLOR="" + ;; + m) COMMITMSG=${OPTARG} ;; + p | r) REMOTE=${OPTARG} ;; + s) SLEEP_TIME=${OPTARG} ;; + e) EVENTS=${OPTARG} ;; + *) + stderr "Error: Option '${option}' does not exist." + shelp + exit 1 + ;; + esac done -shift $((OPTIND-1)) # Shift the input arguments, so that the input file (last arg) is $1 in the code below +shift $((OPTIND - 1)) # Shift the input arguments, so that the input file (last arg) is $1 in the code below 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 + shelp # print usage help + exit # and exit fi # if custom bin names are given for git, inotifywait, or readlink, use those; otherwise fall back to "git", "inotifywait", and "readlink" if [ -z "$GW_GIT_BIN" ]; then GIT="git"; else GIT="$GW_GIT_BIN"; fi if [ -z "$GW_INW_BIN" ]; then - # if Mac, use fswatch - if [ "$(uname)" != "Darwin" ]; then - INW="inotifywait"; - EVENTS="${EVENTS:-close_write,move,move_self,delete,create,modify}" - 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 - EVENTS="${EVENTS:---event=414}" - fi; + # if Mac, use fswatch + if [ "$(uname)" != "Darwin" ]; then + INW="inotifywait" + EVENTS="${EVENTS:-close_write,move,move_self,delete,create,modify}" + 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 + EVENTS="${EVENTS:---event=414}" + fi else - INW="$GW_INW_BIN"; + INW="$GW_INW_BIN" fi if [ -z "$GW_RL_BIN" ]; then RL="readlink"; else RL="$GW_RL_BIN"; fi # Check availability of selected binaries and die if not met for cmd in "$GIT" "$INW"; do - is_command "$cmd" || { stderr "Error: Required command '$cmd' not found." ; exit 2; } + is_command "$cmd" || { + stderr "Error: Required command '$cmd' not found." + exit 2 + } done unset cmd @@ -183,118 +195,119 @@ SLEEP_PID="" # pid of timeout subprocess trap "cleanup" EXIT # make sure the timeout is killed when exiting script - # Expand the path to the target to absolute path if [ "$(uname)" != "Darwin" ]; then - IN=$($RL -f "$1") + IN=$($RL -f "$1") else - if is_command "greadlink"; then - IN=$(greadlink -f "$1") - else - IN=$($RL -f "$1") - if [ $? -eq 1 ]; then - echo "Seems like your readlink doesn't support '-f'. Running without. Please 'brew install coreutils'." - IN=$($RL "$1") - fi - fi; -fi; - + if is_command "greadlink"; then + IN=$(greadlink -f "$1") + else + IN=$($RL -f "$1") + if [ $? -eq 1 ]; then + echo "Seems like your readlink doesn't support '-f'. Running without. Please 'brew install coreutils'." + IN=$($RL "$1") + fi + fi +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 - # construct inotifywait-commandline - if [ "$(uname)" != "Darwin" ]; then - INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") - else - # still need to fix EVENTS since it wants them listed one-by-one - INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") - fi; - GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index - GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure + TARGETDIR=$(sed -e "s/\/*$//" <<< "$IN") # dir to CD into before using git commands: trim trailing slash, if any + # construct inotifywait-commandline + if [ "$(uname)" != "Darwin" ]; then + INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") + else + # still need to fix EVENTS since it wants them listed one-by-one + INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") + fi + GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index + GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure elif [ -f "$1" ]; then # if the target is a single file - TARGETDIR=$(dirname "$IN") # dir to CD into before using git commands: extract from file name - # construct inotifywait-commandline - if [ "$(uname)" != "Darwin" ]; then - INW_ARGS=("-qm" "-e" "$EVENTS" "$IN") - else - INW_ARGS=("$EVENTS" "$IN") - fi + TARGETDIR=$(dirname "$IN") # dir to CD into before using git commands: extract from file name + # construct inotifywait-commandline + if [ "$(uname)" != "Darwin" ]; then + INW_ARGS=("-qm" "-e" "$EVENTS" "$IN") + else + INW_ARGS=("$EVENTS" "$IN") + fi - GIT_ADD_ARGS="$IN" # add only the selected file to index - GIT_COMMIT_ARGS="" # no need to add anything more to "commit" call + GIT_ADD_ARGS="$IN" # add only the selected file to index + GIT_COMMIT_ARGS="" # no need to add anything more to "commit" call else - stderr "Error: The target is neither a regular file nor a directory." - exit 3 + stderr "Error: The target is neither a regular file nor a directory." + exit 3 fi # 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"; - exit 4; - fi + if [ ! -d "$GIT_DIR" ]; then + stderr ".git location is not a directory: $GIT_DIR" + exit 4 + fi - GIT="$GIT --no-pager --work-tree $TARGETDIR --git-dir $GIT_DIR" + GIT="$GIT --no-pager --work-tree $TARGETDIR --git-dir $GIT_DIR" fi # Check if commit message needs any formatting (date splicing) if ! grep "%d" > /dev/null <<< "$COMMITMSG"; then # if commitmsg didn't contain %d, grep returns non-zero - DATE_FMT="" # empty date format (will disable splicing in the main loop) - FORMATTED_COMMITMSG="$COMMITMSG" # save (unchanging) commit message + DATE_FMT="" # empty date format (will disable splicing in the main loop) + FORMATTED_COMMITMSG="$COMMITMSG" # save (unchanging) commit message fi # CD into right dir -cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 5; } - -if [ -n "$REMOTE" ]; then # are we pushing to a remote? - if [ -z "$BRANCH" ]; then # Do we have a branch set to push to ? - PUSH_CMD="$GIT push $REMOTE" # Branch not set, push to remote without a branch - else - # check if we are on a detached HEAD - if HEADREF=$($GIT symbolic-ref HEAD 2> /dev/null); then # HEAD is not detached - #PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH" - PUSH_CMD="$GIT push $REMOTE ${HEADREF#refs/heads/}:$BRANCH" - else # HEAD is detached - PUSH_CMD="$GIT push $REMOTE $BRANCH" - fi +cd "$TARGETDIR" || { + stderr "Error: Can't change directory to '${TARGETDIR}'." + exit 5 +} + +if [ -n "$REMOTE" ]; then # are we pushing to a remote? + if [ -z "$BRANCH" ]; then # Do we have a branch set to push to ? + PUSH_CMD="$GIT push $REMOTE" # Branch not set, push to remote without a branch + else + # check if we are on a detached HEAD + if HEADREF=$($GIT symbolic-ref HEAD 2> /dev/null); then # HEAD is not detached + #PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH" + PUSH_CMD="$GIT push $REMOTE ${HEADREF#refs/heads/}:$BRANCH" + else # HEAD is detached + PUSH_CMD="$GIT push $REMOTE $BRANCH" fi + fi else - PUSH_CMD="" # if not remote is selected, make sure push command is empty + PUSH_CMD="" # if not remote is selected, make sure push command is empty fi # A function to reduce git diff output to the actual changed content, and insert file line numbers. # Based on "https://stackoverflow.com/a/12179492/199142" by John Mellor diff-lines() { - local path= - local line= - local previous_path= - while read -r; do - esc=$'\033' - if [[ $REPLY =~ ---\ (a/)?([^[:blank:]$esc]+).* ]]; then - previous_path=${BASH_REMATCH[2]} - 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 - REPLY=${REPLY:0:150} # limit the line width, so it fits in a single line in most git log outputs - if [[ "$path" == "/dev/null" ]]; then - echo "File $previous_path deleted or moved." - continue - else - echo "$path:$line: $REPLY" - fi - if [[ ${BASH_REMATCH[2]} != - ]]; then - ((line++)) - fi - fi - done + local path= + local line= + local previous_path= + while read -r; do + esc=$'\033' + if [[ $REPLY =~ ---\ (a/)?([^[:blank:]$esc]+).* ]]; then + previous_path=${BASH_REMATCH[2]} + 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 + REPLY=${REPLY:0:150} # limit the line width, so it fits in a single line in most git log outputs + if [[ "$path" == "/dev/null" ]]; then + echo "File $previous_path deleted or moved." + continue + else + echo "$path:$line: $REPLY" + fi + if [[ ${BASH_REMATCH[2]} != - ]]; then + ((line++)) + fi + fi + done } ############################################################################### @@ -305,57 +318,60 @@ diff-lines() { # 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 eval "$INW" "${INW_ARGS[@]}" | while read -r line; do - # 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 + # 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 + fi + + # start timeout process + ( + sleep "$SLEEP_TIME" # wait some more seconds to give apps time to write out all changes + + if [ -n "$DATE_FMT" ]; then + #FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message + FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" # splice the formatted date-time into the commit message fi - # start timeout process - ( - sleep "$SLEEP_TIME" # wait some more seconds to give apps time to write out all changes - - if [ -n "$DATE_FMT" ]; then - #FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message - FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" # splice the formatted date-time into the commit message - fi - - if [[ "$LISTCHANGES" -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed - DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" - 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 - # 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 - FORMATTED_COMMITMSG="New files added: $($GIT status -s)" - fi - else - #FORMATTED_COMMITMSG="Many lines were modified. $FORMATTED_COMMITMSG" - FORMATTED_COMMITMSG=$($GIT diff --stat | grep '|') - fi + if [[ "$LISTCHANGES" -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed + DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" + 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 + # 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 + FORMATTED_COMMITMSG="New files added: $($GIT status -s)" fi + else + #FORMATTED_COMMITMSG="Many lines were modified. $FORMATTED_COMMITMSG" + FORMATTED_COMMITMSG=$($GIT diff --stat | grep '|') + fi + fi - # CD into right dir - cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 6; } - STATUS=$($GIT status -s) - 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 - $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 - - if [ -n "$PUSH_CMD" ]; then - echo "Push command is $PUSH_CMD"; - eval "$PUSH_CMD"; - fi - fi - ) & # and send into background + # CD into right dir + cd "$TARGETDIR" || { + stderr "Error: Can't change directory to '${TARGETDIR}'." + exit 6 + } + STATUS=$($GIT status -s) + 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 + $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 + + if [ -n "$PUSH_CMD" ]; then + echo "Push command is $PUSH_CMD" + eval "$PUSH_CMD" + fi + fi + ) & # and send into background - SLEEP_PID=$! # and remember its PID + SLEEP_PID=$! # and remember its PID done From 5b8254615239c806ee7a3b0486215f195db79731 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sat, 6 Mar 2021 08:33:27 -0700 Subject: [PATCH 07/95] ran gitwatch.sh through shfmt -s --- gitwatch.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 8804df25..82c88202 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -61,7 +61,7 @@ shelp() { echo " write actions of the same batch to finish; default is 2sec" echo " -d The format string used for the timestamp in the commit" echo " message; see 'man date' for details; default is " - echo " \"+%Y-%m-%d %H:%M:%S\"" + echo ' "+%Y-%m-%d %H:%M:%S"' echo " -r If given and non-empty, a 'git push' to the given " echo " is done after every commit; default is empty, i.e. no push" echo " -b The branch which should be pushed automatically;" @@ -84,7 +84,7 @@ shelp() { echo " %d in the string will be replaced by the formatted date/time" echo " (unless the 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\"" + echo ' "Scripted auto-commit on change (%d) by gitwatch.sh"' echo " -e Events passed to inotifywait to watch (defaults to " echo " '$EVENTS')" echo " (useful when using inotify-win, e.g. -e modify,delete,move)" @@ -97,7 +97,7 @@ shelp() { echo "It is therefore recommended to terminate the script before changing the repo's" echo "config and restarting it afterwards." echo "" - echo "By default, gitwatch tries to use the binaries \"git\", \"inotifywait\", and" + 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" @@ -112,7 +112,7 @@ stderr() { # clean up at end of program, killing the remaining sleep process if it still exists cleanup() { - if [[ -n "$SLEEP_PID" ]] && kill -0 "$SLEEP_PID" &> /dev/null; then + if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then kill "$SLEEP_PID" &> /dev/null fi exit 0 @@ -297,7 +297,7 @@ diff-lines() { line=${BASH_REMATCH[2]} elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then REPLY=${REPLY:0:150} # limit the line width, so it fits in a single line in most git log outputs - if [[ "$path" == "/dev/null" ]]; then + if [[ $path == "/dev/null" ]]; then echo "File $previous_path deleted or moved." continue else @@ -319,7 +319,7 @@ diff-lines() { # have been no changes reported during a whole timeout period eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # is there already a timeout process running? - if [[ -n "$SLEEP_PID" ]] && kill -0 "$SLEEP_PID" &> /dev/null; then + 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 @@ -334,13 +334,13 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" # splice the formatted date-time into the commit message fi - if [[ "$LISTCHANGES" -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed + if [[ $LISTCHANGES -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" LENGTH_DIFF_COMMITMSG=0 - if [[ "$LISTCHANGES" -ge 1 ]]; then + if [[ $LISTCHANGES -ge 1 ]]; then LENGTH_DIFF_COMMITMSG=$(echo -n "$DIFF_COMMITMSG" | grep -c '^') fi - if [[ "$LENGTH_DIFF_COMMITMSG" -le $LISTCHANGES ]]; then + if [[ $LENGTH_DIFF_COMMITMSG -le $LISTCHANGES ]]; then # 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" From ad9b4b9c9f9861edeb247d2060c60457a92d3a4d Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 10:52:50 -0700 Subject: [PATCH 08/95] straight conversion to bats-core, added super-linter checking --- .editorconfig | 11 +++++ .github/workflows/gitwatch.yml | 78 ++++++++++++++++++++++++++++++++++ tests/startup-shutdown.bash | 44 ++++++++++--------- tests/sync.bats | 6 +-- 4 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/gitwatch.yml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..0e311595 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +[{*.sh,*.bats,*.bash}] +# like -i=2 +indent_style = space +indent_size = 2 + +shell_variant = auto # like -ln=auto +binary_next_line = true # like -bn +switch_case_indent = true # like -ci +space_redirects = true # like -sr +#keep_padding = true # like -kp +#function_next_line = true # like -fn diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml new file mode 100644 index 00000000..e6d60bc3 --- /dev/null +++ b/.github/workflows/gitwatch.yml @@ -0,0 +1,78 @@ +name: Gitwatch QA + +on: + push: + pull_request: + branches: [master] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + #----------------------------------------------------------------------------- + super-lint: + name: Lint Code Base + + # Don't run action if commit message has #noaction in it. + if: "! contains(github.event.head_commit.message, '#noaction')" + + # I don't think running lint on multiple os' will do any good. + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Run Super-Linter + uses: github/super-linter@v3 + env: + DEFAULT_BRANCH: master + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + #----------------------------------------------------------------------------- + bats: + name: BATS Tests + + # Don't run action if commit message has #noaction in it. + if: "! contains(github.event.head_commit.message, '#noaction')" + + strategy: + matrix: + os: [ubuntu-latest, macOS-latest] + + # The type of runner that the job will run on + runs-on: ${{ matrix.os }} + + timeout-minutes: 5 + + steps: + - name: Setup BATS + # Even though this says 1.2.0,it's actually using 1.2.1 + uses: mig4/setup-bats@v1.2.0 + + - name: Install gitwatch dependencies + shell: bash + run: | + if [ "$RUNNER_OS" == "Linux" ]; then + sudo apt-get -y install inotify-tools + + elif [ "$RUNNER_OS" == "macOS" ]; then + brew install fswatch + brew install coreutils # in order to get readlink + + else + echo "Unsupported OS: $RUNNER_OS" + exit 1 + fi + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Run tests + shell: bash + run: | + git config --global user.email "test@email.com" + git config --global user.name "test user" + bashcov bats -rt tests diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash index c22e3b69..6c5e1869 100644 --- a/tests/startup-shutdown.bash +++ b/tests/startup-shutdown.bash @@ -1,26 +1,32 @@ setup() { - # Time to wait for gitwatch to respond - WAITTIME=4 - # Set up directory structure and initialize remote - testdir=$(mktemp -d) - cd $testdir - mkdir remote - cd remote - git init -q --bare - cd .. - mkdir local - cd local - git clone -q ../remote + # Time to wait for gitwatch to respond + # shellcheck disable=SC2034 + WAITTIME=4 + # Set up directory structure and initialize remote + testdir=$(mktemp -d) + # shellcheck disable=SC2164 + cd "$testdir" + mkdir remote + # shellcheck disable=SC2164 + cd remote + git init -q --bare + # shellcheck disable=SC2103 + cd .. + # shellcheck disable=SC2164 + mkdir local + # shellcheck disable=SC2164 + cd local + git clone -q ../remote } - teardown() { - # Remove testing directories - cd /tmp + # Remove testing directories + # shellcheck disable=SC2164 + cd /tmp -# rm -rf $testdir + # rm -rf $testdir - # Make sure gitwatch script gets killed if script stopped background - # Must kill the entire tree of processes generated - pkill -15 -P $GITWATCH_PID + # Make sure gitwatch script gets killed if script stopped background + # Must kill the entire tree of processes generated + pkill -15 -P "$GITWATCH_PID" } diff --git a/tests/sync.bats b/tests/sync.bats index 76cb241c..cdbe0d13 100644 --- a/tests/sync.bats +++ b/tests/sync.bats @@ -33,7 +33,7 @@ load startup-shutdown currentcommit=$(git rev-parse master) remotecommit=$(git rev-parse origin/master) [ "$currentcommit" = "$remotecommit" ] - + # Try making subdirectory with file lastcommit=$(git rev-parse master) mkdir subdir @@ -45,7 +45,7 @@ load startup-shutdown # Verify that new commit has happened currentcommit=$(git rev-parse master) [ "$lastcommit" != "$currentcommit" ] - + # Verify that push happened currentcommit=$(git rev-parse master) remotecommit=$(git rev-parse origin/master) @@ -59,7 +59,7 @@ load startup-shutdown # Verify that new commit has happened currentcommit=$(git rev-parse master) [ "$lastcommit" != "$currentcommit" ] - + # Verify that push happened currentcommit=$(git rev-parse master) remotecommit=$(git rev-parse origin/master) From c488501741d6318739f00fd4d211659d51127e85 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 10:56:32 -0700 Subject: [PATCH 09/95] disable existing workflow, remove bashcov attempt --- .github/workflows/gitwatch.yml | 2 +- .github/workflows/{run-tests.yml => run-tests.yml.disabled} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{run-tests.yml => run-tests.yml.disabled} (100%) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index e6d60bc3..069dd5bf 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -75,4 +75,4 @@ jobs: run: | git config --global user.email "test@email.com" git config --global user.name "test user" - bashcov bats -rt tests + bats -rt tests diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml.disabled similarity index 100% rename from .github/workflows/run-tests.yml rename to .github/workflows/run-tests.yml.disabled From c658f3cad45c13d104300b26f352e66976948f56 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 11:11:39 -0700 Subject: [PATCH 10/95] quiet markdown linter --- README.md | 121 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 93 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index d19620cd..3a311b28 100644 --- a/README.md +++ b/README.md @@ -3,45 +3,71 @@ A bash script to watch a file or folder and commit changes to a git repo ## What to use it for? + That's really up to you, but here are some examples: -* **config files**: some programs auto-write their config files, without waiting for you to click an 'Apply' button; or even if there is such a button, most programs offer you no way of going back to an earlier version of your settings. If you commit your config file(s) to a git repo, you can track changes and go back to older versions. This script makes it convenient, to have all changes recorded automatically. -* **document files**: if you use an editor that does not have built-in git support (or maybe if you don't like the git support it has), you can use gitwatch to automatically commit your files when you save them, or combine it with the editor's auto-save feature to fully automatically and regularly track your changes -* *more stuff!* If you have any other uses, or can think of ones, please let us know, and we can add them to this list! + +* **config files**: some programs auto-write their config files, without + waiting for you to click an 'Apply' button; or even if there is such + a button, most programs offer you no way of going back to an earlier + version of your settings. If you commit your config file(s) to a git repo, + you can track changes and go back to older versions. This script makes it + convenient, to have all changes recorded automatically. +* **document files**: if you use an editor that does not have built-in git + support (or maybe if you don't like the git support it has), you can use + gitwatch to automatically commit your files when you save them, or combine + it with the editor's auto-save feature to fully automatically and regularly + track your changes +* *more stuff!* If you have any other uses, or can think of ones, please let + us know, and we can add them to this list! ## Installation + `gitwatch` can be installed in various ways. ### From Source -`gitwatch` can be installed from source by simply cloning the repository -and putting the shell script into your `$PATH`. The commands below will -do that for you if `/usr/local/bin` is in your `$PATH`. You may need to -invoke `install` with `sudo`. + +`gitwatch` can be installed from source by simply cloning the repository and +putting the shell script into your `$PATH`. The commands below will do that +for you if `/usr/local/bin` is in your `$PATH`. You may need to invoke +`install` with `sudo`. ```sh $ git clone https://github.com/gitwatch/gitwatch.git $ cd gitwatch $ [sudo] install -b gitwatch.sh /usr/local/bin/gitwatch ``` + #### Update -If you installed `gitwatch` from source, you can update it by following the exact same steps (or `git pull` rather than clone if you kept the repository around). +If you installed `gitwatch` from source, you can update it by following the +exact same steps (or `git pull` rather than clone if you kept the repository +around). ### bpkg + `gitwatch` can be installed with [bpkg](https://github.com/bpkg/bpkg). Make -sure you have [bpkg](https://github.com/bpkg/bpkg) installed before -running the command below. You may need to invoke `bpkg` with `sudo` -when using the `-g` flag. +sure you have [bpkg](https://github.com/bpkg/bpkg) installed before running +the command below. You may need to invoke `bpkg` with `sudo` when using the +`-g` flag. ```sh $ [sudo] bpkg install -g gitwatch/gitwatch ``` +### Archlinux + +There is an [AUR](https://aur.archlinux.org/packages/gitwatch-git/) package +for Archlinux. Install it with you favorite aur helper. + ## Requirements + To run this script, you must have installed and globally available: + * `git` ( [git/git](https://github.com/git/git) | http://www.git-scm.com ) * `inotifywait` (part of **inotify-tools**: [rvoicilas/inotify-tools](https://github.com/rvoicilas/inotify-tools) ) ### Notes for Mac + If running on OS X, you'll need to install the following Homebrew tools: ```sh @@ -49,46 +75,85 @@ $ brew install fswatch $ brew install coreutils ``` - ## What it does -When you start the script, it prepares some variables and checks if the file [a] or directory [b] given as input really exists.
-Then it goes into the main loop (which will run forever, until the script is forcefully stopped/killed), which will: -* watch for changes to the file/directory using `inotifywait` (`inotifywait` will block until something happens) + +When you start the script, it prepares some variables and checks if the file +[a] or directory [b] given as input really exists. + +Then it goes into the main loop (which will run forever, until the script is +forcefully stopped/killed), which will: + +* watch for changes to the file/directory using `inotifywait` (`inotifywait` + will block until something happens) * wait 2 seconds -* `cd` into the directory [b] / the directory containing the file [a] \(because `git` likes to operate locally) +* `cd` into the directory [b] / the directory containing the file [a] + \(because `git` likes to operate locally) * `git add `[a] / `git add .`[b] -* `git commit -m "Scripted auto-commit on change ()"`[a] / `git commit -a -m"Scripted auto-commit on change ()"`[b] -* if a remote is defined (with `-r`) do a push after the commit (a specific branch can be selected with `-b`) +* `git commit -m "Scripted auto-commit on change ()"`[a] / `git commit + -a -m"Scripted auto-commit on change ()"`[b] +* if a remote is defined (with `-r`) do a push after the commit (a specific + branch can be selected with `-b`) Notes: -* the waiting period of 2 sec is added to allow for several changes to be written out completely before committing; depending on how fast the script is executed, this might otherwise cause race conditions when watching a folder +* the waiting period of 2 sec is added to allow for several changes to be + written out completely before committing; depending on how fast the script + is executed, this might otherwise cause race conditions when watching + a folder * currently, folders are always watched recursively ## Usage + `gitwatch.sh [-r [-b ]] `
-It is expected that the watched file/directory are already in a git repository (the script will not create a repository). If a folder is being watched, this will be watched fully recursively; this also means that all files and sub-folders added and removed from the directory will always be added and removed in the next commit. The `.git` folder will be excluded from the `inotifywait` call so changes to it will not cause unnecessary triggering of the script. +It is expected that the watched file/directory are already in a git repository +(the script will not create a repository). If a folder is being watched, this +will be watched fully recursively; this also means that all files and +sub-folders added and removed from the directory will always be added and +removed in the next commit. The `.git` folder will be excluded from the +`inotifywait` call so changes to it will not cause unnecessary triggering of +the script. -If you have any large files in your repository that are changing frequently, you might wish to ignore them with a `.gitignore` file. +If you have any large files in your repository that are changing frequently, +you might wish to ignore them with a `.gitignore` file. ### Starting on Boot -If you want to have the script auto-started upon boot, the method to do this depends on your operating system and distribution. If you have a GUI dialog to set up startup launches, you might want to use that, so you can more easily find and change the startup script calls later on. +If you want to have the script auto-started upon boot, the method to do this +depends on your operating system and distribution. If you have a GUI dialog to +set up startup launches, you might want to use that, so you can more easily +find and change the startup script calls later on. -Please also note that if either of the paths involved (script or target) contains spaces or special characters, you need to escape them accordingly; if you don't know how to do that, the internet will help you, or feel free to ask here or contact me directly. +Please also note that if either of the paths involved (script or target) +contains spaces or special characters, you need to escape them accordingly; if +you don't know how to do that, the internet will help you, or feel free to ask +here or contact me directly. #### SysVInit -A central place to put startup scripts on Linux is generally `/etc/rc.local` (to my knowledge; only tested and confirmed on Ubuntu). This file, if it has the +x bit, will be executed upon startup, **by the root user account**. If you want to start `gitwatch` from `rc.local`, the recommended way to call it is: +A central place to put startup scripts on Linux is generally `/etc/rc.local` +(to my knowledge; only tested and confirmed on Ubuntu). This file, if it has +the +x bit, will be executed upon startup, **by the root user account**. If +you want to start `gitwatch` from `rc.local`, the recommended way to call it +is: `su -c "/absolute/path/to/script/gitwatch.sh /absolute/path/to/watched/file/or/folder" -l &` -The `` bit should be replaced with your username or that of any other (non-root) user account; it only needs write-access to the git repository of the file/folder you want to watch. The ampersand (`&`) at the end sends the launched process into the background (this is important if you have other calls in `rc.local` after the mentioned line, because the `gitwatch` call does not usually return). +The `` bit should be replaced with your username or that of any +other (non-root) user account; it only needs write-access to the git +repository of the file/folder you want to watch. The ampersand (`&`) at the +end sends the launched process into the background (this is important if you +have other calls in `rc.local` after the mentioned line, because the +`gitwatch` call does not usually return). #### systemd -- If installed to a path other than `/usr/bin/gitwatch`, modify `gitwatch@.service` to suit -- Create dir if it does not exist and copy systemd service file with `mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service $HOME/.config/systemd/user` -- Start and enable the service for a given path by running `systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service` +- If installed to a path other than `/usr/bin/gitwatch`, modify + `gitwatch@.service` to suit +- Create dir if it does not exist and copy systemd service file with `mkdir -p + "$HOME/.config/systemd/user" && cp gitwatch@.service + $HOME/.config/systemd/user` +- Start and enable the service for a given path by running `systemctl --user + --now enable gitwatch@$(systemd-escape "'-r url/to/repository' + /path/to/folder").service` ## Other Articles ### On the Gitwatch Wiki From dc499835bbfb6b50ad7c972be0da61dc96ce18b3 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 11:53:51 -0700 Subject: [PATCH 11/95] added markdownlinter fixes and config file --- .markdown-lint.yml | 15 +++++++++++++++ README.md | 30 ++++++++++++++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 .markdown-lint.yml diff --git a/.markdown-lint.yml b/.markdown-lint.yml new file mode 100644 index 00000000..614af8a3 --- /dev/null +++ b/.markdown-lint.yml @@ -0,0 +1,15 @@ +--- +# Linter rules doc: +# - https://github.com/DavidAnson/markdownlint +# +# This file is based off of the github action super-linter template: +# - https://github.com/github/super-linter/blob/master/TEMPLATES/.markdown-lint.yml +# +# Note: +# To comment out a single error: +# +# any violations you want +# + +# We don't care about inline HTML +MD033: false diff --git a/README.md b/README.md index 3a311b28..aea23d13 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,9 @@ for you if `/usr/local/bin` is in your `$PATH`. You may need to invoke `install` with `sudo`. ```sh -$ git clone https://github.com/gitwatch/gitwatch.git -$ cd gitwatch -$ [sudo] install -b gitwatch.sh /usr/local/bin/gitwatch +git clone https://github.com/gitwatch/gitwatch.git +cd gitwatch +[sudo] install -b gitwatch.sh /usr/local/bin/gitwatch ``` #### Update @@ -51,7 +51,7 @@ the command below. You may need to invoke `bpkg` with `sudo` when using the `-g` flag. ```sh -$ [sudo] bpkg install -g gitwatch/gitwatch +[sudo] bpkg install -g gitwatch/gitwatch ``` ### Archlinux @@ -63,16 +63,16 @@ for Archlinux. Install it with you favorite aur helper. To run this script, you must have installed and globally available: -* `git` ( [git/git](https://github.com/git/git) | http://www.git-scm.com ) -* `inotifywait` (part of **inotify-tools**: [rvoicilas/inotify-tools](https://github.com/rvoicilas/inotify-tools) ) +* `git` ([git/git](https://github.com/git/git) | [git-scm](http://www.git-scm.com)) +* `inotifywait` (part of **[inotify-tools](https://github.com/rvoicilas/inotify-tools)**) ### Notes for Mac If running on OS X, you'll need to install the following Homebrew tools: ```sh -$ brew install fswatch -$ brew install coreutils +brew install fswatch +brew install coreutils ``` ## What it does @@ -95,6 +95,7 @@ forcefully stopped/killed), which will: branch can be selected with `-b`) Notes: + * the waiting period of 2 sec is added to allow for several changes to be written out completely before committing; depending on how fast the script is executed, this might otherwise cause race conditions when watching @@ -104,6 +105,7 @@ Notes: ## Usage `gitwatch.sh [-r [-b ]] `
+ It is expected that the watched file/directory are already in a git repository (the script will not create a repository). If a folder is being watched, this will be watched fully recursively; this also means that all files and @@ -113,7 +115,7 @@ removed in the next commit. The `.git` folder will be excluded from the the script. If you have any large files in your repository that are changing frequently, -you might wish to ignore them with a `.gitignore` file. +you might wish to ignore them with a `.gitignore` file. ### Starting on Boot @@ -135,7 +137,9 @@ the +x bit, will be executed upon startup, **by the root user account**. If you want to start `gitwatch` from `rc.local`, the recommended way to call it is: + `su -c "/absolute/path/to/script/gitwatch.sh /absolute/path/to/watched/file/or/folder" -l &` + The `` bit should be replaced with your username or that of any other (non-root) user account; it only needs write-access to the git @@ -146,15 +150,17 @@ have other calls in `rc.local` after the mentioned line, because the #### systemd -- If installed to a path other than `/usr/bin/gitwatch`, modify +* If installed to a path other than `/usr/bin/gitwatch`, modify `gitwatch@.service` to suit -- Create dir if it does not exist and copy systemd service file with `mkdir -p +* Create dir if it does not exist and copy systemd service file with `mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service $HOME/.config/systemd/user` -- Start and enable the service for a given path by running `systemctl --user +* Start and enable the service for a given path by running `systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service` ## Other Articles + ### On the Gitwatch Wiki + * [How to install `gitwatch` as a Debian service with `supervisord`](https://github.com/gitwatch/gitwatch/wiki/gitwatch-as-a-service-on-Debian-with-supervisord) From b38f4526e2721a4c432c4bacde8ba11c2b2fe483 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 12:16:05 -0700 Subject: [PATCH 12/95] remove html line break --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aea23d13..ee32b8f0 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Notes: ## Usage -`gitwatch.sh [-r [-b ]] `
+`gitwatch.sh [-r [-b ]] ` It is expected that the watched file/directory are already in a git repository (the script will not create a repository). If a folder is being watched, this From 190f03a9884636cdccbb8f20c0337db6a9e75d6a Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 15:41:12 -0700 Subject: [PATCH 13/95] added generated toc --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index ee32b8f0..ca12ac59 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ + + + * [gitwatch](#gitwatch) + * [What to use it for?](#what-to-use-it-for) + * [Installation](#installation) + * [From Source](#from-source) + * [Update](#update) + * [bpkg](#bpkg) + * [Archlinux](#archlinux) + * [Requirements](#requirements) + * [Notes for Mac](#notes-for-mac) + * [What it does](#what-it-does) + * [Usage](#usage) + * [Starting on Boot](#starting-on-boot) + * [SysVInit](#sysvinit) + * [systemd](#systemd) + * [Other Articles](#other-articles) + * [On the Gitwatch Wiki](#on-the-gitwatch-wiki) + + + + # gitwatch A bash script to watch a file or folder and commit changes to a git repo From 6fee35b1b9bd5a329e3dd746761c1f7a7866e255 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Sun, 7 Mar 2021 15:42:56 -0700 Subject: [PATCH 14/95] #noaction - added comments to toc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ca12ac59..9a97df78 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ + * [gitwatch](#gitwatch) * [What to use it for?](#what-to-use-it-for) From 7ff8d90bf52bc646e958dfe750edb21e7d709e9d Mon Sep 17 00:00:00 2001 From: Alan Young Date: Tue, 9 Mar 2021 06:43:07 -0700 Subject: [PATCH 15/95] fixed hadolint issues --- Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 36e4ce3c..25be743f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,11 @@ FROM alpine:3.1 -RUN apk add --update bash git inotify-tools openssh && rm -rf /var/cache/apk/* +# hadolint ignore=DL3018 +RUN apk add --no-cache bash git inotify-tools openssh RUN mkdir -p /app WORKDIR /app -ADD gitwatch.sh ./ +COPY gitwatch.sh ./ -RUN chmod 755 *.sh +RUN chmod 755 -- *.sh ENTRYPOINT ["./gitwatch.sh"] From a834ee5f2fd78379b6f2bd95c9518cea37499051 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Tue, 9 Mar 2021 06:52:41 -0700 Subject: [PATCH 16/95] ignore generated toc for markdownlint --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9a97df78..7cb52021 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ + * [gitwatch](#gitwatch) * [What to use it for?](#what-to-use-it-for) @@ -21,6 +22,7 @@ + # gitwatch A bash script to watch a file or folder and commit changes to a git repo From 92415c4e9d83efcd87ec7b7237fd9bbdea350407 Mon Sep 17 00:00:00 2001 From: Alan Young Date: Tue, 9 Mar 2021 08:43:46 -0700 Subject: [PATCH 17/95] I think this will pass githubs super-linter now --- .github/linters/.markdown-lint.yml | 1 + .markdown-lint.yml => .markdownlint.yml | 8 ++++++++ README.md | 2 ++ 3 files changed, 11 insertions(+) create mode 120000 .github/linters/.markdown-lint.yml rename .markdown-lint.yml => .markdownlint.yml (82%) diff --git a/.github/linters/.markdown-lint.yml b/.github/linters/.markdown-lint.yml new file mode 120000 index 00000000..7e62b3d9 --- /dev/null +++ b/.github/linters/.markdown-lint.yml @@ -0,0 +1 @@ +../../.markdownlint.yml \ No newline at end of file diff --git a/.markdown-lint.yml b/.markdownlint.yml similarity index 82% rename from .markdown-lint.yml rename to .markdownlint.yml index 614af8a3..16c2116f 100644 --- a/.markdown-lint.yml +++ b/.markdownlint.yml @@ -11,5 +11,13 @@ # any violations you want # +# Unordered list style +MD004: false + +MD007: + # Unordered list indentation + indent: 2 + # We don't care about inline HTML MD033: false + diff --git a/README.md b/README.md index 7cb52021..38ff65d0 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,9 @@ forcefully stopped/killed), which will: \(because `git` likes to operate locally) * `git add `[a] / `git add .`[b] * `git commit -m "Scripted auto-commit on change ()"`[a] / `git commit + -a -m"Scripted auto-commit on change ()"`[b] + * if a remote is defined (with `-r`) do a push after the commit (a specific branch can be selected with `-b`) From 8ebe135b0abc9fc7ad8b4f8ec0160cc3eb0e0f56 Mon Sep 17 00:00:00 2001 From: Stefan Jaud <59165496+pjanck@users.noreply.github.com> Date: Wed, 24 Nov 2021 18:13:02 +0100 Subject: [PATCH 18/95] Update markdown broken rendering (main README) (#102) * Update markdown broken rendering (main README) Redesigned bullet points of the actions taken by the script for file / directory, also made current --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 38ff65d0..4895767c 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ brew install coreutils ## What it does When you start the script, it prepares some variables and checks if the file -[a] or directory [b] given as input really exists. +or directory given as input really exists. Then it goes into the main loop (which will run forever, until the script is forcefully stopped/killed), which will: @@ -111,13 +111,14 @@ forcefully stopped/killed), which will: * watch for changes to the file/directory using `inotifywait` (`inotifywait` will block until something happens) * wait 2 seconds -* `cd` into the directory [b] / the directory containing the file [a] - \(because `git` likes to operate locally) -* `git add `[a] / `git add .`[b] -* `git commit -m "Scripted auto-commit on change ()"`[a] / `git commit - - -a -m"Scripted auto-commit on change ()"`[b] - +* case file: + * `cd` into the directory containing the file (because `git` likes to operate locally) + * `git add ` + * `git commit -m "Scripted auto-commit on change ()"` +* case directory: + * `cd` into the directory (because `git` likes to operate locally) + * `git add --all .` + * `git commit -m "Scripted auto-commit on change ()"` * if a remote is defined (with `-r`) do a push after the commit (a specific branch can be selected with `-b`) From 7b4001b095306f784eb7658ccb123b2ab107cb8f Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Thu, 2 Dec 2021 21:14:58 -0600 Subject: [PATCH 19/95] changed startup-shutdown.bash to executable to make linter happy --- tests/startup-shutdown.bash | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 tests/startup-shutdown.bash diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash old mode 100644 new mode 100755 From d45d320ac21835c4ae64b5549ede479db8d1bc87 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Thu, 2 Dec 2021 21:24:23 -0600 Subject: [PATCH 20/95] Excluding symbolic link from linting --- .github/workflows/gitwatch.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 069dd5bf..f05e9671 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -30,6 +30,11 @@ jobs: env: DEFAULT_BRANCH: master GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Linter is having errors at symbolic link, appears to be + # this bug: + # https://github.com/github/super-linter/issues/1400 + # https://github.com/kucherenko/jscpd/issues/481 + FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml #----------------------------------------------------------------------------- bats: From 10fbad835fcc1020da475c3e93dad31c44994808 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Mon, 25 Apr 2022 07:17:12 -0500 Subject: [PATCH 21/95] upgraded super-linter to version 4 --- .github/workflows/gitwatch.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index f05e9671..0ef59859 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -26,7 +26,7 @@ jobs: fetch-depth: 0 - name: Run Super-Linter - uses: github/super-linter@v3 + uses: github/super-linter@v4 env: DEFAULT_BRANCH: master GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 1e9becb61692c76f5b14a4d1bca721682c7b6872 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 12:57:57 -0500 Subject: [PATCH 22/95] removed eval as per linter recommendation https://github.com/koalaman/shellcheck/wiki/SC2294 --- gitwatch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index 82c88202..0982d647 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -317,7 +317,7 @@ diff-lines() { # 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 -eval "$INW" "${INW_ARGS[@]}" | while read -r line; do +"$INW" "${INW_ARGS[@]}" | while read -r line; do # is there already a timeout process running? if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then # kill it and wait for completion From b9ddc72d2547766680bf72fe97b4faf0a781fa91 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 13:01:24 -0500 Subject: [PATCH 23/95] Put eval back in, since it broke functionality --- gitwatch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index 0982d647..82c88202 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -317,7 +317,7 @@ diff-lines() { # 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 -"$INW" "${INW_ARGS[@]}" | while read -r line; do +eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # is there already a timeout process running? if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then # kill it and wait for completion From d22e186654ad0dcf2963ccdb33013ce782383680 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 13:02:47 -0500 Subject: [PATCH 24/95] ignored linter error SC2294 --- .markdownlint.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.markdownlint.yml b/.markdownlint.yml index 16c2116f..99756aa7 100644 --- a/.markdownlint.yml +++ b/.markdownlint.yml @@ -21,3 +21,5 @@ MD007: # We don't care about inline HTML MD033: false +# Removing eval as recommended broke code; might fix later, but not now +SC2294: false From 5371385aa1123f9196b965081687adc927acbbbf Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 13:16:18 -0500 Subject: [PATCH 25/95] more linter bug squashing --- .github/workflows/gitwatch.yml | 5 +++-- tests/commitlog.bats | 3 ++- tests/remotedirs.bats | 3 ++- tests/status-change.bats | 3 ++- tests/sync.bats | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 0ef59859..0be6f187 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -1,3 +1,4 @@ +--- name: Gitwatch QA on: @@ -9,7 +10,7 @@ on: workflow_dispatch: jobs: - #----------------------------------------------------------------------------- + #------------------------------------------------------------------------- super-lint: name: Lint Code Base @@ -36,7 +37,7 @@ jobs: # https://github.com/kucherenko/jscpd/issues/481 FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml - #----------------------------------------------------------------------------- + #------------------------------------------------------------------------- bats: name: BATS Tests diff --git a/tests/commitlog.bats b/tests/commitlog.bats index bbe300c0..863cbf37 100644 --- a/tests/commitlog.bats +++ b/tests/commitlog.bats @@ -7,7 +7,8 @@ load startup-shutdown -@test "commit log messages working" { +@test "commit log messages working" +{ # Start up gitwatch with logging, see if works ${BATS_TEST_DIRNAME}/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! diff --git a/tests/remotedirs.bats b/tests/remotedirs.bats index bbb1e941..302b20b7 100644 --- a/tests/remotedirs.bats +++ b/tests/remotedirs.bats @@ -7,7 +7,8 @@ load startup-shutdown -@test "remote git dirs working, with commit logging" { +@test "remote git dirs working, with commit logging" +{ # Move .git somewhere else dotgittestdir=$(mktemp -d) mv "$testdir/local/remote/.git" "$dotgittestdir" diff --git a/tests/status-change.bats b/tests/status-change.bats index be187329..5b6b8fe1 100644 --- a/tests/status-change.bats +++ b/tests/status-change.bats @@ -7,7 +7,8 @@ load startup-shutdown -@test "commit only when git status change" { +@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>&- & diff --git a/tests/sync.bats b/tests/sync.bats index cdbe0d13..3f40ee96 100644 --- a/tests/sync.bats +++ b/tests/sync.bats @@ -7,7 +7,8 @@ load startup-shutdown -@test "syncing correctly" { +@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>- & From 27eadad8a98c4886ac5b4b5966b80cbc516ca02d Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 13:29:39 -0500 Subject: [PATCH 26/95] attempt at new test syntax to fix linter bug --- tests/commitlog.bats | 3 +-- tests/remotedirs.bats | 3 +-- tests/status-change.bats | 3 +-- tests/sync.bats | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/commitlog.bats b/tests/commitlog.bats index 863cbf37..b19ed740 100644 --- a/tests/commitlog.bats +++ b/tests/commitlog.bats @@ -7,8 +7,7 @@ load startup-shutdown -@test "commit log messages working" -{ +function commit_log_messages_working { #@test # Start up gitwatch with logging, see if works ${BATS_TEST_DIRNAME}/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! diff --git a/tests/remotedirs.bats b/tests/remotedirs.bats index 302b20b7..06ff64e1 100644 --- a/tests/remotedirs.bats +++ b/tests/remotedirs.bats @@ -7,8 +7,7 @@ load startup-shutdown -@test "remote git dirs working, with commit logging" -{ +function remote_git_dirs_working_with_commit_logging { #@test # Move .git somewhere else dotgittestdir=$(mktemp -d) mv "$testdir/local/remote/.git" "$dotgittestdir" diff --git a/tests/status-change.bats b/tests/status-change.bats index 5b6b8fe1..7f0524c1 100644 --- a/tests/status-change.bats +++ b/tests/status-change.bats @@ -7,8 +7,7 @@ load startup-shutdown -@test "commit only when git status change" -{ +function commit_only_when_git_status_change { #@test # Start up gitwatch and capture its output ${BATS_TEST_DIRNAME}/../gitwatch.sh "$testdir/local/remote" > "$testdir/output.txt" 3>&- & diff --git a/tests/sync.bats b/tests/sync.bats index 3f40ee96..29dd969d 100644 --- a/tests/sync.bats +++ b/tests/sync.bats @@ -7,8 +7,7 @@ load startup-shutdown -@test "syncing correctly" -{ +function syncing_correctly { #@test # 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>- & From 619ee1ac752c4db6b06d867343aeec3a21194702 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 14:13:38 -0500 Subject: [PATCH 27/95] fixed commitlog linting errors --- tests/commitlog.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/commitlog.bats b/tests/commitlog.bats index b19ed740..1f96266c 100644 --- a/tests/commitlog.bats +++ b/tests/commitlog.bats @@ -9,7 +9,7 @@ load startup-shutdown function commit_log_messages_working { #@test # 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 @@ -25,11 +25,11 @@ function commit_log_messages_working { #@test # Wait a bit for inotify to figure out the file has changed, and do its add, # and commit - sleep $WAITTIME + sleep "$WAITTIME" # Make a new change echo "line2" >> file1.txt - sleep $WAITTIME + sleep "$WAITTIME" # Check commit log that the diff is in there run git log -1 --oneline From 1361d2397eaa1a7336dbd4ecc120fc1b286d2191 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 14:33:48 -0500 Subject: [PATCH 28/95] attempting to also ignore bats files from linter --- .github/workflows/gitwatch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 0be6f187..e618f1e0 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -35,7 +35,8 @@ jobs: # this bug: # https://github.com/github/super-linter/issues/1400 # https://github.com/kucherenko/jscpd/issues/481 - FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml + # Test files are bats files, which don't follow bash standards + FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* #------------------------------------------------------------------------- bats: From 9336cb18b925123cb79b99f21c788abd7199fc0d Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 14:40:54 -0500 Subject: [PATCH 29/95] try just tests --- .github/workflows/gitwatch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index e618f1e0..6693d06f 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -36,7 +36,8 @@ jobs: # https://github.com/github/super-linter/issues/1400 # https://github.com/kucherenko/jscpd/issues/481 # Test files are bats files, which don't follow bash standards - FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* + # FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* + FILTER_REGEX_EXCLUDE: tests\.* #------------------------------------------------------------------------- bats: From 46d21b9836c8d94a1737b5dadb8fe143971d0230 Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 15:03:46 -0500 Subject: [PATCH 30/95] tried renaming super linter action --- .github/workflows/gitwatch.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 6693d06f..27430eca 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -26,7 +26,7 @@ jobs: with: fetch-depth: 0 - - name: Run Super-Linter + - name: Lint Code Base uses: github/super-linter@v4 env: DEFAULT_BRANCH: master From f94f523f012a9e6faa7bcdf7a7165f13e46e1bea Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Tue, 3 May 2022 15:12:10 -0500 Subject: [PATCH 31/95] gave up and removed super linter --- .github/workflows/gitwatch.yml | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 27430eca..37bad4c3 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -26,18 +26,21 @@ jobs: with: fetch-depth: 0 - - name: Lint Code Base - uses: github/super-linter@v4 - env: - DEFAULT_BRANCH: master - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # Linter is having errors at symbolic link, appears to be - # this bug: - # https://github.com/github/super-linter/issues/1400 - # https://github.com/kucherenko/jscpd/issues/481 - # Test files are bats files, which don't follow bash standards - # FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* - FILTER_REGEX_EXCLUDE: tests\.* + # For the time being, I'm giving up on Super Linter. It's a great tool, + # but it's currently broken, and it's taking more time to make it work than + # it's worth. + # - name: Lint Code Base + # uses: github/super-linter@v4 + # env: + # DEFAULT_BRANCH: master + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # # Linter is having errors at symbolic link, appears to be + # # this bug: + # # https://github.com/github/super-linter/issues/1400 + # # https://github.com/kucherenko/jscpd/issues/481 + # # Test files are bats files, which don't follow bash standards + # # FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* + # # FILTER_REGEX_EXCLUDE: tests\.* #------------------------------------------------------------------------- bats: 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 32/95] 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 82c88202..c9c39106 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 From 770874a1884953b945a975adbb953e95acfa635a Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 08:06:52 -0600 Subject: [PATCH 33/95] Installing new version of bats --- .github/workflows/gitwatch.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 37bad4c3..48ea4636 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -22,9 +22,12 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: fetch-depth: 0 + - name: Setup node + uses: actions/setup-node@v3 + - run: sudo npm install -g bats # For the time being, I'm giving up on Super Linter. It's a great tool, # but it's currently broken, and it's taking more time to make it work than From aef672957c154150fd9afcd3d12efecd7ce31302 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 08:09:29 -0600 Subject: [PATCH 34/95] Try again installing updated bats --- .github/workflows/gitwatch.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 48ea4636..a165a969 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -25,9 +25,6 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup node - uses: actions/setup-node@v3 - - run: sudo npm install -g bats # For the time being, I'm giving up on Super Linter. It's a great tool, # but it's currently broken, and it's taking more time to make it work than @@ -62,9 +59,11 @@ jobs: timeout-minutes: 5 steps: + - name: Setup node + uses: actions/setup-node@v3 - name: Setup BATS # Even though this says 1.2.0,it's actually using 1.2.1 - uses: mig4/setup-bats@v1.2.0 + run: sudo npm install -g bats - name: Install gitwatch dependencies shell: bash From 55875c8374d6ac7157eff120b9a4c8f6154e03a8 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 08:22:23 -0600 Subject: [PATCH 35/95] trying to diagnose hanging mac --- tests/zcommit.bats | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/zcommit.bats diff --git a/tests/zcommit.bats b/tests/zcommit.bats new file mode 100644 index 00000000..a3ee2acc --- /dev/null +++ b/tests/zcommit.bats @@ -0,0 +1,38 @@ +#!/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 + +function zcommit_log_messages_working { #@test + # Start up gitwatch with logging, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 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 "$WAITTIME" + + # Make a new change + echo "line2" >> file1.txt + sleep "$WAITTIME" + + # Check commit log that the diff is in there + run git log -1 --oneline + [[ $output == *"file1.txt"* ]] +} + From f2b4b7a70b3a85aa1a4cf0ac60225e08ca548c74 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 08:49:19 -0600 Subject: [PATCH 36/95] hanging mac: trying to echo --- tests/startup-shutdown.bash | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash index 6c5e1869..a6993659 100755 --- a/tests/startup-shutdown.bash +++ b/tests/startup-shutdown.bash @@ -20,6 +20,7 @@ setup() { } teardown() { + echo '# Teardown started' >&3 # Remove testing directories # shellcheck disable=SC2164 cd /tmp @@ -29,4 +30,6 @@ teardown() { # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated pkill -15 -P "$GITWATCH_PID" + + echo '# Teardown complete' >&3 } From 1bff25243977b6e4ed53e42d70471f595ea03881 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 08:56:41 -0600 Subject: [PATCH 37/95] try a test step --- .github/workflows/gitwatch.yml | 4 +++- tests/zcommit.bats | 38 ---------------------------------- 2 files changed, 3 insertions(+), 39 deletions(-) delete mode 100644 tests/zcommit.bats diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index a165a969..056ecf5f 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -62,7 +62,6 @@ jobs: - name: Setup node uses: actions/setup-node@v3 - name: Setup BATS - # Even though this says 1.2.0,it's actually using 1.2.1 run: sudo npm install -g bats - name: Install gitwatch dependencies @@ -89,3 +88,6 @@ jobs: git config --global user.email "test@email.com" git config --global user.name "test user" bats -rt tests + + - name: Test step + run: echo "Now at test step" diff --git a/tests/zcommit.bats b/tests/zcommit.bats deleted file mode 100644 index a3ee2acc..00000000 --- a/tests/zcommit.bats +++ /dev/null @@ -1,38 +0,0 @@ -#!/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 - -function zcommit_log_messages_working { #@test - # Start up gitwatch with logging, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 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 "$WAITTIME" - - # Make a new change - echo "line2" >> file1.txt - sleep "$WAITTIME" - - # Check commit log that the diff is in there - run git log -1 --oneline - [[ $output == *"file1.txt"* ]] -} - From 152d28565242ed133685e6f95f9fdf1dde2cda9f Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:01:31 -0600 Subject: [PATCH 38/95] Mac try removing tests --- .github/workflows/gitwatch.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 056ecf5f..afdb2de1 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -80,14 +80,14 @@ jobs: fi - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - - name: Run tests - shell: bash - run: | - git config --global user.email "test@email.com" - git config --global user.name "test user" - bats -rt tests + # - name: Run tests + # shell: bash + # run: | + # git config --global user.email "test@email.com" + # git config --global user.name "test user" + # bats -rt tests - name: Test step run: echo "Now at test step" From 3dcd7c6e62818275b95f85f8f89c6df95d2b4356 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:03:17 -0600 Subject: [PATCH 39/95] just git config back in --- .github/workflows/gitwatch.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index afdb2de1..eee7bc21 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -82,11 +82,11 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - # - name: Run tests - # shell: bash - # run: | - # git config --global user.email "test@email.com" - # git config --global user.name "test user" + - name: Run tests + shell: bash + run: | + git config --global user.email "test@email.com" + git config --global user.name "test user" # bats -rt tests - name: Test step From 0eb1394e35f446e02c1c87bc042f8560e1c67e03 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:05:04 -0600 Subject: [PATCH 40/95] now trying to echo when done --- .github/workflows/gitwatch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index eee7bc21..4eefa970 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -87,7 +87,8 @@ jobs: run: | git config --global user.email "test@email.com" git config --global user.name "test user" - # bats -rt tests + bats -rt tests + echo "Finished running tests" - name: Test step run: echo "Now at test step" From 8ccf2e080ea1f8381910191f770cdebe10c22ab5 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:10:01 -0600 Subject: [PATCH 41/95] try just counting tests --- .github/workflows/gitwatch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 4eefa970..4e85056d 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -87,7 +87,8 @@ jobs: run: | git config --global user.email "test@email.com" git config --global user.name "test user" - bats -rt tests + bats -c tests + #bats -rt tests echo "Finished running tests" - name: Test step From 26cc1b12392cf37d89b68fc24467c846054c333c Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:14:40 -0600 Subject: [PATCH 42/95] trying a simple test --- .github/workflows/gitwatch.yml | 2 +- othertests/tryit.bats | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 othertests/tryit.bats diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 4e85056d..a549b3b1 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -87,8 +87,8 @@ jobs: run: | git config --global user.email "test@email.com" git config --global user.name "test user" - bats -c tests #bats -rt tests + bats -rt othertests echo "Finished running tests" - name: Test step diff --git a/othertests/tryit.bats b/othertests/tryit.bats new file mode 100644 index 00000000..9f875991 --- /dev/null +++ b/othertests/tryit.bats @@ -0,0 +1,11 @@ +#!/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 + + +function commit_log_messages_working { #@test + echo "Test complete" +} From 97c5f49e1b6ea5120b72664b37ad3a1f91fa4f43 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:19:03 -0600 Subject: [PATCH 43/95] startup shutdown included --- othertests/tryit.bats | 1 + 1 file changed, 1 insertion(+) diff --git a/othertests/tryit.bats b/othertests/tryit.bats index 9f875991..42050b73 100644 --- a/othertests/tryit.bats +++ b/othertests/tryit.bats @@ -5,6 +5,7 @@ # To run it, at a command prompt: # bats testscript.bats +load startup-shutdown function commit_log_messages_working { #@test echo "Test complete" From 450572230f08ed342e306d9c495272a93da54166 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:21:42 -0600 Subject: [PATCH 44/95] included startup/shutdown --- othertests/startup-shutdown.bash | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 othertests/startup-shutdown.bash diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash new file mode 100755 index 00000000..a6993659 --- /dev/null +++ b/othertests/startup-shutdown.bash @@ -0,0 +1,35 @@ +setup() { + # Time to wait for gitwatch to respond + # shellcheck disable=SC2034 + WAITTIME=4 + # Set up directory structure and initialize remote + testdir=$(mktemp -d) + # shellcheck disable=SC2164 + cd "$testdir" + mkdir remote + # shellcheck disable=SC2164 + cd remote + git init -q --bare + # shellcheck disable=SC2103 + cd .. + # shellcheck disable=SC2164 + mkdir local + # shellcheck disable=SC2164 + cd local + git clone -q ../remote +} + +teardown() { + echo '# Teardown started' >&3 + # Remove testing directories + # shellcheck disable=SC2164 + cd /tmp + + # rm -rf $testdir + + # Make sure gitwatch script gets killed if script stopped background + # Must kill the entire tree of processes generated + pkill -15 -P "$GITWATCH_PID" + + echo '# Teardown complete' >&3 +} From 09d9565183cb934ccbfe3325ff392a6b1b829ee9 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:24:57 -0600 Subject: [PATCH 45/95] start test with gitwatch launching --- othertests/tryit.bats | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/othertests/tryit.bats b/othertests/tryit.bats index 42050b73..fc859752 100644 --- a/othertests/tryit.bats +++ b/othertests/tryit.bats @@ -8,5 +8,38 @@ load startup-shutdown function commit_log_messages_working { #@test + # Start up gitwatch with logging, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! + echo "Test complete" } + +# function commit_log_messages_working { #@test +# # Start up gitwatch with logging, see if works +# "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 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 "$WAITTIME" + +# # Make a new change +# echo "line2" >> file1.txt +# sleep "$WAITTIME" + +# # Check commit log that the diff is in there +# run git log -1 --oneline +# [[ $output == *"file1.txt"* ]] +# } From a2dc46413d89b325d2f34a275e0f59ff5871bf7c Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:26:43 -0600 Subject: [PATCH 46/95] fixed bad function line --- othertests/tryit.bats | 1 - 1 file changed, 1 deletion(-) diff --git a/othertests/tryit.bats b/othertests/tryit.bats index fc859752..de1b577b 100644 --- a/othertests/tryit.bats +++ b/othertests/tryit.bats @@ -15,7 +15,6 @@ function commit_log_messages_working { #@test echo "Test complete" } -# function commit_log_messages_working { #@test # # Start up gitwatch with logging, see if works # "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & # GITWATCH_PID=$! From e639ce29b97a2a8dfa07a31b5a5afd3e0ed0cfee Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:31:58 -0600 Subject: [PATCH 47/95] see if a kill -9 does the trick on Mac --- othertests/startup-shutdown.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index a6993659..29b2b9b0 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -29,7 +29,7 @@ teardown() { # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated - pkill -15 -P "$GITWATCH_PID" + pkill -9 -P "$GITWATCH_PID" echo '# Teardown complete' >&3 } From c558fc9db4909487f2a5d3e33b9ba22584004df8 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:37:50 -0600 Subject: [PATCH 48/95] printing process id --- othertests/startup-shutdown.bash | 1 + othertests/tryit.bats | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index 29b2b9b0..97f2e823 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -27,6 +27,7 @@ teardown() { # rm -rf $testdir + echo "Process id again $GITWATCH_PID" >&3 # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated pkill -9 -P "$GITWATCH_PID" diff --git a/othertests/tryit.bats b/othertests/tryit.bats index de1b577b..30c8c4c0 100644 --- a/othertests/tryit.bats +++ b/othertests/tryit.bats @@ -12,7 +12,8 @@ function commit_log_messages_working { #@test "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! - echo "Test complete" + echo "Process id $GITWATCH_PID" >&3 + echo "Test complete" >&3 } # # Start up gitwatch with logging, see if works From d580ff0f90377e43870fc0c3181c74295de38203 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 09:43:19 -0600 Subject: [PATCH 49/95] try enabling ssh to debug --- .github/workflows/gitwatch.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index a549b3b1..4ec431e7 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -82,6 +82,9 @@ jobs: - name: Checkout code uses: actions/checkout@v3 + - name: Setup upterm session + uses: lhotari/action-upterm@v1 + - name: Run tests shell: bash run: | From f03dabbeb0130338a383ca7a252d6b14d1b41220 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:04:34 -0600 Subject: [PATCH 50/95] fswatch kill in --- othertests/startup-shutdown.bash | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index 97f2e823..c960d75c 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -30,7 +30,9 @@ teardown() { echo "Process id again $GITWATCH_PID" >&3 # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated - pkill -9 -P "$GITWATCH_PID" + pkill -15 -P "$GITWATCH_PID" + # Also make sure to kill fswatch if on Mac + killall fswatch echo '# Teardown complete' >&3 } From f751f423d0533ff28a5e592c3c3f801a2542717d Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:06:35 -0600 Subject: [PATCH 51/95] removed upterm --- .github/workflows/gitwatch.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 4ec431e7..5bab280b 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -82,8 +82,8 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - - name: Setup upterm session - uses: lhotari/action-upterm@v1 + # - name: Setup upterm session + # uses: lhotari/action-upterm@v1 - name: Run tests shell: bash From 351d2438842a4d721a34ec07465b2757463615be Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:11:03 -0600 Subject: [PATCH 52/95] trying a killall -9 --- othertests/startup-shutdown.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index c960d75c..2557f89d 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -32,7 +32,7 @@ teardown() { # Must kill the entire tree of processes generated pkill -15 -P "$GITWATCH_PID" # Also make sure to kill fswatch if on Mac - killall fswatch + killall -9 fswatch echo '# Teardown complete' >&3 } From 532cae8610d1f27ebf2ed08ea55bdcac23a17df0 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:14:36 -0600 Subject: [PATCH 53/95] ssh back in again to try to figure out why not --- .github/workflows/gitwatch.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 5bab280b..4ec431e7 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -82,8 +82,8 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - # - name: Setup upterm session - # uses: lhotari/action-upterm@v1 + - name: Setup upterm session + uses: lhotari/action-upterm@v1 - name: Run tests shell: bash From 05c9450fd3ae8ee0ea46913be640bc6e02b0e37a Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:23:53 -0600 Subject: [PATCH 54/95] reordered fswatch kill --- .github/workflows/gitwatch.yml | 4 ++-- othertests/startup-shutdown.bash | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 4ec431e7..5bab280b 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -82,8 +82,8 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - - name: Setup upterm session - uses: lhotari/action-upterm@v1 + # - name: Setup upterm session + # uses: lhotari/action-upterm@v1 - name: Run tests shell: bash diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index 2557f89d..db3435dc 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -29,10 +29,10 @@ teardown() { echo "Process id again $GITWATCH_PID" >&3 # Make sure gitwatch script gets killed if script stopped background + # Also make sure to kill fswatch if on Mac + killall fswatch # Must kill the entire tree of processes generated pkill -15 -P "$GITWATCH_PID" - # Also make sure to kill fswatch if on Mac - killall -9 fswatch echo '# Teardown complete' >&3 } From 25d82797c5b7950eead750d082b97016cdf208bd Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:34:22 -0600 Subject: [PATCH 55/95] try with fix from bats site --- othertests/startup-shutdown.bash | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index db3435dc..d8af6887 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -19,6 +19,17 @@ setup() { git clone -q ../remote } +function close_non_std_fds() { + local open_fds non_std_fds=() + get_open_fds + for fd in "${open_fds[@]}"; do + if [[ $fd -gt 2 ]]; then + non_std_fds+=("$fd") + fi + done + close_fds "${non_std_fds[@]}" +} + teardown() { echo '# Teardown started' >&3 # Remove testing directories @@ -34,5 +45,8 @@ teardown() { # Must kill the entire tree of processes generated pkill -15 -P "$GITWATCH_PID" + close_non_std_fds + echo '# Teardown complete' >&3 + } From b24bf2c4c42e6f135d5f4b8e5faf1ff5e392aa69 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:39:29 -0600 Subject: [PATCH 56/95] yet another attempt --- othertests/startup-shutdown.bash | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash index d8af6887..9fa14b0b 100755 --- a/othertests/startup-shutdown.bash +++ b/othertests/startup-shutdown.bash @@ -19,17 +19,6 @@ setup() { git clone -q ../remote } -function close_non_std_fds() { - local open_fds non_std_fds=() - get_open_fds - for fd in "${open_fds[@]}"; do - if [[ $fd -gt 2 ]]; then - non_std_fds+=("$fd") - fi - done - close_fds "${non_std_fds[@]}" -} - teardown() { echo '# Teardown started' >&3 # Remove testing directories @@ -39,6 +28,11 @@ teardown() { # rm -rf $testdir echo "Process id again $GITWATCH_PID" >&3 + + # Try killing background process + kill -9 %1 + fg + # Make sure gitwatch script gets killed if script stopped background # Also make sure to kill fswatch if on Mac killall fswatch From ab9e5d70bcc3c13d57d917ee35b8626bc8944150 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:44:27 -0600 Subject: [PATCH 57/95] tests back in --- .github/workflows/gitwatch.yml | 4 ++-- tests/startup-shutdown.bash | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 5bab280b..3c2d9a80 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -90,8 +90,8 @@ jobs: run: | git config --global user.email "test@email.com" git config --global user.name "test user" - #bats -rt tests - bats -rt othertests + bats -rt tests + #bats -rt othertests echo "Finished running tests" - name: Test step diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash index a6993659..54e40143 100755 --- a/tests/startup-shutdown.bash +++ b/tests/startup-shutdown.bash @@ -25,7 +25,9 @@ teardown() { # shellcheck disable=SC2164 cd /tmp - # rm -rf $testdir + # Kill background process + kill -9 %1 + fg # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated From 02377d857a58fd8a7bcd4d6698d073c8feb6ea92 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 10:48:52 -0600 Subject: [PATCH 58/95] trying again --- tests/startup-shutdown.bash | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash index 54e40143..d1410474 100755 --- a/tests/startup-shutdown.bash +++ b/tests/startup-shutdown.bash @@ -29,6 +29,8 @@ teardown() { kill -9 %1 fg + # Also make sure to kill fswatch if on Mac + killall fswatch # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated pkill -15 -P "$GITWATCH_PID" From e6d61cdb14c6919c7abbebde95bcb0f4e08c1778 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 11:02:13 -0600 Subject: [PATCH 59/95] try new test for spaces --- tests/spaces.bats | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/spaces.bats diff --git a/tests/spaces.bats b/tests/spaces.bats new file mode 100644 index 00000000..7d5e2d9a --- /dev/null +++ b/tests/spaces.bats @@ -0,0 +1,67 @@ +#!/usr/bin/env bats + +function spaces_in_target_dir { #@test + # Time to wait for gitwatch to respond + # shellcheck disable=SC2034 + WAITTIME=4 + # Set up directory structure and initialize remote + testdir=$(mktemp -d "/tmp/tmp space.XXXXXXX") + # shellcheck disable=SC2164 + cd "$testdir" + mkdir remote + # shellcheck disable=SC2164 + cd remote + git init -q --bare + # shellcheck disable=SC2103 + cd .. + # shellcheck disable=SC2164 + mkdir local + # shellcheck disable=SC2164 + cd local + git clone -q ../remote + + # Start up gitwatch with logging, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 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 "$WAITTIME" + + # Make a new change + echo "line2" >> file1.txt + sleep "$WAITTIME" + + # Check commit log that the diff is in there + run git log -1 --oneline + [[ $output == *"file1.txt"* ]] + + echo '# Teardown started' >&3 + # Remove testing directories + # shellcheck disable=SC2164 + cd /tmp + + # Kill background process + kill -9 %1 + fg + + # Also make sure to kill fswatch if on Mac + killall fswatch + # Make sure gitwatch script gets killed if script stopped background + # Must kill the entire tree of processes generated + pkill -15 -P "$GITWATCH_PID" + + echo '# Teardown complete' >&3 +} + From dd8164a0a99fd44991260860966605dd45ba276c Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 11:26:00 -0600 Subject: [PATCH 60/95] trying again --- tests/spaces.bats | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/spaces.bats b/tests/spaces.bats index 7d5e2d9a..f5c7566e 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -23,6 +23,7 @@ function spaces_in_target_dir { #@test # Start up gitwatch with logging, see if works "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! + echo "$GITWATCH_PID" # Keeps kill message from printing to screen disown @@ -30,22 +31,22 @@ function spaces_in_target_dir { #@test # 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 + # # 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" + # # Wait a bit for inotify to figure out the file has changed, and do its add, + # # and commit + # sleep "$WAITTIME" - # Make a new change - echo "line2" >> file1.txt - sleep "$WAITTIME" + # # Make a new change + # echo "line2" >> file1.txt + # sleep "$WAITTIME" - # Check commit log that the diff is in there - run git log -1 --oneline - [[ $output == *"file1.txt"* ]] + # # Check commit log that the diff is in there + # run git log -1 --oneline + # [[ $output == *"file1.txt"* ]] echo '# Teardown started' >&3 # Remove testing directories @@ -53,14 +54,16 @@ function spaces_in_target_dir { #@test cd /tmp # Kill background process - kill -9 %1 - fg + # kill -9 %1 + # fg # Also make sure to kill fswatch if on Mac - killall fswatch + killall fswatch || true # Make sure gitwatch script gets killed if script stopped background # Must kill the entire tree of processes generated - pkill -15 -P "$GITWATCH_PID" + pkill -15 -f gitwatch.sh + pkill -15 -f gitwatch.sh + # pkill -15 -P "$GITWATCH_PID" echo '# Teardown complete' >&3 } From 641230668fbda2362855933634dcb827df57dc18 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 11:29:54 -0600 Subject: [PATCH 61/95] cleaned up test, looks good --- othertests/startup-shutdown.bash | 46 -------------------------------- othertests/tryit.bats | 45 ------------------------------- tests/spaces.bats | 19 ++----------- 3 files changed, 2 insertions(+), 108 deletions(-) delete mode 100755 othertests/startup-shutdown.bash delete mode 100644 othertests/tryit.bats diff --git a/othertests/startup-shutdown.bash b/othertests/startup-shutdown.bash deleted file mode 100755 index 9fa14b0b..00000000 --- a/othertests/startup-shutdown.bash +++ /dev/null @@ -1,46 +0,0 @@ -setup() { - # Time to wait for gitwatch to respond - # shellcheck disable=SC2034 - WAITTIME=4 - # Set up directory structure and initialize remote - testdir=$(mktemp -d) - # shellcheck disable=SC2164 - cd "$testdir" - mkdir remote - # shellcheck disable=SC2164 - cd remote - git init -q --bare - # shellcheck disable=SC2103 - cd .. - # shellcheck disable=SC2164 - mkdir local - # shellcheck disable=SC2164 - cd local - git clone -q ../remote -} - -teardown() { - echo '# Teardown started' >&3 - # Remove testing directories - # shellcheck disable=SC2164 - cd /tmp - - # rm -rf $testdir - - echo "Process id again $GITWATCH_PID" >&3 - - # Try killing background process - kill -9 %1 - fg - - # Make sure gitwatch script gets killed if script stopped background - # Also make sure to kill fswatch if on Mac - killall fswatch - # Must kill the entire tree of processes generated - pkill -15 -P "$GITWATCH_PID" - - close_non_std_fds - - echo '# Teardown complete' >&3 - -} diff --git a/othertests/tryit.bats b/othertests/tryit.bats deleted file mode 100644 index 30c8c4c0..00000000 --- a/othertests/tryit.bats +++ /dev/null @@ -1,45 +0,0 @@ -#!/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 - -function commit_log_messages_working { #@test - # Start up gitwatch with logging, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! - - echo "Process id $GITWATCH_PID" >&3 - echo "Test complete" >&3 -} - -# # Start up gitwatch with logging, see if works -# "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 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 "$WAITTIME" - -# # Make a new change -# echo "line2" >> file1.txt -# sleep "$WAITTIME" - -# # Check commit log that the diff is in there -# run git log -1 --oneline -# [[ $output == *"file1.txt"* ]] -# } diff --git a/tests/spaces.bats b/tests/spaces.bats index f5c7566e..f9b6f8c8 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -1,5 +1,7 @@ #!/usr/bin/env bats +# This inserts customs setup and teardown because of spaces in the file name + function spaces_in_target_dir { #@test # Time to wait for gitwatch to respond # shellcheck disable=SC2034 @@ -31,23 +33,6 @@ function spaces_in_target_dir { #@test # 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 "$WAITTIME" - - # # Make a new change - # echo "line2" >> file1.txt - # sleep "$WAITTIME" - - # # Check commit log that the diff is in there - # run git log -1 --oneline - # [[ $output == *"file1.txt"* ]] - echo '# Teardown started' >&3 # Remove testing directories # shellcheck disable=SC2164 From 910f143505608bd0107509637ccbfce05ed83863 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 11:38:56 -0600 Subject: [PATCH 62/95] take space out to make sure tests pass --- tests/spaces.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spaces.bats b/tests/spaces.bats index f9b6f8c8..8c7cd15a 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -7,7 +7,7 @@ function spaces_in_target_dir { #@test # shellcheck disable=SC2034 WAITTIME=4 # Set up directory structure and initialize remote - testdir=$(mktemp -d "/tmp/tmp space.XXXXXXX") + testdir=$(mktemp -d "/tmp/tmpspace.XXXXXXX") # shellcheck disable=SC2164 cd "$testdir" mkdir remote From 0bbf2b584834915711e202fdcad0574ba7ed7b8a Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 11:57:38 -0600 Subject: [PATCH 63/95] new approach for trying to dup space bug --- tests/spaces.bats | 50 +++++++++--------------------- tests/startup-shutdown-spaces.bash | 41 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 35 deletions(-) create mode 100755 tests/startup-shutdown-spaces.bash diff --git a/tests/spaces.bats b/tests/spaces.bats index 8c7cd15a..510b6633 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -2,30 +2,12 @@ # This inserts customs setup and teardown because of spaces in the file name -function spaces_in_target_dir { #@test - # Time to wait for gitwatch to respond - # shellcheck disable=SC2034 - WAITTIME=4 - # Set up directory structure and initialize remote - testdir=$(mktemp -d "/tmp/tmpspace.XXXXXXX") - # shellcheck disable=SC2164 - cd "$testdir" - mkdir remote - # shellcheck disable=SC2164 - cd remote - git init -q --bare - # shellcheck disable=SC2103 - cd .. - # shellcheck disable=SC2164 - mkdir local - # shellcheck disable=SC2164 - cd local - git clone -q ../remote +load startup-shutdown-spaces +function spaces_in_target_dir { #@test # Start up gitwatch with logging, see if works "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & GITWATCH_PID=$! - echo "$GITWATCH_PID" # Keeps kill message from printing to screen disown @@ -33,23 +15,21 @@ function spaces_in_target_dir { #@test # Create a file, verify that it hasn't been added yet, then commit cd remote - echo '# Teardown started' >&3 - # Remove testing directories - # shellcheck disable=SC2164 - cd /tmp + # 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 - # Kill background process - # kill -9 %1 - # fg + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep "$WAITTIME" - # Also make sure to kill fswatch if on Mac - killall fswatch || true - # Make sure gitwatch script gets killed if script stopped background - # Must kill the entire tree of processes generated - pkill -15 -f gitwatch.sh - pkill -15 -f gitwatch.sh - # pkill -15 -P "$GITWATCH_PID" + # Make a new change + echo "line2" >> file1.txt + sleep "$WAITTIME" - echo '# Teardown complete' >&3 + # Check commit log that the diff is in there + run git log -1 --oneline + [[ $output == *"file1.txt"* ]] } diff --git a/tests/startup-shutdown-spaces.bash b/tests/startup-shutdown-spaces.bash new file mode 100755 index 00000000..82c21fe2 --- /dev/null +++ b/tests/startup-shutdown-spaces.bash @@ -0,0 +1,41 @@ +# This inserts customs setup and teardown because of spaces in the file name + +setup() { + # Time to wait for gitwatch to respond + # shellcheck disable=SC2034 + WAITTIME=4 + # Set up directory structure and initialize remote + testdir=$(mktemp -d "/tmp/tmp space.XXXXXXX") + # shellcheck disable=SC2164 + cd "$testdir" + mkdir remote + # shellcheck disable=SC2164 + cd remote + git init -q --bare + # shellcheck disable=SC2103 + cd .. + # shellcheck disable=SC2164 + mkdir local + # shellcheck disable=SC2164 + cd local + git clone -q ../remote +} + +teardown() { + echo '# Teardown started' >&3 + # Remove testing directories + # shellcheck disable=SC2164 + cd /tmp + + # Kill background process + kill -9 %1 + fg + + # Also make sure to kill fswatch if on Mac + killall fswatch + # Make sure gitwatch script gets killed if script stopped background + # Must kill the entire tree of processes generated + pkill -15 -P "$GITWATCH_PID" + + echo '# Teardown complete' >&3 +} From 8e884278134326d9fdee2cc214cfa65f84d800f7 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 12:02:46 -0600 Subject: [PATCH 64/95] spaces back in --- tests/spaces.bats | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/spaces.bats b/tests/spaces.bats index 510b6633..266a1cae 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -7,6 +7,7 @@ load startup-shutdown-spaces function spaces_in_target_dir { #@test # Start up gitwatch with logging, see if works "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & + echo "Testdir: $testdir" >&3 GITWATCH_PID=$! # Keeps kill message from printing to screen From d757ac2f213be98fdb5d12b4004d58d0149219a8 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 12:12:23 -0600 Subject: [PATCH 65/95] yet again trying spaces --- tests/spaces.bats | 4 ++-- tests/startup-shutdown-spaces.bash | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/spaces.bats b/tests/spaces.bats index 266a1cae..db8726d4 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -6,7 +6,7 @@ load startup-shutdown-spaces function spaces_in_target_dir { #@test # 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 with spaces" 3>&- & echo "Testdir: $testdir" >&3 GITWATCH_PID=$! @@ -14,7 +14,7 @@ function spaces_in_target_dir { #@test disown # Create a file, verify that it hasn't been added yet, then commit - cd remote + cd "remote with spaces" # According to inotify documentation, a race condition results if you write # to directory too soon after it has been created; hence, a short wait. diff --git a/tests/startup-shutdown-spaces.bash b/tests/startup-shutdown-spaces.bash index 82c21fe2..a8856cd8 100755 --- a/tests/startup-shutdown-spaces.bash +++ b/tests/startup-shutdown-spaces.bash @@ -5,7 +5,7 @@ setup() { # shellcheck disable=SC2034 WAITTIME=4 # Set up directory structure and initialize remote - testdir=$(mktemp -d "/tmp/tmp space.XXXXXXX") + testdir=$(mktemp -d) # shellcheck disable=SC2164 cd "$testdir" mkdir remote @@ -18,7 +18,7 @@ setup() { mkdir local # shellcheck disable=SC2164 cd local - git clone -q ../remote + git clone -q ../remote "remote with spaces" } teardown() { From 06fdc07206c03befb9ecb30ed9b65952db62e4f6 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (home machine)" Date: Sun, 4 Dec 2022 12:26:11 -0600 Subject: [PATCH 66/95] one more try --- tests/spaces.bats | 4 ++-- tests/startup-shutdown-spaces.bash | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/spaces.bats b/tests/spaces.bats index db8726d4..09e90e8a 100644 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -6,7 +6,7 @@ load startup-shutdown-spaces function spaces_in_target_dir { #@test # Start up gitwatch with logging, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote with spaces" 3>&- & + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/rem with spaces" 3>&- & echo "Testdir: $testdir" >&3 GITWATCH_PID=$! @@ -14,7 +14,7 @@ function spaces_in_target_dir { #@test disown # Create a file, verify that it hasn't been added yet, then commit - cd "remote with spaces" + cd "rem with spaces" # According to inotify documentation, a race condition results if you write # to directory too soon after it has been created; hence, a short wait. diff --git a/tests/startup-shutdown-spaces.bash b/tests/startup-shutdown-spaces.bash index a8856cd8..dbfecb90 100755 --- a/tests/startup-shutdown-spaces.bash +++ b/tests/startup-shutdown-spaces.bash @@ -5,7 +5,7 @@ setup() { # shellcheck disable=SC2034 WAITTIME=4 # Set up directory structure and initialize remote - testdir=$(mktemp -d) + testdir=$(mktemp -d "/tmp/temp space.XXXXX") # shellcheck disable=SC2164 cd "$testdir" mkdir remote @@ -18,7 +18,7 @@ setup() { mkdir local # shellcheck disable=SC2164 cd local - git clone -q ../remote "remote with spaces" + git clone -q ../remote "rem with spaces" } teardown() { From 7e0b027cfcae358ecf00efbd757d3d46a588f2e6 Mon Sep 17 00:00:00 2001 From: Dave Musicant Date: Fri, 23 Jun 2023 05:56:36 -0500 Subject: [PATCH 67/95] README.md updated to fix path in systemd directions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4895767c..c8ef6659 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ have other calls in `rc.local` after the mentioned line, because the #### systemd -* If installed to a path other than `/usr/bin/gitwatch`, modify +* If installed to a path other than `/usr/local/bin/gitwatch`, modify `gitwatch@.service` to suit * Create dir if it does not exist and copy systemd service file with `mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service 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 68/95] 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 c9c39106..a8eb89af 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 00000000..4c39e076 --- /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 d1410474..943679b7 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 From 5d685af85d44415dee821034688b231555dc36e5 Mon Sep 17 00:00:00 2001 From: Shvedov Yury Date: Tue, 26 Dec 2023 16:21:22 +0300 Subject: [PATCH 69/95] Add support for NixOS (#117) You are able to use it as standalone package, or service either. --- README.md | 28 ++++++++++++++++++ flake.nix | 19 ++++++++++++ gitwatch.nix | 19 ++++++++++++ module.nix | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 flake.nix create mode 100644 gitwatch.nix create mode 100644 module.nix diff --git a/README.md b/README.md index c8ef6659..c026e696 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ * [Update](#update) * [bpkg](#bpkg) * [Archlinux](#archlinux) + * [NixOs](#nixos) + * [As Module](#as-module) + * [As Package](#as-package) * [Requirements](#requirements) * [Notes for Mac](#notes-for-mac) * [What it does](#what-it-does) @@ -84,6 +87,31 @@ the command below. You may need to invoke `bpkg` with `sudo` when using the There is an [AUR](https://aur.archlinux.org/packages/gitwatch-git/) package for Archlinux. Install it with you favorite aur helper. +### NixOs + +#### As Module + +Say you add this as input `gitwatch` to your flake. Then you may want to append +field `gitwatch.modules` to your `nixosSystem` modules. Then you are able to +enable `services.gitwatch.*` service in per-repository mode. Like next: + +```nix +services.gitwatch.my-repo = { + enable = true; + path = "/home/me/my-repo"; + remote = "git@github.com:me/my-repo.git"; + user = "me"; +}; +``` + +This will make NixOS to create `systemd` service for `my-repo` repository. + +#### As Package + +You can to play around with nix package inside this repository. Call `nix run` +in this repository to run `gitwatch` script or `nix shell` to enter shell with +`gitwatch` command available. + ## Requirements To run this script, you must have installed and globally available: diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..ee2c85ea --- /dev/null +++ b/flake.nix @@ -0,0 +1,19 @@ +{ + description = "A bash script to watch a file or folder and commit changes to a git repo"; + outputs = { self, nixpkgs, flake-utils }: + let + packages = flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in + { + packages = rec { + gitwatch = pkgs.callPackage ./gitwatch.sh { }; + default = gitwatch; + }; + }); + in + packages // { modules = [ ./module.nix ]; }; +} diff --git a/gitwatch.nix b/gitwatch.nix new file mode 100644 index 00000000..4205ecd9 --- /dev/null +++ b/gitwatch.nix @@ -0,0 +1,19 @@ +{ runCommandNoCC +, lib +, makeWrapper + +, git +, openssh +, inotify-tools +}: runCommandNoCC "gitwatch" { + nativeBuildInputs = [ makeWrapper ]; +} '' + mkdir -p $out/bin + dest="$out/bin/gitwatch" + cp ${./gitwatch.sh} $dest + chmod +x $dest + patchShebangs $dest + + wrapProgram $dest \ + --prefix PATH ';' ${lib.makeBinPath [ git inotify-tools openssh ]} +'' diff --git a/module.nix b/module.nix new file mode 100644 index 00000000..1f3eeb58 --- /dev/null +++ b/module.nix @@ -0,0 +1,84 @@ +{ lib, pkgs, config, ... }: +let + gitwatch = pkgs.callPackage ./gitwatch.nix { }; + mkSystemdService = name: cfg: lib.nameValuePair + "gitwatch-${name}" + ( + let + getvar = flag: var: + if cfg."${var}" != null + then "${flag} ${cfg."${var}"}" + else ""; + branch = getvar "-b" "branch"; + fetcher = + if cfg.remote == null + then "true" + else '' + ''; + in + { + inherit (cfg) enable; + after = [ "network-online.target" ]; + description = "gitwatch for ${name}"; + path = with pkgs; [ gitwatch git openssh ]; + script = '' + if [ -n "${cfg.remote}" ] && ! [ -d "${cfg.path}" ]; then + git clone ${branch} "${cfg.remote}" "${cfg.path}" + fi + ${fetcher} + gitwatch ${getvar "-r" "remote"} ${branch} ${cfg.path} + ''; + serviceConfig.User = cfg.user; + } + ); +in +{ + options.services.gitwatch = lib.mkOption { + description = '' + A set of git repositories to watch for. See + [gitwatch](https://github.com/gitwatch/gitwatch) for more. + ''; + default = { }; + example = { + my-repo = { + enable = true; + user = "user"; + path = "/home/user/watched-project"; + remote = "git@github.com:me/my-project.git"; + }; + disabled-repo = { + enable = false; + user = "user"; + path = "/home/user/disabled-project"; + remote = "git@github.com:me/my-old-project.git"; + branch = "autobranch"; + }; + }; + type = with lib.types; attrsOf (submodule { + options = { + enable = lib.mkEnableOption "watching for repo"; + path = lib.mkOption { + description = "The path to repo in local machine"; + type = str; + }; + user = lib.mkOption { + description = "The name of services's user"; + type = str; + default = "root"; + }; + remote = lib.mkOption { + description = "Optional url of remote repository"; + type = nullOr str; + default = null; + }; + branch = lib.mkOption { + description = "Optional branch in remote repository"; + type = nullOr str; + default = null; + }; + }; + }); + }; + config.systemd.services = + lib.mapAttrs' mkSystemdService config.services.gitwatch; +} From 7ef75862f8c40ae00cf3e248871dd1ffadb0537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Neves=20Costa?= Date: Sun, 31 Dec 2023 11:54:05 -0300 Subject: [PATCH 70/95] Community-Enriched Project Documentation (#118) * docs: Update README.md Added references to community articles for enhanced documentation and context. * fix: Article URL Community article link fixed. * refactor: Reorganize README for Emphasis on Official Documentation Restructured README layout to prioritize the visibility of official documentation, enhancing user accessibility and emphasizing the project's reliance on authoritative information. --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c026e696..af7a1f0e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ * [systemd](#systemd) * [Other Articles](#other-articles) * [On the Gitwatch Wiki](#on-the-gitwatch-wiki) + * [Community Articles](#community-articles) + @@ -219,4 +221,9 @@ have other calls in `rc.local` after the mentioned line, because the ### On the Gitwatch Wiki -* [How to install `gitwatch` as a Debian service with `supervisord`](https://github.com/gitwatch/gitwatch/wiki/gitwatch-as-a-service-on-Debian-with-supervisord) +* [How to Install `Gitwatch` as a Debian Service With `supervisord`](https://github.com/gitwatch/gitwatch/wiki/gitwatch-as-a-service-on-Debian-with-supervisord) + +### Community Articles + +* [How To Use `Gitwatch` by Maisa Milena](https://medium.com/@maisa.milena/how-to-use-gitwatch-92c72e8ea4c4) +* [Syncing and Backing Up Your Thoughts with `Obsidian`, `Syncthing`, and `Gitwatch` by Vinícius Costa](https://viniciusnevescosta.medium.com/syncing-and-backing-up-your-thoughts-with-obsidian-syncthing-and-gitwatch-a55670b2b63f) From 3a0b520094d3b74b119cd90d09fdfc41ece5dc2f Mon Sep 17 00:00:00 2001 From: Frairlyn Camilo Roque Suarez <74797568+cheerio-pixel@users.noreply.github.com> Date: Mon, 8 Jan 2024 11:20:20 -0400 Subject: [PATCH 71/95] Escape correctly environmental variable in systemd service file (#120) --- gitwatch@.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch@.service b/gitwatch@.service index 339ae7cb..c11afafa 100644 --- a/gitwatch@.service +++ b/gitwatch@.service @@ -3,7 +3,7 @@ Description=Watch file or directory and git commit all changes. run with: system [Service] Environment="SCRIPT_ARGS=%I" -ExecStart=/usr/local/bin/gitwatch $SCRIPT_ARGS +ExecStart=/usr/local/bin/gitwatch ${SCRIPT_ARGS} ExecStop=/bin/true [Install] From 67ad3061a258ee0ad7e0cbd6335d5d3b7fa39e8a Mon Sep 17 00:00:00 2001 From: Guillaume Berche Date: Fri, 19 Jan 2024 10:00:43 +0100 Subject: [PATCH 72/95] Add new -R option triggering a git pull --rebase Fix #88 --- gitwatch.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index a8eb89af..5c90aa2e 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -36,6 +36,7 @@ # REMOTE="" +PULL_BEFORE_PUSH=0 BRANCH="" SLEEP_TIME=2 DATE_FMT="+%Y-%m-%d %H:%M:%S" @@ -65,6 +66,7 @@ shelp() { echo ' "+%Y-%m-%d %H:%M:%S"' echo " -r If given and non-empty, a 'git push' to the given " echo " is done after every commit; default is empty, i.e. no push" + echo " -R If given along with -r, a 'git pull --rebase ' is done before any push" echo " -b The branch which should be pushed automatically;" echo " - if not given, the push command used is 'git push '," echo " thus doing a default push (see git man pages for details)" @@ -133,7 +135,7 @@ is_merging () { ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line options +while getopts b:d:h:g:L:l:m:p:r:s:e:x:MR option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -150,6 +152,7 @@ while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line option m) COMMITMSG=${OPTARG} ;; M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; + R) PULL_BEFORE_PUSH=1 ;; s) SLEEP_TIME=${OPTARG} ;; x) EXCLUDE_PATTERN=${OPTARG} ;; e) EVENTS=${OPTARG} ;; @@ -293,8 +296,13 @@ if [ -n "$REMOTE" ]; then # are we pushing to a remote? PUSH_CMD="$GIT push $REMOTE $BRANCH" fi fi + if [[ $PULL_BEFORE_PUSH -eq 1 ]]; then + PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch + fi + else PUSH_CMD="" # if not remote is selected, make sure push command is empty + PULL_CMD="" # if not remote is selected, make sure pull command is empty fi # A function to reduce git diff output to the actual changed content, and insert file line numbers. @@ -389,6 +397,11 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # shellcheck disable=SC2086 $GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit + if [ -n "$PULL_CMD" ]; then + echo "Pull command is $PULL_CMD" + eval "$PULL_CMD" + fi + if [ -n "$PUSH_CMD" ]; then echo "Push command is $PUSH_CMD" eval "$PUSH_CMD" From 500305f591a30a575b04b272684f7b457af141ff Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Wed, 13 Mar 2024 16:54:47 -0500 Subject: [PATCH 73/95] bats test for new rebasing --- tests/pull-rebase.bats | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/pull-rebase.bats diff --git a/tests/pull-rebase.bats b/tests/pull-rebase.bats new file mode 100644 index 00000000..f08217a8 --- /dev/null +++ b/tests/pull-rebase.bats @@ -0,0 +1,71 @@ +#!/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 + +function pulling_and_rebasing_correctly { #@test + + # Create a file, verify that it hasn't been added yet, + # then commit and push + cd remote + + # Start up gitwatch and see if commit and push happen automatically + # after waiting two seconds + ${BATS_TEST_DIRNAME}/../gitwatch.sh -r origin -R "$testdir/local/remote" 3>- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # 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, + # commit, and push. + sleep $WAITTIME + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Create a second local + cd ../.. + mkdir local2 + cd local2 + git clone -q ../remote + cd remote + + # Add a file to new repo + sleep 1 + echo "line2" >> file2.txt + git add file2.txt + git commit -am "file 2 added" + git push + + # Change back to original repo, make a third change, then verify that + # second one got here + cd ../../local/remote + sleep 1 + echo "line3" >> file3.txt + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Verify that new file is here + sleep $WAITTIME + [ -f file2.txt ] + + # Remove testing directories + cd /tmp + rm -rf $testdir +} + From 0397e468d8a519c4eeadbdeba939cda0ee428a04 Mon Sep 17 00:00:00 2001 From: Guillaume Berche Date: Wed, 13 Mar 2024 23:01:29 +0100 Subject: [PATCH 74/95] Add new -R option triggering a git pull --rebase (#121) Fix #88 --- gitwatch.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index a8eb89af..5c90aa2e 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -36,6 +36,7 @@ # REMOTE="" +PULL_BEFORE_PUSH=0 BRANCH="" SLEEP_TIME=2 DATE_FMT="+%Y-%m-%d %H:%M:%S" @@ -65,6 +66,7 @@ shelp() { echo ' "+%Y-%m-%d %H:%M:%S"' echo " -r If given and non-empty, a 'git push' to the given " echo " is done after every commit; default is empty, i.e. no push" + echo " -R If given along with -r, a 'git pull --rebase ' is done before any push" echo " -b The branch which should be pushed automatically;" echo " - if not given, the push command used is 'git push '," echo " thus doing a default push (see git man pages for details)" @@ -133,7 +135,7 @@ is_merging () { ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line options +while getopts b:d:h:g:L:l:m:p:r:s:e:x:MR option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -150,6 +152,7 @@ while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line option m) COMMITMSG=${OPTARG} ;; M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; + R) PULL_BEFORE_PUSH=1 ;; s) SLEEP_TIME=${OPTARG} ;; x) EXCLUDE_PATTERN=${OPTARG} ;; e) EVENTS=${OPTARG} ;; @@ -293,8 +296,13 @@ if [ -n "$REMOTE" ]; then # are we pushing to a remote? PUSH_CMD="$GIT push $REMOTE $BRANCH" fi fi + if [[ $PULL_BEFORE_PUSH -eq 1 ]]; then + PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch + fi + else PUSH_CMD="" # if not remote is selected, make sure push command is empty + PULL_CMD="" # if not remote is selected, make sure pull command is empty fi # A function to reduce git diff output to the actual changed content, and insert file line numbers. @@ -389,6 +397,11 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # shellcheck disable=SC2086 $GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit + if [ -n "$PULL_CMD" ]; then + echo "Pull command is $PULL_CMD" + eval "$PULL_CMD" + fi + if [ -n "$PUSH_CMD" ]; then echo "Push command is $PUSH_CMD" eval "$PUSH_CMD" From e59bac6d84f48b92f40f5dd5af40dc30e27db070 Mon Sep 17 00:00:00 2001 From: borgstad Date: Mon, 8 Apr 2024 14:08:11 +0200 Subject: [PATCH 75/95] Update nix flake to read `gitwatch.nix` instead of `gitwatch.sh` (#124) * Update nix flake to read .nix instead of .sh * Update Readme for NixOS --- README.md | 11 ++++++----- flake.lock | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 2 +- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 flake.lock diff --git a/README.md b/README.md index af7a1f0e..d891efad 100644 --- a/README.md +++ b/README.md @@ -93,12 +93,11 @@ for Archlinux. Install it with you favorite aur helper. #### As Module -Say you add this as input `gitwatch` to your flake. Then you may want to append -field `gitwatch.modules` to your `nixosSystem` modules. Then you are able to -enable `services.gitwatch.*` service in per-repository mode. Like next: +If you add `gitwatch` to your flake, and append field `gitwatch.modules` to +your `nixosSystem` modules, then you can enable `services.gitwatch.*`: ```nix -services.gitwatch.my-repo = { +services.gitwatch. = { enable = true; path = "/home/me/my-repo"; remote = "git@github.com:me/my-repo.git"; @@ -106,7 +105,9 @@ services.gitwatch.my-repo = { }; ``` -This will make NixOS to create `systemd` service for `my-repo` repository. +This will make NixOS to create `systemd` service named `gitwatch-`. +Note that the service does not start before reboot, or alternatively if it is +started manually: `systemctl start gitwatch-`. #### As Package diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..a2414a98 --- /dev/null +++ b/flake.lock @@ -0,0 +1,58 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1712449641, + "narHash": "sha256-U9DDWMexN6o5Td2DznEgguh8TRIUnIl9levmit43GcI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "600b15aea1b36eeb43833a50b0e96579147099ff", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index ee2c85ea..e1585044 100644 --- a/flake.nix +++ b/flake.nix @@ -10,7 +10,7 @@ in { packages = rec { - gitwatch = pkgs.callPackage ./gitwatch.sh { }; + gitwatch = pkgs.callPackage ./gitwatch.nix { }; default = gitwatch; }; }); From 09b0f1ae0a585e4870c464b1ff698f311fcdbe27 Mon Sep 17 00:00:00 2001 From: Kieran Collienne <20397647+kollienne@users.noreply.github.com> Date: Fri, 12 Apr 2024 05:00:03 -0700 Subject: [PATCH 76/95] fix sending all script arguments as arg 1 (#125) Co-authored-by: Kieran Collienne --- gitwatch@.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch@.service b/gitwatch@.service index c11afafa..b8450524 100644 --- a/gitwatch@.service +++ b/gitwatch@.service @@ -3,7 +3,7 @@ Description=Watch file or directory and git commit all changes. run with: system [Service] Environment="SCRIPT_ARGS=%I" -ExecStart=/usr/local/bin/gitwatch ${SCRIPT_ARGS} +ExecStart=/usr/bin/bash -c "/usr/local/bin/gitwatch ${SCRIPT_ARGS}" ExecStop=/bin/true [Install] From e17d9cf02b670beba327b096a5e9d5d63fc822bb Mon Sep 17 00:00:00 2001 From: Yury Shvedov Date: Mon, 20 May 2024 15:26:02 +0300 Subject: [PATCH 77/95] [nix] Add `wantedBy` to systemd service (#126) This field needed to properly enable service by configuration. --- module.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/module.nix b/module.nix index 1f3eeb58..91c76f30 100644 --- a/module.nix +++ b/module.nix @@ -19,6 +19,7 @@ let { inherit (cfg) enable; after = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; description = "gitwatch for ${name}"; path = with pkgs; [ gitwatch git openssh ]; script = '' From 4b0b31085942ba808bd72fbb7a75ac7d5353ebfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Mei=C3=9Fner?= <936176+t-nil@users.noreply.github.com> Date: Mon, 20 May 2024 14:41:30 +0200 Subject: [PATCH 78/95] Fix systemd-escape invocation to parse flags correctly (#127) Without `--` systemd-escape tries to parse gitwatch flags as its own flags (try with `-s` as first gitwatch flag). --- gitwatch@.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch@.service b/gitwatch@.service index b8450524..00168253 100644 --- a/gitwatch@.service +++ b/gitwatch@.service @@ -1,5 +1,5 @@ [Unit] -Description=Watch file or directory and git commit all changes. run with: systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service +Description=Watch file or directory and git commit all changes. run with: systemctl --user --now enable gitwatch@$(systemd-escape -- "'-r url/to/repository' /path/to/folder").service [Service] Environment="SCRIPT_ARGS=%I" From 338430adf05d91d430447684e30778120b6efea2 Mon Sep 17 00:00:00 2001 From: Yury Shvedov Date: Wed, 11 Jun 2025 14:59:50 +0300 Subject: [PATCH 79/95] readme: NixOS 24.11 released with this package (#129) --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d891efad..e8b3d848 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ * [Update](#update) * [bpkg](#bpkg) * [Archlinux](#archlinux) - * [NixOs](#nixos) + * [NixOS](#nixos) * [As Module](#as-module) * [As Package](#as-package) * [Requirements](#requirements) @@ -89,12 +89,15 @@ the command below. You may need to invoke `bpkg` with `sudo` when using the There is an [AUR](https://aur.archlinux.org/packages/gitwatch-git/) package for Archlinux. Install it with you favorite aur helper. -### NixOs +### NixOS + +Starting from NixOS 24.11 this package available in mainline. Additionally, you +can use receipts from this repository. #### As Module -If you add `gitwatch` to your flake, and append field `gitwatch.modules` to -your `nixosSystem` modules, then you can enable `services.gitwatch.*`: +Each watching path should be described in _submodule_ `services.gitwatch.*` like +next: ```nix services.gitwatch. = { @@ -106,14 +109,11 @@ services.gitwatch. = { ``` This will make NixOS to create `systemd` service named `gitwatch-`. -Note that the service does not start before reboot, or alternatively if it is -started manually: `systemctl start gitwatch-`. +More details you can see at `man configuration.nix`. #### As Package -You can to play around with nix package inside this repository. Call `nix run` -in this repository to run `gitwatch` script or `nix shell` to enter shell with -`gitwatch` command available. +The `gitwatch` script available as package in _nixpkgs_; ## Requirements From 3969bbf8cbd3f515ad42822538f5a244e4a970a5 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (limbo)" Date: Mon, 23 Jun 2025 10:57:58 -0500 Subject: [PATCH 80/95] spelling error fix (related to PR 132) --- gitwatch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index 5c90aa2e..e7ad2c5b 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -385,7 +385,7 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do } STATUS=$($GIT status -s) if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. - # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word splitted + # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word split # shellcheck disable=SC2086 if [ "$SKIP_IF_MERGING" -eq 1 ] && is_merging; then From b35891454d858ffec82552a8c7d82e24d616f510 Mon Sep 17 00:00:00 2001 From: MDW Date: Mon, 23 Jun 2025 18:05:05 +0200 Subject: [PATCH 81/95] Add pre-commit setup (#132) Co-authored-by: MDW --- .github/workflows/pre-commit.yml | 53 +++++++++++++++++++ .pre-commit-config.yaml | 87 ++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 00000000..abaea9ef --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,53 @@ +--- +name: pre-commit +on: + pull_request: + push: +jobs: + pre-commit: + runs-on: ubuntu-latest + env: + RAW_LOG: pre-commit.log + SKIP: no-commit-to-branch + steps: + - run: sudo apt-get update && sudo apt-get install cppcheck + if: false + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + if: false + with: + cache: pip + python-version: 3.12.1 + - run: python -m pip install pre-commit regex + - uses: actions/cache/restore@v4 + with: + path: ~/.cache/pre-commit/ + key: + pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') + }} + - name: Run pre-commit hooks + run: | + set -o pipefail + pre-commit gc + pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG} + #- name: Convert Raw Log to Annotations + # uses: mdeweerd/logToCheckStyle@v2025.1.1 + # if: ${{ failure() }} + # with: + # in: ${{ env.RAW_LOG }} + - uses: actions/cache/save@v4 + if: ${{ ! cancelled() }} + with: + path: ~/.cache/pre-commit/ + key: + pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') + }} + - name: Provide log as artifact + uses: actions/upload-artifact@v4 + if: ${{ ! cancelled() }} + with: + name: precommit-logs + path: | + ${{ env.RAW_LOG }} + ${{ env.CS_XML }} + retention-days: 2 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..63203760 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,87 @@ +--- +files: ^(.*\.(py|json|md|sh|yaml|yml|cfg|txt))$ +exclude: ^(\.[^/]*cache/.*|demo/.*|debug/.*)$ +repos: + # Disable because this does not work for the main author. + # - repo: https://github.com/pre-commit/mirrors-prettier + # rev: "v2.7.1" + # stages: [manual] + # hooks: + # - id: prettier + - repo: https://github.com/executablebooks/mdformat + # Do this before other tools "fixing" the line endings + rev: 0.7.22 + hooks: + - id: mdformat + name: Format Markdown + entry: mdformat # Executable to run, with fixed options + language: python + types: [markdown] + args: [--wrap, "75", --number] + # files: ^HomeAssistant.md$ + additional_dependencies: + - mdformat-toc + - mdformat-beautysh + - mdformat-config + - mdformat-gfm + - setuptools + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: no-commit-to-branch + args: [--branch, main] + - id: check-yaml + # Exclude because of bug in checker + exclude: ^(docker-compose\.yml|.*/release-drafter\.yml)$ + - id: debug-statements + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-json + - id: mixed-line-ending + - id: check-builtin-literals + - id: check-ast + - id: check-merge-conflict + - id: check-executables-have-shebangs + - id: check-shebang-scripts-are-executable + - id: check-docstring-first + - id: fix-byte-order-marker + - id: check-case-conflict + - id: pretty-format-json + exclude: ^(.vscode|.devcontainer) + args: + # order of keys in manifest.json is "special" + - --no-sort-keys + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + - repo: https://github.com/adrienverge/yamllint.git + rev: v1.37.1 + hooks: + - id: yamllint + args: + - --no-warnings + - -d + - "{extends: relaxed, rules: {line-length: {max: 90}}}" + - repo: https://github.com/codespell-project/codespell + rev: v2.4.1 + hooks: + - id: codespell + # exclude: (\.md|apps.yaml|translations/.*.yaml)$ + - repo: https://github.com/IamTheFij/docker-pre-commit + rev: v3.0.1 + hooks: + - id: docker-compose-check + - repo: https://github.com/lovesegfault/beautysh.git + rev: v6.2.1 + hooks: + - id: beautysh + args: ["-i", "2"] + additional_dependencies: + - setuptools + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.10.0.1 + hooks: + - id: shellcheck + files: ^[^\.].*\.sh$ + args: [--shell, bash] From 7256743cd462f00165fe28d7135f84e543b3c1e8 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (limbo)" Date: Mon, 23 Jun 2025 11:15:46 -0500 Subject: [PATCH 82/95] lots of formatting differences after adding pre-commit verifications --- .github/workflows/gitwatch.yml | 24 ++-- README.md | 211 +++++++++++++++++---------------- gitwatch.sh | 6 +- package.json | 4 +- 4 files changed, 128 insertions(+), 117 deletions(-) diff --git a/.github/workflows/gitwatch.yml b/.github/workflows/gitwatch.yml index 3c2d9a80..98b667a9 100644 --- a/.github/workflows/gitwatch.yml +++ b/.github/workflows/gitwatch.yml @@ -29,18 +29,18 @@ jobs: # For the time being, I'm giving up on Super Linter. It's a great tool, # but it's currently broken, and it's taking more time to make it work than # it's worth. - # - name: Lint Code Base - # uses: github/super-linter@v4 - # env: - # DEFAULT_BRANCH: master - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # # Linter is having errors at symbolic link, appears to be - # # this bug: - # # https://github.com/github/super-linter/issues/1400 - # # https://github.com/kucherenko/jscpd/issues/481 - # # Test files are bats files, which don't follow bash standards - # # FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* - # # FILTER_REGEX_EXCLUDE: tests\.* + # - name: Lint Code Base + # uses: github/super-linter@v4 + # env: + # DEFAULT_BRANCH: master + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # # Linter is having errors at symbolic link, appears to be + # # this bug: + # # https://github.com/github/super-linter/issues/1400 + # # https://github.com/kucherenko/jscpd/issues/481 + # # Test files are bats files, which don't follow bash standards + # # FILTER_REGEX_EXCLUDE: \.github/linters/\.markdown-lint.yml | tests\.* + # # FILTER_REGEX_EXCLUDE: tests\.* #------------------------------------------------------------------------- bats: diff --git a/README.md b/README.md index e8b3d848..ab4a605c 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,38 @@ + + + - * [gitwatch](#gitwatch) - * [What to use it for?](#what-to-use-it-for) - * [Installation](#installation) - * [From Source](#from-source) - * [Update](#update) - * [bpkg](#bpkg) - * [Archlinux](#archlinux) - * [NixOS](#nixos) - * [As Module](#as-module) - * [As Package](#as-package) - * [Requirements](#requirements) - * [Notes for Mac](#notes-for-mac) - * [What it does](#what-it-does) - * [Usage](#usage) - * [Starting on Boot](#starting-on-boot) - * [SysVInit](#sysvinit) - * [systemd](#systemd) - * [Other Articles](#other-articles) - * [On the Gitwatch Wiki](#on-the-gitwatch-wiki) - * [Community Articles](#community-articles) +- [gitwatch](#gitwatch) + - [What to use it for?](#what-to-use-it-for) + - [Installation](#installation) + - [From Source](#from-source) + - [Update](#update) + - [bpkg](#bpkg) + - [Archlinux](#archlinux) + - [NixOS](#nixos) + - [As Module](#as-module) + - [As Package](#as-package) + - [Requirements](#requirements) + - [Notes for Mac](#notes-for-mac) + - [What it does](#what-it-does) + - [Usage](#usage) + - [Starting on Boot](#starting-on-boot) + - [SysVInit](#sysvinit) + - [systemd](#systemd) + - [Other Articles](#other-articles) + - [On the Gitwatch Wiki](#on-the-gitwatch-wiki) + - [Community Articles](#community-articles) + + # gitwatch A bash script to watch a file or folder and commit changes to a git repo @@ -36,19 +41,19 @@ A bash script to watch a file or folder and commit changes to a git repo That's really up to you, but here are some examples: -* **config files**: some programs auto-write their config files, without - waiting for you to click an 'Apply' button; or even if there is such - a button, most programs offer you no way of going back to an earlier - version of your settings. If you commit your config file(s) to a git repo, - you can track changes and go back to older versions. This script makes it - convenient, to have all changes recorded automatically. -* **document files**: if you use an editor that does not have built-in git +- **config files**: some programs auto-write their config files, without + waiting for you to click an 'Apply' button; or even if there is such a + button, most programs offer you no way of going back to an earlier + version of your settings. If you commit your config file(s) to a git + repo, you can track changes and go back to older versions. This script + makes it convenient, to have all changes recorded automatically. +- **document files**: if you use an editor that does not have built-in git support (or maybe if you don't like the git support it has), you can use - gitwatch to automatically commit your files when you save them, or combine - it with the editor's auto-save feature to fully automatically and regularly - track your changes -* *more stuff!* If you have any other uses, or can think of ones, please let - us know, and we can add them to this list! + gitwatch to automatically commit your files when you save them, or + combine it with the editor's auto-save feature to fully automatically and + regularly track your changes +- _more stuff!_ If you have any other uses, or can think of ones, please + let us know, and we can add them to this list! ## Installation @@ -56,9 +61,9 @@ That's really up to you, but here are some examples: ### From Source -`gitwatch` can be installed from source by simply cloning the repository and -putting the shell script into your `$PATH`. The commands below will do that -for you if `/usr/local/bin` is in your `$PATH`. You may need to invoke +`gitwatch` can be installed from source by simply cloning the repository +and putting the shell script into your `$PATH`. The commands below will do +that for you if `/usr/local/bin` is in your `$PATH`. You may need to invoke `install` with `sudo`. ```sh @@ -70,8 +75,8 @@ cd gitwatch #### Update If you installed `gitwatch` from source, you can update it by following the -exact same steps (or `git pull` rather than clone if you kept the repository -around). +exact same steps (or `git pull` rather than clone if you kept the +repository around). ### bpkg @@ -91,13 +96,13 @@ for Archlinux. Install it with you favorite aur helper. ### NixOS -Starting from NixOS 24.11 this package available in mainline. Additionally, you -can use receipts from this repository. +Starting from NixOS 24.11 this package available in mainline. Additionally, +you can use receipts from this repository. #### As Module -Each watching path should be described in _submodule_ `services.gitwatch.*` like -next: +Each watching path should be described in _submodule_ `services.gitwatch.*` +like next: ```nix services.gitwatch. = { @@ -108,8 +113,9 @@ services.gitwatch. = { }; ``` -This will make NixOS to create `systemd` service named `gitwatch-`. -More details you can see at `man configuration.nix`. +This will make NixOS to create `systemd` service named +`gitwatch-`. More details you can see at +`man configuration.nix`. #### As Package @@ -119,8 +125,10 @@ The `gitwatch` script available as package in _nixpkgs_; To run this script, you must have installed and globally available: -* `git` ([git/git](https://github.com/git/git) | [git-scm](http://www.git-scm.com)) -* `inotifywait` (part of **[inotify-tools](https://github.com/rvoicilas/inotify-tools)**) +- `git` ([git/git](https://github.com/git/git) | + [git-scm](http://www.git-scm.com)) +- `inotifywait` (part of + **[inotify-tools](https://github.com/rvoicilas/inotify-tools)**) ### Notes for Mac @@ -133,98 +141,99 @@ brew install coreutils ## What it does -When you start the script, it prepares some variables and checks if the file -or directory given as input really exists. - -Then it goes into the main loop (which will run forever, until the script is -forcefully stopped/killed), which will: - -* watch for changes to the file/directory using `inotifywait` (`inotifywait` - will block until something happens) -* wait 2 seconds -* case file: - * `cd` into the directory containing the file (because `git` likes to operate locally) - * `git add ` - * `git commit -m "Scripted auto-commit on change ()"` -* case directory: - * `cd` into the directory (because `git` likes to operate locally) - * `git add --all .` - * `git commit -m "Scripted auto-commit on change ()"` -* if a remote is defined (with `-r`) do a push after the commit (a specific +When you start the script, it prepares some variables and checks if the +file or directory given as input really exists. + +Then it goes into the main loop (which will run forever, until the script +is forcefully stopped/killed), which will: + +- watch for changes to the file/directory using `inotifywait` + (`inotifywait` will block until something happens) +- wait 2 seconds +- case file: + - `cd` into the directory containing the file (because `git` likes to + operate locally) + - `git add ` + - `git commit -m "Scripted auto-commit on change ()"` +- case directory: + - `cd` into the directory (because `git` likes to operate locally) + - `git add --all .` + - `git commit -m "Scripted auto-commit on change ()"` +- if a remote is defined (with `-r`) do a push after the commit (a specific branch can be selected with `-b`) Notes: -* the waiting period of 2 sec is added to allow for several changes to be - written out completely before committing; depending on how fast the script - is executed, this might otherwise cause race conditions when watching - a folder -* currently, folders are always watched recursively +- the waiting period of 2 sec is added to allow for several changes to be + written out completely before committing; depending on how fast the + script is executed, this might otherwise cause race conditions when + watching a folder +- currently, folders are always watched recursively ## Usage `gitwatch.sh [-r [-b ]] ` -It is expected that the watched file/directory are already in a git repository -(the script will not create a repository). If a folder is being watched, this -will be watched fully recursively; this also means that all files and -sub-folders added and removed from the directory will always be added and -removed in the next commit. The `.git` folder will be excluded from the -`inotifywait` call so changes to it will not cause unnecessary triggering of -the script. +It is expected that the watched file/directory are already in a git +repository (the script will not create a repository). If a folder is being +watched, this will be watched fully recursively; this also means that all +files and sub-folders added and removed from the directory will always be +added and removed in the next commit. The `.git` folder will be excluded +from the `inotifywait` call so changes to it will not cause unnecessary +triggering of the script. -If you have any large files in your repository that are changing frequently, -you might wish to ignore them with a `.gitignore` file. +If you have any large files in your repository that are changing +frequently, you might wish to ignore them with a `.gitignore` file. ### Starting on Boot -If you want to have the script auto-started upon boot, the method to do this -depends on your operating system and distribution. If you have a GUI dialog to -set up startup launches, you might want to use that, so you can more easily -find and change the startup script calls later on. +If you want to have the script auto-started upon boot, the method to do +this depends on your operating system and distribution. If you have a GUI +dialog to set up startup launches, you might want to use that, so you can +more easily find and change the startup script calls later on. Please also note that if either of the paths involved (script or target) -contains spaces or special characters, you need to escape them accordingly; if -you don't know how to do that, the internet will help you, or feel free to ask -here or contact me directly. +contains spaces or special characters, you need to escape them accordingly; +if you don't know how to do that, the internet will help you, or feel free +to ask here or contact me directly. #### SysVInit -A central place to put startup scripts on Linux is generally `/etc/rc.local` -(to my knowledge; only tested and confirmed on Ubuntu). This file, if it has -the +x bit, will be executed upon startup, **by the root user account**. If -you want to start `gitwatch` from `rc.local`, the recommended way to call it -is: +A central place to put startup scripts on Linux is generally +`/etc/rc.local` (to my knowledge; only tested and confirmed on Ubuntu). +This file, if it has the +x bit, will be executed upon startup, **by the +root user account**. If you want to start `gitwatch` from `rc.local`, the +recommended way to call it is: + `su -c "/absolute/path/to/script/gitwatch.sh /absolute/path/to/watched/file/or/folder" -l &` + The `` bit should be replaced with your username or that of any other (non-root) user account; it only needs write-access to the git repository of the file/folder you want to watch. The ampersand (`&`) at the -end sends the launched process into the background (this is important if you -have other calls in `rc.local` after the mentioned line, because the +end sends the launched process into the background (this is important if +you have other calls in `rc.local` after the mentioned line, because the `gitwatch` call does not usually return). #### systemd -* If installed to a path other than `/usr/local/bin/gitwatch`, modify +- If installed to a path other than `/usr/local/bin/gitwatch`, modify `gitwatch@.service` to suit -* Create dir if it does not exist and copy systemd service file with `mkdir -p - "$HOME/.config/systemd/user" && cp gitwatch@.service - $HOME/.config/systemd/user` -* Start and enable the service for a given path by running `systemctl --user - --now enable gitwatch@$(systemd-escape "'-r url/to/repository' - /path/to/folder").service` +- Create dir if it does not exist and copy systemd service file with + `mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service $HOME/.config/systemd/user` +- Start and enable the service for a given path by running + `systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service` ## Other Articles ### On the Gitwatch Wiki -* [How to Install `Gitwatch` as a Debian Service With `supervisord`](https://github.com/gitwatch/gitwatch/wiki/gitwatch-as-a-service-on-Debian-with-supervisord) +- [How to Install `Gitwatch` as a Debian Service With `supervisord`](https://github.com/gitwatch/gitwatch/wiki/gitwatch-as-a-service-on-Debian-with-supervisord) ### Community Articles -* [How To Use `Gitwatch` by Maisa Milena](https://medium.com/@maisa.milena/how-to-use-gitwatch-92c72e8ea4c4) -* [Syncing and Backing Up Your Thoughts with `Obsidian`, `Syncthing`, and `Gitwatch` by Vinícius Costa](https://viniciusnevescosta.medium.com/syncing-and-backing-up-your-thoughts-with-obsidian-syncthing-and-gitwatch-a55670b2b63f) +- [How To Use `Gitwatch` by Maisa Milena](https://medium.com/@maisa.milena/how-to-use-gitwatch-92c72e8ea4c4) +- [Syncing and Backing Up Your Thoughts with `Obsidian`, `Syncthing`, and `Gitwatch` by Vinícius Costa](https://viniciusnevescosta.medium.com/syncing-and-backing-up-your-thoughts-with-obsidian-syncthing-and-gitwatch-a55670b2b63f) diff --git a/gitwatch.sh b/gitwatch.sh index e7ad2c5b..9cc79ffb 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -228,9 +228,9 @@ 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$)'" + EXCLUDE_OPTS="'(\.git/|\.git$)'" else - EXCLUDE_OPTS="'(\.git/|\.git$|$EXCLUDE_PATTERN)'" + EXCLUDE_OPTS="'(\.git/|\.git$|$EXCLUDE_PATTERN)'" fi # construct inotifywait-commandline @@ -297,7 +297,7 @@ if [ -n "$REMOTE" ]; then # are we pushing to a remote? fi fi if [[ $PULL_BEFORE_PUSH -eq 1 ]]; then - PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch + PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch fi else diff --git a/package.json b/package.json index 097b4a2a..845d76e1 100644 --- a/package.json +++ b/package.json @@ -4,5 +4,7 @@ "description": "A script to watch a file or folder and automatically commit changes to a Git repo", "global": "1", "install": "install -b gitwatch.sh ${PREFIX:-/usr/local}/bin/gitwatch", - "scripts": [ "gitwatch.sh" ] + "scripts": [ + "gitwatch.sh" + ] } From fc383f29b302a79fc6b24a941bcde83642665057 Mon Sep 17 00:00:00 2001 From: "Dave Musicant (limbo)" Date: Mon, 23 Jun 2025 11:30:44 -0500 Subject: [PATCH 83/95] Added in a number of shellcheck disables. These are likely things that should be fixed, but am disabling for now to keep things running. --- gitwatch.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gitwatch.sh b/gitwatch.sh index 9cc79ffb..1de1c22e 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -227,6 +227,7 @@ 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 + # shellcheck disable=SC2086 if [ -z $EXCLUDE_PATTERN ]; then EXCLUDE_OPTS="'(\.git/|\.git$)'" else @@ -235,9 +236,11 @@ if [ -d "$1" ]; then # if the target is a directory # construct inotifywait-commandline if [ "$(uname)" != "Darwin" ]; then + # shellcheck disable=SC2206 INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") else # still need to fix EVENTS since it wants them listed one-by-one + # shellcheck disable=SC2206 INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") fi GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index @@ -342,6 +345,8 @@ diff-lines() { # 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 +# Would be great to fix the ignored issue below; ignoring it for now. +# shellcheck disable=SC2294 eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # is there already a timeout process running? if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then @@ -393,6 +398,7 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do exit 0 fi + # shellcheck disable=SC2086 $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 From 5f925e1f78cef169302ccbd751deaca28fa2d571 Mon Sep 17 00:00:00 2001 From: JQ Date: Tue, 19 Aug 2025 08:42:50 -0400 Subject: [PATCH 84/95] Add option for commit message determined by shell command (#134) * initial commit * init commitcmd test * change testing branch name to main from master * add commitcmd tests * revert master to main change in tests --- gitwatch.sh | 23 +++++++++++-- tests/commitcmd.bats | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 tests/commitcmd.bats diff --git a/gitwatch.sh b/gitwatch.sh index 1de1c22e..a62a0ea4 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -41,6 +41,8 @@ BRANCH="" SLEEP_TIME=2 DATE_FMT="+%Y-%m-%d %H:%M:%S" COMMITMSG="Scripted auto-commit on change (%d) by gitwatch.sh" +COMMITCMD="" +PASSDIFFS=0 LISTCHANGES=-1 LISTCHANGES_COLOR="--color=always" GIT_DIR="" @@ -88,6 +90,11 @@ shelp() { echo " (unless the 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"' + echo " -c The command to be run to generate a commit message. If empty," + echo " defaults to the standard commit message. This option overrides -m," + echo " -d, and -l." + echo " -C Pass list of diffed files to via pipe. Has no effect if" + echo " -c is not given." echo " -e Events passed to inotifywait to watch (defaults to " echo " '$EVENTS')" echo " (useful when using inotify-win, e.g. -e modify,delete,move)" @@ -128,14 +135,14 @@ is_command() { hash "$1" 2> /dev/null } -# Test whether or not current git directory has ongoign merge +# Test whether or not current git directory has ongoing merge is_merging () { [ -f "$(git rev-parse --git-dir)"/MERGE_HEAD ] } ############################################################################### -while getopts b:d:h:g:L:l:m:p:r:s:e:x:MR option; do # Process command line options +while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MR option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -150,6 +157,8 @@ while getopts b:d:h:g:L:l:m:p:r:s:e:x:MR option; do # Process command line optio LISTCHANGES_COLOR="" ;; m) COMMITMSG=${OPTARG} ;; + c) COMMITCMD=${OPTARG} ;; + C) PASSDIFFS=1 ;; M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; R) PULL_BEFORE_PUSH=1 ;; @@ -383,6 +392,16 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do fi fi + if [ -n "$COMMITCMD" ]; then + if [ "$PASSDIFFS" -eq 1 ]; then + # If -C is set, pass the list of diffed files to the commit command + # Unsure whether or not I should check if the command fails + FORMATTED_COMMITMSG="$($COMMITCMD < <($GIT diff --name-only))" + else + FORMATTED_COMMITMSG="$($COMMITCMD)" + fi + fi + # CD into right dir cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." diff --git a/tests/commitcmd.bats b/tests/commitcmd.bats new file mode 100644 index 00000000..6db8a91b --- /dev/null +++ b/tests/commitcmd.bats @@ -0,0 +1,82 @@ +#!/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 + +function commit_command_single { #@test + + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # Create a file, verify that it hasn't been added yet, then commit and push + 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, commit, and push. + sleep $WAITTIME + + run git log -1 --oneline + [[ $output == *$(uname) ]] +} + +function commit_command_format { #@test + # tests nested commit command + + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "echo '$(uname) is the uname of this device, the time is $(date)' " "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # Create a file, verify that it hasn't been added yet, then commit and push + 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, commit, and push. + sleep $WAITTIME + + run git log -1 --oneline + [[ $output == *$(uname)* ]] + [[ $output == *$(date +%Y)* ]] +} + +function commit_command_overwrite { #@test + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" -l 123 -L 0 -d "+%Y" "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # Create a file, verify that it hasn't been added yet, then commit and push + 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, commit, and push. + sleep $WAITTIME + + run git log -1 --oneline + [[ $output == *$(uname)* ]] +} + From dfdd3bdf271a6e7f2476b93314642fac86277461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Catarino?= Date: Tue, 30 Sep 2025 22:10:23 +0200 Subject: [PATCH 85/95] feat: add message in nix module (#137) update flake --- README.md | 1 + flake.lock | 12 ++++++------ module.nix | 8 +++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ab4a605c..9a69adc0 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ services.gitwatch. = { path = "/home/me/my-repo"; remote = "git@github.com:me/my-repo.git"; user = "me"; + message = "Auto-commit by gitwatch on %d"; }; ``` diff --git a/flake.lock b/flake.lock index a2414a98..0968cb72 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -19,11 +19,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1712449641, - "narHash": "sha256-U9DDWMexN6o5Td2DznEgguh8TRIUnIl9levmit43GcI=", + "lastModified": 1759070547, + "narHash": "sha256-JVZl8NaVRYb0+381nl7LvPE+A774/dRpif01FKLrYFQ=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "600b15aea1b36eeb43833a50b0e96579147099ff", + "rev": "647e5c14cbd5067f44ac86b74f014962df460840", "type": "github" }, "original": { diff --git a/module.nix b/module.nix index 91c76f30..7e76fe35 100644 --- a/module.nix +++ b/module.nix @@ -27,7 +27,7 @@ let git clone ${branch} "${cfg.remote}" "${cfg.path}" fi ${fetcher} - gitwatch ${getvar "-r" "remote"} ${branch} ${cfg.path} + gitwatch ${getvar "-r" "remote"} ${getvar "-m" "message"} ${branch} ${cfg.path} ''; serviceConfig.User = cfg.user; } @@ -46,6 +46,7 @@ in user = "user"; path = "/home/user/watched-project"; remote = "git@github.com:me/my-project.git"; + message = "Auto-commit by gitwatch on %d"; }; disabled-repo = { enable = false; @@ -72,6 +73,11 @@ in type = nullOr str; default = null; }; + message = lib.mkOption { + description = "Optional message to use in as commit message."; + type = nullOr str; + default = null; + }; branch = lib.mkOption { description = "Optional branch in remote repository"; type = nullOr str; From 22da84a823572cc0add2054f0f8f7175daa7def1 Mon Sep 17 00:00:00 2001 From: meek2100 <34908880+meek2100@users.noreply.github.com> Date: Tue, 21 Oct 2025 11:11:29 -0700 Subject: [PATCH 86/95] Add a verbose option to help with informational items and debugging. (#139) --- gitwatch.sh | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index a62a0ea4..96be2af3 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -2,12 +2,13 @@ # # gitwatch - watch file or directory and git commit all changes as they happen # -# Copyright (C) 2013-2018 Patrick Lehner +# Copyright (C) 2013-2025 Patrick Lehner # with modifications and contributions by: # - Matthew McGowan # - Dominik D. Geyer # - Phil Thompson # - Dave Musicant +# - Darin Theurer # ############################################################################# # This program is free software: you can redistribute it and/or modify @@ -47,6 +48,7 @@ LISTCHANGES=-1 LISTCHANGES_COLOR="--color=always" GIT_DIR="" SKIP_IF_MERGING=0 +VERBOSE=0 # Print a message about how to use this script shelp() { @@ -54,7 +56,7 @@ shelp() { echo "" echo "Usage:" echo "${0##*/} [-s ] [-d ] [-r [-b ]]" - echo " [-m ] [-l|-L ] [-x ] [-M] " + echo " [-m ] [-l|-L ] [-x ] [-M] [-v] " 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" @@ -100,6 +102,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 " -v Run in verbose mode for debugging. Enables informational messages and command tracing (set -x)." echo " -x Pattern to exclude from inotifywait" echo "" echo "As indicated, several conditions are only checked once at launch of the" @@ -122,6 +125,13 @@ stderr() { echo "$@" >&2 } +# print all arguments to stdout if in verbose mode +verbose_echo() { + if [ "$VERBOSE" -eq 1 ]; then + echo "$@" + fi +} + # clean up at end of program, killing the remaining sleep process if it still exists cleanup() { if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then @@ -142,7 +152,7 @@ is_merging () { ############################################################################### -while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MR option; do # Process command line options +while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MRv option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -163,6 +173,10 @@ while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MR option; do # Process command line o p | r) REMOTE=${OPTARG} ;; R) PULL_BEFORE_PUSH=1 ;; s) SLEEP_TIME=${OPTARG} ;; + v) + VERBOSE=1 + set -x + ;; x) EXCLUDE_PATTERN=${OPTARG} ;; e) EVENTS=${OPTARG} ;; *) @@ -233,6 +247,7 @@ else fi if [ -d "$1" ]; then # if the target is a directory + verbose_echo "Target is a directory." TARGETDIR=$(sed -e "s/\/*$//" <<< "$IN") # dir to CD into before using git commands: trim trailing slash, if any @@ -256,6 +271,7 @@ if [ -d "$1" ]; then # if the target is a directory GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure elif [ -f "$1" ]; then # if the target is a single file + verbose_echo "Target is a file." TARGETDIR=$(dirname "$IN") # dir to CD into before using git commands: extract from file name # construct inotifywait-commandline @@ -297,22 +313,28 @@ cd "$TARGETDIR" || { } if [ -n "$REMOTE" ]; then # are we pushing to a remote? + verbose_echo "Push remote selected: $REMOTE" if [ -z "$BRANCH" ]; then # Do we have a branch set to push to ? + verbose_echo "No push branch selected, using default." PUSH_CMD="$GIT push $REMOTE" # Branch not set, push to remote without a branch else # check if we are on a detached HEAD if HEADREF=$($GIT symbolic-ref HEAD 2> /dev/null); then # HEAD is not detached + verbose_echo "Push branch selected: $BRANCH, current branch: ${HEADREF#refs/heads/}" #PUSH_CMD="$GIT push $REMOTE $(sed "s_^refs/heads/__" <<< "$HEADREF"):$BRANCH" PUSH_CMD="$GIT push $REMOTE ${HEADREF#refs/heads/}:$BRANCH" else # HEAD is detached + verbose_echo "Push branch selected: $BRANCH, HEAD is detached." PUSH_CMD="$GIT push $REMOTE $BRANCH" fi fi if [[ $PULL_BEFORE_PUSH -eq 1 ]]; then + verbose_echo "Pull before push is enabled." PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch fi else + verbose_echo "No push remote selected." PUSH_CMD="" # if not remote is selected, make sure push command is empty PULL_CMD="" # if not remote is selected, make sure pull command is empty fi @@ -357,6 +379,7 @@ diff-lines() { # Would be great to fix the ignored issue below; ignoring it for now. # shellcheck disable=SC2294 eval "$INW" "${INW_ARGS[@]}" | while read -r line; do + verbose_echo "Change detected: $line" # is there already a timeout process running? if [[ -n $SLEEP_PID ]] && kill -0 "$SLEEP_PID" &> /dev/null; then # kill it and wait for completion @@ -409,28 +432,35 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do } STATUS=$($GIT status -s) if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. + verbose_echo "Tracked changes detected." # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word split # shellcheck disable=SC2086 if [ "$SKIP_IF_MERGING" -eq 1 ] && is_merging; then - echo "Skipping commit - repo is merging" + verbose_echo "Skipping commit - repo is merging" exit 0 fi # shellcheck disable=SC2086 $GIT add $GIT_ADD_ARGS # add file(s) to index + verbose_echo "Running git add with arguments: $GIT_ADD_ARGS" # shellcheck disable=SC2086 $GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit + verbose_echo "Running git commit with arguments: $GIT_COMMIT_ARGS -m\"$FORMATTED_COMMITMSG\"" if [ -n "$PULL_CMD" ]; then echo "Pull command is $PULL_CMD" + verbose_echo "Executing pull command: $PULL_CMD" eval "$PULL_CMD" fi if [ -n "$PUSH_CMD" ]; then echo "Push command is $PUSH_CMD" + verbose_echo "Executing push command: $PUSH_CMD" eval "$PUSH_CMD" fi + else + verbose_echo "No tracked changes detected." fi ) & # and send into background From edac37fbc5cb30c3bcb1a085856373d1338646be Mon Sep 17 00:00:00 2001 From: meek2100 <34908880+meek2100@users.noreply.github.com> Date: Mon, 27 Oct 2025 11:46:20 -0700 Subject: [PATCH 87/95] Add support for committing pending changes on first run with support for spaces in PATH for both Linux and MacOS. (#144) * Add support for committing pending changes on first run. * Add support for paths with spaces. --- gitwatch.sh | 164 ++++++++++++++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 76 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 96be2af3..c73a9a96 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -49,6 +49,7 @@ LISTCHANGES_COLOR="--color=always" GIT_DIR="" SKIP_IF_MERGING=0 VERBOSE=0 +COMMIT_ON_START=0 # Print a message about how to use this script shelp() { @@ -56,7 +57,7 @@ shelp() { echo "" echo "Usage:" echo "${0##*/} [-s ] [-d ] [-r [-b ]]" - echo " [-m ] [-l|-L ] [-x ] [-M] [-v] " + echo " [-m ] [-l|-L ] [-x ] [-M] [-v] [-f] " 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" @@ -101,6 +102,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 " -f Commit any pending changes on startup before watching." echo " -M Prevent commits when there is an ongoing merge in the repo" echo " -v Run in verbose mode for debugging. Enables informational messages and command tracing (set -x)." echo " -x Pattern to exclude from inotifywait" @@ -152,7 +154,7 @@ is_merging () { ############################################################################### -while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MRv option; do # Process command line options +while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MRvf option; do # Process command line options case "${option}" in b) BRANCH=${OPTARG} ;; d) DATE_FMT=${OPTARG} ;; @@ -169,6 +171,7 @@ while getopts b:d:h:g:L:l:m:c:C:p:r:s:e:x:MRv option; do # Process command line m) COMMITMSG=${OPTARG} ;; c) COMMITCMD=${OPTARG} ;; C) PASSDIFFS=1 ;; + f) COMMIT_ON_START=1 ;; M) SKIP_IF_MERGING=1 ;; p | r) REMOTE=${OPTARG} ;; R) PULL_BEFORE_PUSH=1 ;; @@ -261,11 +264,11 @@ if [ -d "$1" ]; then # if the target is a directory # construct inotifywait-commandline if [ "$(uname)" != "Darwin" ]; then # shellcheck disable=SC2206 - INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") + INW_ARGS=("-qmr" "-e" "$EVENTS" "--exclude" $EXCLUDE_OPTS "$(printf "%q" "$TARGETDIR")") else # still need to fix EVENTS since it wants them listed one-by-one # shellcheck disable=SC2206 - INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" $EXCLUDE_OPTS "\"$TARGETDIR\"") + INW_ARGS=("--recursive" "$EVENTS" "-E" "--exclude" $EXCLUDE_OPTS "$(printf "%q" "$TARGETDIR")") fi GIT_ADD_ARGS="--all ." # add "." (CWD) recursively to index GIT_COMMIT_ARGS="" # add -a switch to "commit" call just to be sure @@ -369,8 +372,88 @@ diff-lines() { done } +# The main commit and push logic +perform_commit() { + local LOCAL_FORMATTED_COMMITMSG + + if [ -n "$DATE_FMT" ]; then + LOCAL_FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" + else + LOCAL_FORMATTED_COMMITMSG="$FORMATTED_COMMITMSG" + fi + + if [[ $LISTCHANGES -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed + local DIFF_COMMITMSG + DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" + local 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 + # Use git diff as the commit msg, unless if files were added or deleted but not modified + if [ -n "$DIFF_COMMITMSG" ]; then + LOCAL_FORMATTED_COMMITMSG="$DIFF_COMMITMSG" + else + LOCAL_FORMATTED_COMMITMSG="New files added: $($GIT status -s)" + fi + else + LOCAL_FORMATTED_COMMITMSG=$($GIT diff --stat | grep '|') + fi + fi + + if [ -n "$COMMITCMD" ]; then + if [ "$PASSDIFFS" -eq 1 ]; then + # If -C is set, pass the list of diffed files to the commit command + # Unsure whether or not I should check if the command fails + LOCAL_FORMATTED_COMMITMSG="$($COMMITCMD < <($GIT diff --name-only))" + else + LOCAL_FORMATTED_COMMITMSG="$($COMMITCMD)" + fi + fi + + local STATUS + STATUS=$($GIT status -s) + if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. + verbose_echo "Tracked changes detected." + # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word split + # shellcheck disable=SC2086 + + if [ "$SKIP_IF_MERGING" -eq 1 ] && is_merging; then + verbose_echo "Skipping commit - repo is merging" + return + fi + + # shellcheck disable=SC2086 + $GIT add $GIT_ADD_ARGS # add file(s) to index + verbose_echo "Running git add with arguments: $GIT_ADD_ARGS" + # shellcheck disable=SC2086 + $GIT commit $GIT_COMMIT_ARGS -m"$LOCAL_FORMATTED_COMMITMSG" # construct commit message and commit + verbose_echo "Running git commit with arguments: $GIT_COMMIT_ARGS -m\"$LOCAL_FORMATTED_COMMITMSG\"" + + if [ -n "$PULL_CMD" ]; then + echo "Pull command is $PULL_CMD" + verbose_echo "Executing pull command: $PULL_CMD" + eval "$PULL_CMD" + fi + + if [ -n "$PUSH_CMD" ]; then + echo "Push command is $PUSH_CMD" + verbose_echo "Executing push command: $PUSH_CMD" + eval "$PUSH_CMD" + fi + else + verbose_echo "No tracked changes detected." + fi +} + ############################################################################### +# If -f is specified, perform an initial commit before starting to watch +if [ "$COMMIT_ON_START" -eq 1 ]; then + verbose_echo "Performing initial commit check..." + perform_commit +fi + # main program loop: wait for changes and commit them # 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 @@ -390,78 +473,7 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do # start timeout process ( sleep "$SLEEP_TIME" # wait some more seconds to give apps time to write out all changes - - if [ -n "$DATE_FMT" ]; then - #FORMATTED_COMMITMSG="$(sed "s/%d/$(date "$DATE_FMT")/" <<< "$COMMITMSG")" # splice the formatted date-time into the commit message - FORMATTED_COMMITMSG="${COMMITMSG/\%d/$(date "$DATE_FMT")}" # splice the formatted date-time into the commit message - fi - - if [[ $LISTCHANGES -ge 0 ]]; then # allow listing diffs in the commit log message, unless if there are too many lines changed - DIFF_COMMITMSG="$($GIT diff -U0 "$LISTCHANGES_COLOR" | diff-lines)" - 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 - # 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 - FORMATTED_COMMITMSG="New files added: $($GIT status -s)" - fi - else - #FORMATTED_COMMITMSG="Many lines were modified. $FORMATTED_COMMITMSG" - FORMATTED_COMMITMSG=$($GIT diff --stat | grep '|') - fi - fi - - if [ -n "$COMMITCMD" ]; then - if [ "$PASSDIFFS" -eq 1 ]; then - # If -C is set, pass the list of diffed files to the commit command - # Unsure whether or not I should check if the command fails - FORMATTED_COMMITMSG="$($COMMITCMD < <($GIT diff --name-only))" - else - FORMATTED_COMMITMSG="$($COMMITCMD)" - fi - fi - - # CD into right dir - cd "$TARGETDIR" || { - stderr "Error: Can't change directory to '${TARGETDIR}'." - exit 6 - } - STATUS=$($GIT status -s) - if [ -n "$STATUS" ]; then # only commit if status shows tracked changes. - verbose_echo "Tracked changes detected." - # We want GIT_ADD_ARGS and GIT_COMMIT_ARGS to be word split - # shellcheck disable=SC2086 - - if [ "$SKIP_IF_MERGING" -eq 1 ] && is_merging; then - verbose_echo "Skipping commit - repo is merging" - exit 0 - fi - - # shellcheck disable=SC2086 - $GIT add $GIT_ADD_ARGS # add file(s) to index - verbose_echo "Running git add with arguments: $GIT_ADD_ARGS" - # shellcheck disable=SC2086 - $GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit - verbose_echo "Running git commit with arguments: $GIT_COMMIT_ARGS -m\"$FORMATTED_COMMITMSG\"" - - if [ -n "$PULL_CMD" ]; then - echo "Pull command is $PULL_CMD" - verbose_echo "Executing pull command: $PULL_CMD" - eval "$PULL_CMD" - fi - - if [ -n "$PUSH_CMD" ]; then - echo "Push command is $PUSH_CMD" - verbose_echo "Executing push command: $PUSH_CMD" - eval "$PUSH_CMD" - fi - else - verbose_echo "No tracked changes detected." - fi + perform_commit ) & # and send into background SLEEP_PID=$! # and remember its PID From 5cdaeb49dc0872961cb11e1d876cefe8b9aca7c4 Mon Sep 17 00:00:00 2001 From: meek2100 <34908880+meek2100@users.noreply.github.com> Date: Mon, 27 Oct 2025 11:48:53 -0700 Subject: [PATCH 88/95] Add support for docker-compose and building a ghcr.io docker package. (#145) * Add support for docker-compose and building a ghcr.io docker package. * Mark entrypoint.sh as executable * Add support for paths with spaces. --- .github/workflows/publish-docker.yml | 29 +++++++ Dockerfile | 9 +- README.md | 119 +++++++++++++++++++++++++++ docker-compose.yml | 45 ++++++++++ entrypoint.sh | 93 +++++++++++++++++++++ 5 files changed, 291 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/publish-docker.yml create mode 100644 docker-compose.yml create mode 100755 entrypoint.sh diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml new file mode 100644 index 00000000..3823ff4f --- /dev/null +++ b/.github/workflows/publish-docker.yml @@ -0,0 +1,29 @@ +--- +name: Docker Build and Publish +# Allows you to run this workflow manually from the Actions tab +on: workflow_dispatch +jobs: + build-and-push: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: | + ghcr.io/${{ github.repository }}:latest + ghcr.io/${{ github.repository }}:${{ github.sha }} diff --git a/Dockerfile b/Dockerfile index 25be743f..f1cfb032 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ -FROM alpine:3.1 +FROM alpine:latest # hadolint ignore=DL3018 RUN apk add --no-cache bash git inotify-tools openssh RUN mkdir -p /app WORKDIR /app -COPY gitwatch.sh ./ -RUN chmod 755 -- *.sh +COPY gitwatch.sh entrypoint.sh ./ -ENTRYPOINT ["./gitwatch.sh"] +RUN chmod +x /app/gitwatch.sh /app/entrypoint.sh + +ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 9a69adc0..f4591325 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ - [NixOS](#nixos) - [As Module](#as-module) - [As Package](#as-package) + - [Docker](#docker) + - [Docker Compose (Recommended)](#docker-compose-recommended) + - [Using the Dockerfile](#using-the-dockerfile) - [Requirements](#requirements) - [Notes for Mac](#notes-for-mac) - [What it does](#what-it-does) @@ -122,6 +125,122 @@ This will make NixOS to create `systemd` service named The `gitwatch` script available as package in _nixpkgs_; +## Docker + +You can also run `gitwatch` inside a Docker container. This is useful for +isolating dependencies and ensuring a consistent environment. + +### Docker Compose (Recommended) + +The easiest way to run `gitwatch` with Docker is by using the provided +`docker-compose.yml` file. + +**1. Prerequisites:** + +- **Docker and Docker Compose**: Make sure you have both installed. + - [Install Docker](https://docs.docker.com/get-docker/) + - [Install Docker Compose](https://docs.docker.com/compose/install/) +- **A Git Repository**: You need a local directory that is a Git repository + you want to watch. +- **SSH Key**: For pushing to a remote repository, the container needs + access to an SSH key that is authorized with your Git provider. + +**2. Configuration:** + +The `docker-compose.yml` file is configured using environment variables. +You can either edit the `environment` section directly in the file or +create a `.env` file in the same directory to set the values. + +Here's a breakdown of the important parts of the `docker-compose.yml` file: + +- **`volumes`**: This is the most critical section to configure. + - `./watched-repo:/app/watched-repo`: This maps a directory from your + computer (the "host") into the container. + - You **must** change `./watched-repo` to the path of the local Git + repository you want `gitwatch` to monitor. + - `~/.ssh/id_rsa:/root/.ssh/id_rsa:ro`: This securely mounts your SSH + private key into the container in read-only mode (`ro`). This is + necessary for `gitwatch` to push changes to your remote repository. + - `~/.gitconfig:/root/.gitconfig:ro`: This mounts your Git configuration + into the container. This ensures that the commits made by `gitwatch` + are attributed to you with the correct name and email. +- **`environment`**: This section controls how `gitwatch` behaves. + + **3. Environment Variables** + +The following environment variables are available for configuring the +`gitwatch` container: + +| Variable | Default Value | Description | +| :----------------- | :--------------------- | :---------------------------------------------------------------------------------------------------------------------------- | +| `GIT_WATCH_DIR` | `/app/watched-repo` | The directory inside the container to watch for changes. This must match the container path you set in the `volumes` section. | +| `GIT_REMOTE` | `origin` | The name of the remote repository to push to. | +| `GIT_BRANCH` | `main` | The branch to push to. | +| `PULL_BEFORE_PUSH` | `"false"` | Set to `"true"` to run `git pull --rebase` before every push. | +| `SLEEP_TIME` | `2` | Time in seconds to wait after a file change before committing. | +| `COMMIT_MSG` | `"Auto-commit: %d"` | The commit message format. `%d` is replaced with the date/time. | +| `DATE_FMT` | `"+%Y-%m-%d %H:%M:%S"` | The date format used in the commit message (see `man date` for options). | +| `EXCLUDE_PATTERN` | `""` | A comma-separated list of patterns to exclude from monitoring (e.g., `"*.log, *.tmp, tmp/"`). | +| `SKIP_IF_MERGING` | `"false"` | Set to `"true"` to prevent commits when a merge is in progress. | +| `COMMIT_ON_START` | `"false"` | Set to "true" to commit any pending changes on startup. | +| `VERBOSE` | `"false"` | Set to "true" to enable verbose output for debugging. | + + + +**4. Running gitwatch:** + +- **Start the container** in the background (detached mode): + + ```sh + docker-compose up -d + ``` + +- **View the logs** to see what `gitwatch` is doing: + + ```sh + docker-compose logs -f + ``` + +- **Stop the container**: + + ```sh + docker-compose down + ``` + +### Using the Dockerfile + +If you prefer to build the Docker image yourself, you can use the provided +`Dockerfile`. This is useful if you want to customize the image with +additional tools or dependencies. + +**1. Build the image:** + +From the root of the `gitwatch` repository, run: + +```sh +docker build -t gitwatch . +``` + +**2. Run the container:** + +To run the container, you need to provide the same volumes and environment +variables as the Docker Compose setup. + +```sh +docker run -d \ + --name gitwatch \ + -v /path/to/your/repo:/app/watched-repo \ + -v ~/.ssh/id_rsa:/root/.ssh/id_rsa:ro \ + -v ~/.gitconfig:/root/.gitconfig:ro \ + -e GIT_WATCH_DIR="/app/watched-repo" \ + -e GIT_REMOTE="origin" \ + -e GIT_BRANCH="main" \ + gitwatch +``` + +**Important:** Remember to replace `/path/to/your/repo` with the actual +path to the Git repository you want to watch. + ## Requirements To run this script, you must have installed and globally available: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..fbf9db19 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,45 @@ +version: "3.8" + +services: + gitwatch: + # Pull the latest official image from the gitwatch repository on GitHub Container Registry + image: ghcr.io/gitwatch/gitwatch:latest + restart: always + volumes: + # The local directory on your machine that you want to watch + # IMPORTANT: You must create this directory first (e.g., ./my-notes-repo) + - ./watched-repo:/app/watched-repo + + # Mount your SSH key for pushing to remote repositories + - ~/.ssh/id_rsa:/root/.ssh/id_rsa:ro + + # Mount your git config to use your correct user name and email for commits + - ~/.gitconfig:/root/.gitconfig:ro + environment: + # --- Watched Directory --- + # This should match the container path in the 'volumes' section above + GIT_WATCH_DIR: /app/watched-repo + + # --- Git Remote Configuration --- + # The remote repository to push to (e.g., 'origin') + GIT_REMOTE: origin + # The branch to push to (e.g., 'main' or 'master') + GIT_BRANCH: main + # Set to "true" to run 'git pull --rebase' before every push + PULL_BEFORE_PUSH: "false" + + # --- Gitwatch Behavior --- + # Time in seconds to wait after a file change before committing + SLEEP_TIME: 2 + # The commit message format. %d is replaced with the date/time + COMMIT_MSG: "Auto-commit: %d" + # The date format used in the commit message (see 'man date' for options) + DATE_FMT: "+%Y-%m-%d %H:%M:%S" + # A comma-separated list of patterns to exclude from monitoring (e.g., "*.log, *.tmp, tmp/") + EXCLUDE_PATTERN: "" + # Set to "true" to prevent commits when a merge is in progress + SKIP_IF_MERGING: "false" + # Set to "true" to commit any pending changes on startup + COMMIT_ON_START: "false" + # Set to "true" to enable verbose output for debugging + VERBOSE: "false" diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..0dd28dc3 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status +set -e + +# --- Environment Variable Configuration with Defaults --- + +# Target directory to watch +GIT_WATCH_DIR=${GIT_WATCH_DIR:-/app/watched-repo} + +# Git options +GIT_REMOTE=${GIT_REMOTE:-origin} +GIT_BRANCH=${GIT_BRANCH:-main} + +# Gitwatch behavior +SLEEP_TIME=${SLEEP_TIME:-2} +COMMIT_MSG=${COMMIT_MSG:-"Scripted auto-commit on change (%d) by gitwatch.sh"} +DATE_FMT=${DATE_FMT:-"+%Y-%m-%d %H:%M:%S"} +# Read the user-friendly pattern +USER_EXCLUDE_PATTERN=${EXCLUDE_PATTERN:-""} +EVENTS=${EVENTS:-""} + +# Boolean flags (set to "true" to enable) +PULL_BEFORE_PUSH=${PULL_BEFORE_PUSH:-false} +SKIP_IF_MERGING=${SKIP_IF_MERGING:-false} +VERBOSE=${VERBOSE:-false} +COMMIT_ON_START=${COMMIT_ON_START:-false} + +# --- Command Construction --- + +# Use a bash array to safely build the command and its arguments +cmd=( "/app/gitwatch.sh" ) + +# Add options with arguments +cmd+=( -r "${GIT_REMOTE}" ) +cmd+=( -b "${GIT_BRANCH}" ) +cmd+=( -s "${SLEEP_TIME}" ) +cmd+=( -m "${COMMIT_MSG}" ) +cmd+=( -d "${DATE_FMT}" ) + +# --- Convert User-Friendly Exclude Pattern to Regex --- +if [ -n "${USER_EXCLUDE_PATTERN}" ]; then + # 1. Replace commas with spaces to treat as separate words. + PATTERNS_AS_WORDS=${USER_EXCLUDE_PATTERN//,/ } + # 2. Use an array to store and automatically trim whitespace from each pattern. + read -r -a PATTERN_ARRAY <<< "$PATTERNS_AS_WORDS" + # 3. Join the array elements with the regex OR pipe `|`. + PROCESSED_PATTERN=$(IFS=\|; echo "${PATTERN_ARRAY[*]}") + + # 4. Escape periods to treat them as literal dots in regex + PROCESSED_PATTERN=${PROCESSED_PATTERN//./\\.} + + # 5. Convert glob stars `*` into the regex equivalent `.*` + PROCESSED_PATTERN=${PROCESSED_PATTERN//\*/\.\*} + + cmd+=( -x "${PROCESSED_PATTERN}" ) +fi + + +if [ -n "${EVENTS}" ]; then + cmd+=( -e "${EVENTS}" ) +fi + +# Add boolean flags if they are set to "true" +if [ "${PULL_BEFORE_PUSH}" = "true" ]; then + cmd+=( -R ) +fi + +if [ "${SKIP_IF_MERGING}" = "true" ]; then + cmd+=( -M ) +fi + +if [ "${VERBOSE}" = "true" ]; then + cmd+=( -v ) +fi + +if [ "${COMMIT_ON_START}" = "true" ]; then + cmd+=( -f ) +fi + +# The final argument is the directory to watch +cmd+=( "${GIT_WATCH_DIR}" ) + +# --- Execution --- + +echo "Starting gitwatch with the following arguments:" +# Use printf with %q to safely quote the arguments for display +printf "%q " "${cmd[@]}" +echo # Add a newline for cleaner logging +echo "-------------------------------------------------" + +# Use exec to replace the current shell process with gitwatch +exec "${cmd[@]}" From be61d71f943c677462a408de892ec4abfa788220 Mon Sep 17 00:00:00 2001 From: meek2100 <34908880+meek2100@users.noreply.github.com> Date: Fri, 31 Oct 2025 06:02:25 -0700 Subject: [PATCH 89/95] Update pre-commit configuration and clean up failures. (#149) * Update pre-commit config, update versions, move prettier to stable, and coverage for Dockerfile and nix files. * Cleanup pre-commit issues. * Fix precommit failures due to new handolint and lack of executable permission on .bats tests. * Fix nix pre-commit failures. * Fix last pre-commit failure. --- .github/linters/.markdown-lint.yml | 2 +- .gitignore | 2 + .pre-commit-config.yaml | 62 ++++++++------- Dockerfile | 8 +- flake.nix | 13 +++- gitwatch.nix | 44 +++++++---- module.nix | 96 ++++++++++++----------- tests/commitcmd.bats | 97 ++++++++++++----------- tests/commitlog.bats | 41 +++++----- tests/notify-ignore.bats | 60 +++++++------- tests/pull-rebase.bats | 117 ++++++++++++++-------------- tests/remotedirs.bats | 61 +++++++-------- tests/spaces.bats | 43 +++++----- tests/startup-shutdown-spaces.bash | 0 tests/startup-shutdown.bash | 0 tests/status-change.bats | 62 +++++++-------- tests/sync.bats | 121 ++++++++++++++--------------- 17 files changed, 422 insertions(+), 407 deletions(-) mode change 100644 => 100755 tests/commitcmd.bats mode change 100644 => 100755 tests/commitlog.bats mode change 100644 => 100755 tests/notify-ignore.bats mode change 100644 => 100755 tests/pull-rebase.bats mode change 100644 => 100755 tests/remotedirs.bats mode change 100644 => 100755 tests/spaces.bats mode change 100755 => 100644 tests/startup-shutdown-spaces.bash mode change 100755 => 100644 tests/startup-shutdown.bash mode change 100644 => 100755 tests/status-change.bats mode change 100644 => 100755 tests/sync.bats diff --git a/.github/linters/.markdown-lint.yml b/.github/linters/.markdown-lint.yml index 7e62b3d9..dd720ff9 120000 --- a/.github/linters/.markdown-lint.yml +++ b/.github/linters/.markdown-lint.yml @@ -1 +1 @@ -../../.markdownlint.yml \ No newline at end of file +../../.markdownlint.yml diff --git a/.gitignore b/.gitignore index ce11904c..8b5f7f2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +#Directories +node_modules/ #Archives *.gz *.tar diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 63203760..1fce4d4d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,24 +1,21 @@ --- -files: ^(.*\.(py|json|md|sh|yaml|yml|cfg|txt))$ +# REMOVED global 'files:' regex to expand coverage to all file types. exclude: ^(\.[^/]*cache/.*|demo/.*|debug/.*)$ repos: - # Disable because this does not work for the main author. - # - repo: https://github.com/pre-commit/mirrors-prettier - # rev: "v2.7.1" - # stages: [manual] - # hooks: - # - id: prettier + - repo: https://github.com/rbubley/mirrors-prettier + rev: "v3.6.2" + hooks: + - id: prettier + types_or: [javascript, json, markdown, yaml, html, css] - repo: https://github.com/executablebooks/mdformat - # Do this before other tools "fixing" the line endings - rev: 0.7.22 + rev: 1.0.0 hooks: - id: mdformat name: Format Markdown - entry: mdformat # Executable to run, with fixed options + entry: mdformat language: python types: [markdown] args: [--wrap, "75", --number] - # files: ^HomeAssistant.md$ additional_dependencies: - mdformat-toc - mdformat-beautysh @@ -26,48 +23,41 @@ repos: - mdformat-gfm - setuptools - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: no-commit-to-branch args: [--branch, main] - id: check-yaml - # Exclude because of bug in checker exclude: ^(docker-compose\.yml|.*/release-drafter\.yml)$ - id: debug-statements - id: end-of-file-fixer - id: trailing-whitespace - id: check-json - id: mixed-line-ending - - id: check-builtin-literals - - id: check-ast + args: [--fix=lf] + types: [text] + exclude: \.md$ - id: check-merge-conflict - id: check-executables-have-shebangs - id: check-shebang-scripts-are-executable - - id: check-docstring-first - id: fix-byte-order-marker - id: check-case-conflict - - id: pretty-format-json - exclude: ^(.vscode|.devcontainer) - args: - # order of keys in manifest.json is "special" - - --no-sort-keys - - repo: https://github.com/pre-commit/mirrors-prettier - rev: v4.0.0-alpha.8 - hooks: - - id: prettier + - id: check-added-large-files + args: ["--maxkb=500"] - repo: https://github.com/adrienverge/yamllint.git rev: v1.37.1 hooks: - id: yamllint args: - - --no-warnings - - -d - - "{extends: relaxed, rules: {line-length: {max: 90}}}" + - "--no-warnings" + - "-d {extends: relaxed, rules: {line-length: {max: 90}}}" - repo: https://github.com/codespell-project/codespell rev: v2.4.1 hooks: - id: codespell - # exclude: (\.md|apps.yaml|translations/.*.yaml)$ + args: + - "--ignore-words-list=hist,thist" + - "--skip=*.lock,*.json" - repo: https://github.com/IamTheFij/docker-pre-commit rev: v3.0.1 hooks: @@ -80,8 +70,20 @@ repos: additional_dependencies: - setuptools - repo: https://github.com/shellcheck-py/shellcheck-py - rev: v0.10.0.1 + rev: v0.11.0.1 hooks: - id: shellcheck files: ^[^\.].*\.sh$ args: [--shell, bash] + - repo: https://github.com/AleksaC/hadolint-py + rev: v2.14.0 + hooks: + - id: hadolint + name: Lint Dockerfiles + args: [--ignore, DL3018] + - repo: https://github.com/NixOS/nixfmt + rev: v1.1.0 + hooks: + - id: nixfmt + name: Format Nix files + types: [nix] diff --git a/Dockerfile b/Dockerfile index f1cfb032..bdf5287f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ -FROM alpine:latest +FROM alpine:3.20 # hadolint ignore=DL3018 -RUN apk add --no-cache bash git inotify-tools openssh +RUN apk add --no-cache bash git inotify-tools openssh && \ + mkdir -p /app -RUN mkdir -p /app WORKDIR /app COPY gitwatch.sh entrypoint.sh ./ RUN chmod +x /app/gitwatch.sh /app/entrypoint.sh -ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/flake.nix b/flake.nix index e1585044..d0669ae6 100644 --- a/flake.nix +++ b/flake.nix @@ -1,8 +1,14 @@ { description = "A bash script to watch a file or folder and commit changes to a git repo"; - outputs = { self, nixpkgs, flake-utils }: + outputs = + { + self, + nixpkgs, + flake-utils, + }: let - packages = flake-utils.lib.eachDefaultSystem (system: + packages = flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; @@ -13,7 +19,8 @@ gitwatch = pkgs.callPackage ./gitwatch.nix { }; default = gitwatch; }; - }); + } + ); in packages // { modules = [ ./module.nix ]; }; } diff --git a/gitwatch.nix b/gitwatch.nix index 4205ecd9..950ce040 100644 --- a/gitwatch.nix +++ b/gitwatch.nix @@ -1,19 +1,29 @@ -{ runCommandNoCC -, lib -, makeWrapper +{ + runCommandNoCC, + lib, + makeWrapper, -, git -, openssh -, inotify-tools -}: runCommandNoCC "gitwatch" { - nativeBuildInputs = [ makeWrapper ]; -} '' - mkdir -p $out/bin - dest="$out/bin/gitwatch" - cp ${./gitwatch.sh} $dest - chmod +x $dest - patchShebangs $dest + git, + openssh, + inotify-tools, +}: +runCommandNoCC "gitwatch" + { + nativeBuildInputs = [ makeWrapper ]; + } + '' + mkdir -p $out/bin + dest="$out/bin/gitwatch" + cp ${./gitwatch.sh} $dest + chmod +x $dest + patchShebangs $dest - wrapProgram $dest \ - --prefix PATH ';' ${lib.makeBinPath [ git inotify-tools openssh ]} -'' + wrapProgram $dest \ + --prefix PATH ';' ${ + lib.makeBinPath [ + git + inotify-tools + openssh + ] + } + '' diff --git a/module.nix b/module.nix index 7e76fe35..f26424cc 100644 --- a/module.nix +++ b/module.nix @@ -1,27 +1,29 @@ -{ lib, pkgs, config, ... }: +{ + lib, + pkgs, + config, + ... +}: let gitwatch = pkgs.callPackage ./gitwatch.nix { }; - mkSystemdService = name: cfg: lib.nameValuePair - "gitwatch-${name}" - ( + mkSystemdService = + name: cfg: + lib.nameValuePair "gitwatch-${name}" ( let - getvar = flag: var: - if cfg."${var}" != null - then "${flag} ${cfg."${var}"}" - else ""; + getvar = flag: var: if cfg."${var}" != null then "${flag} ${cfg."${var}"}" else ""; branch = getvar "-b" "branch"; - fetcher = - if cfg.remote == null - then "true" - else '' - ''; + fetcher = if cfg.remote == null then "true" else ''''; in { inherit (cfg) enable; after = [ "network-online.target" ]; wantedBy = [ "multi-user.target" ]; description = "gitwatch for ${name}"; - path = with pkgs; [ gitwatch git openssh ]; + path = with pkgs; [ + gitwatch + git + openssh + ]; script = '' if [ -n "${cfg.remote}" ] && ! [ -d "${cfg.path}" ]; then git clone ${branch} "${cfg.remote}" "${cfg.path}" @@ -36,8 +38,9 @@ in { options.services.gitwatch = lib.mkOption { description = '' - A set of git repositories to watch for. See - [gitwatch](https://github.com/gitwatch/gitwatch) for more. + A set of git repositories to watch for. + See + [gitwatch](https://github.com/gitwatch/gitwatch) for more. ''; default = { }; example = { @@ -56,36 +59,37 @@ in branch = "autobranch"; }; }; - type = with lib.types; attrsOf (submodule { - options = { - enable = lib.mkEnableOption "watching for repo"; - path = lib.mkOption { - description = "The path to repo in local machine"; - type = str; - }; - user = lib.mkOption { - description = "The name of services's user"; - type = str; - default = "root"; - }; - remote = lib.mkOption { - description = "Optional url of remote repository"; - type = nullOr str; - default = null; + type = + with lib.types; + attrsOf (submodule { + options = { + enable = lib.mkEnableOption "watching for repo"; + path = lib.mkOption { + description = "The path to repo in local machine"; + type = str; + }; + user = lib.mkOption { + description = "The name of services's user"; + type = str; + default = "root"; + }; + remote = lib.mkOption { + description = "Optional url of remote repository"; + type = nullOr str; + default = null; + }; + message = lib.mkOption { + description = "Optional message to use in as commit message."; + type = nullOr str; + default = null; + }; + branch = lib.mkOption { + description = "Optional branch in remote repository"; + type = nullOr str; + default = null; + }; }; - message = lib.mkOption { - description = "Optional message to use in as commit message."; - type = nullOr str; - default = null; - }; - branch = lib.mkOption { - description = "Optional branch in remote repository"; - type = nullOr str; - default = null; - }; - }; - }); + }); }; - config.systemd.services = - lib.mapAttrs' mkSystemdService config.services.gitwatch; + config.systemd.services = lib.mapAttrs' mkSystemdService config.services.gitwatch; } diff --git a/tests/commitcmd.bats b/tests/commitcmd.bats old mode 100644 new mode 100755 index 6db8a91b..af839f14 --- a/tests/commitcmd.bats +++ b/tests/commitcmd.bats @@ -8,75 +8,74 @@ load startup-shutdown function commit_command_single { #@test - - # Start up gitwatch with custom commit command, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! - # Create a file, verify that it hasn't been added yet, then commit and push - cd remote + # Keeps kill message from printing to screen + disown - # 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 + # Create a file, verify that it hasn't been added yet, then commit and push + cd remote - # Wait a bit for inotify to figure out the file has changed, and do its add, commit, and push. - sleep $WAITTIME + # 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 - run git log -1 --oneline - [[ $output == *$(uname) ]] + # Wait a bit for inotify to figure out the file has changed, and do its add, commit, and push. + sleep $WAITTIME + + run git log -1 --oneline + [[ $output == *$(uname) ]] } function commit_command_format { #@test - # tests nested commit command + # tests nested commit command - # Start up gitwatch with custom commit command, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "echo '$(uname) is the uname of this device, the time is $(date)' " "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "echo '$(uname) is the uname of this device, the time is $(date)' " "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Keeps kill message from printing to screen + disown - # Create a file, verify that it hasn't been added yet, then commit and push - cd remote + # Create a file, verify that it hasn't been added yet, then commit and push + 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 + # 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, commit, and push. - sleep $WAITTIME + # Wait a bit for inotify to figure out the file has changed, and do its add, commit, and push. + sleep $WAITTIME - run git log -1 --oneline - [[ $output == *$(uname)* ]] - [[ $output == *$(date +%Y)* ]] + run git log -1 --oneline + [[ $output == *$(uname)* ]] + [[ $output == *$(date +%Y)* ]] } function commit_command_overwrite { #@test - # Start up gitwatch with custom commit command, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" -l 123 -L 0 -d "+%Y" "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! + # Start up gitwatch with custom commit command, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -c "uname" -l 123 -L 0 -d "+%Y" "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Keeps kill message from printing to screen + disown - # Create a file, verify that it hasn't been added yet, then commit and push - cd remote + # Create a file, verify that it hasn't been added yet, then commit and push + 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 + # 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, commit, and push. - sleep $WAITTIME + # Wait a bit for inotify to figure out the file has changed, and do its add, commit, and push. + sleep $WAITTIME - run git log -1 --oneline - [[ $output == *$(uname)* ]] + run git log -1 --oneline + [[ $output == *$(uname)* ]] } - diff --git a/tests/commitlog.bats b/tests/commitlog.bats old mode 100644 new mode 100755 index 1f96266c..f4cef2b4 --- a/tests/commitlog.bats +++ b/tests/commitlog.bats @@ -8,31 +8,30 @@ load startup-shutdown function commit_log_messages_working { #@test - # Start up gitwatch with logging, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! + # Start up gitwatch with logging, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Keeps kill message from printing to screen + disown - # Create a file, verify that it hasn't been added yet, then commit - cd remote + # 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 + # 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" + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep "$WAITTIME" - # Make a new change - echo "line2" >> file1.txt - sleep "$WAITTIME" + # Make a new change + echo "line2" >> file1.txt + sleep "$WAITTIME" - # Check commit log that the diff is in there - run git log -1 --oneline - [[ $output == *"file1.txt"* ]] + # Check commit log that the diff is in there + run git log -1 --oneline + [[ $output == *"file1.txt"* ]] } - diff --git a/tests/notify-ignore.bats b/tests/notify-ignore.bats old mode 100644 new mode 100755 index 4c39e076..8ea7a7b2 --- a/tests/notify-ignore.bats +++ b/tests/notify-ignore.bats @@ -12,44 +12,42 @@ load startup-shutdown 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=$! + # 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 + # 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 + # 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 + # 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 + # 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 + # 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 + # 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 + 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 ] + # 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 ] + run grep "file2.txt" $testdir/output.txt + [ $status -ne 0 ] } - - diff --git a/tests/pull-rebase.bats b/tests/pull-rebase.bats old mode 100644 new mode 100755 index f08217a8..c2698ccf --- a/tests/pull-rebase.bats +++ b/tests/pull-rebase.bats @@ -9,63 +9,62 @@ load startup-shutdown function pulling_and_rebasing_correctly { #@test - # Create a file, verify that it hasn't been added yet, - # then commit and push - cd remote - - # Start up gitwatch and see if commit and push happen automatically - # after waiting two seconds - ${BATS_TEST_DIRNAME}/../gitwatch.sh -r origin -R "$testdir/local/remote" 3>- & - GITWATCH_PID=$! - - # Keeps kill message from printing to screen - disown - - # 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, - # commit, and push. - sleep $WAITTIME - - # Verify that push happened - currentcommit=$(git rev-parse master) - remotecommit=$(git rev-parse origin/master) - [ "$currentcommit" = "$remotecommit" ] - - # Create a second local - cd ../.. - mkdir local2 - cd local2 - git clone -q ../remote - cd remote - - # Add a file to new repo - sleep 1 - echo "line2" >> file2.txt - git add file2.txt - git commit -am "file 2 added" - git push - - # Change back to original repo, make a third change, then verify that - # second one got here - cd ../../local/remote - sleep 1 - echo "line3" >> file3.txt - - # Verify that push happened - currentcommit=$(git rev-parse master) - remotecommit=$(git rev-parse origin/master) - [ "$currentcommit" = "$remotecommit" ] - - # Verify that new file is here - sleep $WAITTIME - [ -f file2.txt ] - - # Remove testing directories - cd /tmp - rm -rf $testdir + # Create a file, verify that it hasn't been added yet, + # then commit and push + cd remote + + # Start up gitwatch and see if commit and push happen automatically + # after waiting two seconds + ${BATS_TEST_DIRNAME}/../gitwatch.sh -r origin -R "$testdir/local/remote" 3>- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # 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, + # commit, and push. + sleep $WAITTIME + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Create a second local + cd ../.. + mkdir local2 + cd local2 + git clone -q ../remote + cd remote + + # Add a file to new repo + sleep 1 + echo "line2" >> file2.txt + git add file2.txt + git commit -am "file 2 added" + git push + + # Change back to original repo, make a third change, then verify that + # second one got here + cd ../../local/remote + sleep 1 + echo "line3" >> file3.txt + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Verify that new file is here + sleep $WAITTIME + [ -f file2.txt ] + + # Remove testing directories + cd /tmp + rm -rf $testdir } - diff --git a/tests/remotedirs.bats b/tests/remotedirs.bats old mode 100644 new mode 100755 index 06ff64e1..145be729 --- a/tests/remotedirs.bats +++ b/tests/remotedirs.bats @@ -8,44 +8,43 @@ load startup-shutdown function remote_git_dirs_working_with_commit_logging { #@test - # Move .git somewhere else - dotgittestdir=$(mktemp -d) - mv "$testdir/local/remote/.git" "$dotgittestdir" + # Move .git somewhere else + dotgittestdir=$(mktemp -d) + mv "$testdir/local/remote/.git" "$dotgittestdir" - # Start up gitwatch, intentionally in wrong directory, with remote dir specified - ${BATS_TEST_DIRNAME}/../gitwatch.sh -l 10 -g "$dotgittestdir/.git" "$testdir/local/remote" 3>&- & - GITWATCH_PID=$! + # Start up gitwatch, intentionally in wrong directory, with remote dir specified + ${BATS_TEST_DIRNAME}/../gitwatch.sh -l 10 -g "$dotgittestdir/.git" "$testdir/local/remote" 3>&- & + GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Keeps kill message from printing to screen + disown - # Create a file, verify that it hasn't been added yet, then commit - cd remote + # 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 + # 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 + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep $WAITTIME - # Store commit for later comparison - lastcommit=$(git --git-dir $dotgittestdir/.git rev-parse master) + # Store commit for later comparison + lastcommit=$(git --git-dir $dotgittestdir/.git rev-parse master) - # Make a new change - echo "line2" >> file1.txt - sleep $WAITTIME - - # Verify that new commit has happened - currentcommit=$(git --git-dir $dotgittestdir/.git rev-parse master) - [ "$lastcommit" != "$currentcommit" ] + # Make a new change + echo "line2" >> file1.txt + sleep $WAITTIME - # Check commit log that the diff is in there - run git --git-dir $dotgittestdir/.git log -1 --oneline - [[ $output == *"file1.txt"* ]] + # Verify that new commit has happened + currentcommit=$(git --git-dir $dotgittestdir/.git rev-parse master) + [ "$lastcommit" != "$currentcommit" ] - rm -rf $dotgittestdir -} + # Check commit log that the diff is in there + run git --git-dir $dotgittestdir/.git log -1 --oneline + [[ $output == *"file1.txt"* ]] + rm -rf $dotgittestdir +} diff --git a/tests/spaces.bats b/tests/spaces.bats old mode 100644 new mode 100755 index 09e90e8a..40a1ae16 --- a/tests/spaces.bats +++ b/tests/spaces.bats @@ -5,32 +5,31 @@ load startup-shutdown-spaces function spaces_in_target_dir { #@test - # Start up gitwatch with logging, see if works - "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/rem with spaces" 3>&- & - echo "Testdir: $testdir" >&3 - GITWATCH_PID=$! + # Start up gitwatch with logging, see if works + "${BATS_TEST_DIRNAME}"/../gitwatch.sh -l 10 "$testdir/local/rem with spaces" 3>&- & + echo "Testdir: $testdir" >&3 + GITWATCH_PID=$! - # Keeps kill message from printing to screen - disown + # Keeps kill message from printing to screen + disown - # Create a file, verify that it hasn't been added yet, then commit - cd "rem with spaces" + # Create a file, verify that it hasn't been added yet, then commit + cd "rem with spaces" - # 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 + # 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" + # Wait a bit for inotify to figure out the file has changed, and do its add, + # and commit + sleep "$WAITTIME" - # Make a new change - echo "line2" >> file1.txt - sleep "$WAITTIME" + # Make a new change + echo "line2" >> file1.txt + sleep "$WAITTIME" - # Check commit log that the diff is in there - run git log -1 --oneline - [[ $output == *"file1.txt"* ]] + # Check commit log that the diff is in there + run git log -1 --oneline + [[ $output == *"file1.txt"* ]] } - diff --git a/tests/startup-shutdown-spaces.bash b/tests/startup-shutdown-spaces.bash old mode 100755 new mode 100644 diff --git a/tests/startup-shutdown.bash b/tests/startup-shutdown.bash old mode 100755 new mode 100644 diff --git a/tests/status-change.bats b/tests/status-change.bats old mode 100644 new mode 100755 index 7f0524c1..3ea4b8f8 --- a/tests/status-change.bats +++ b/tests/status-change.bats @@ -9,37 +9,35 @@ load startup-shutdown function commit_only_when_git_status_change { #@test - # 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 $WAITTIME - - # Touch the file, but no change - touch file1.txt - sleep $WAITTIME - - echo "hi there" > "$testdir/output.txt" - cat "$testdir/output.txt" - run git log -1 --oneline - echo $output - #run bash -c "grep \"nothing to commit\" $testdir/output.txt | wc -l" - run grep "nothing to commit" $testdir/output.txt - [ $status -ne 0 ] - -} + # 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 $WAITTIME + # Touch the file, but no change + touch file1.txt + sleep $WAITTIME + + echo "hi there" > "$testdir/output.txt" + cat "$testdir/output.txt" + run git log -1 --oneline + echo $output + #run bash -c "grep \"nothing to commit\" $testdir/output.txt | wc -l" + run grep "nothing to commit" $testdir/output.txt + [ $status -ne 0 ] + +} diff --git a/tests/sync.bats b/tests/sync.bats old mode 100644 new mode 100755 index 29dd969d..defaf158 --- a/tests/sync.bats +++ b/tests/sync.bats @@ -8,65 +8,64 @@ load startup-shutdown function syncing_correctly { #@test - # 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>- & - GITWATCH_PID=$! - - # Keeps kill message from printing to screen - disown - - # Create a file, verify that it hasn't been added yet, - # then commit and push - 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, - # commit, and push. - sleep $WAITTIME - - # Verify that push happened - currentcommit=$(git rev-parse master) - remotecommit=$(git rev-parse origin/master) - [ "$currentcommit" = "$remotecommit" ] - - # Try making subdirectory with file - lastcommit=$(git rev-parse master) - mkdir subdir - cd subdir - echo "line2" >> file2.txt - - sleep $WAITTIME - - # Verify that new commit has happened - currentcommit=$(git rev-parse master) - [ "$lastcommit" != "$currentcommit" ] - - # Verify that push happened - currentcommit=$(git rev-parse master) - remotecommit=$(git rev-parse origin/master) - [ "$currentcommit" = "$remotecommit" ] - - - # Try removing file to see if can work - rm file2.txt - sleep $WAITTIME - - # Verify that new commit has happened - currentcommit=$(git rev-parse master) - [ "$lastcommit" != "$currentcommit" ] - - # Verify that push happened - currentcommit=$(git rev-parse master) - remotecommit=$(git rev-parse origin/master) - [ "$currentcommit" = "$remotecommit" ] - - # Remove testing directories - cd /tmp - rm -rf $testdir + # 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>- & + GITWATCH_PID=$! + + # Keeps kill message from printing to screen + disown + + # Create a file, verify that it hasn't been added yet, + # then commit and push + 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, + # commit, and push. + sleep $WAITTIME + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Try making subdirectory with file + lastcommit=$(git rev-parse master) + mkdir subdir + cd subdir + echo "line2" >> file2.txt + + sleep $WAITTIME + + # Verify that new commit has happened + currentcommit=$(git rev-parse master) + [ "$lastcommit" != "$currentcommit" ] + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + + # Try removing file to see if can work + rm file2.txt + sleep $WAITTIME + + # Verify that new commit has happened + currentcommit=$(git rev-parse master) + [ "$lastcommit" != "$currentcommit" ] + + # Verify that push happened + currentcommit=$(git rev-parse master) + remotecommit=$(git rev-parse origin/master) + [ "$currentcommit" = "$remotecommit" ] + + # Remove testing directories + cd /tmp + rm -rf $testdir } - From 0ff0f552707f26716a56ab5dbdd44c50f5ad8cbf Mon Sep 17 00:00:00 2001 From: FieldofClay <7278759+FieldofClay@users.noreply.github.com> Date: Sat, 1 Nov 2025 03:43:47 +1100 Subject: [PATCH 90/95] Add arm image (#150) --- .github/workflows/publish-docker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml index 3823ff4f..3c79e220 100644 --- a/.github/workflows/publish-docker.yml +++ b/.github/workflows/publish-docker.yml @@ -23,6 +23,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . + platforms: linux/amd64,linux/arm64 push: true tags: | ghcr.io/${{ github.repository }}:latest From 1083a4a6a08459a1cbbbf970b0e7339f1a44d280 Mon Sep 17 00:00:00 2001 From: meek2100 Date: Fri, 31 Oct 2025 08:45:14 -0700 Subject: [PATCH 91/95] Add beautysh as dependency. --- .github/workflows/pre-commit.yml | 2 ++ .pre-commit-config.yaml | 1 + 2 files changed, 3 insertions(+) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index abaea9ef..226cdbc7 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -3,6 +3,8 @@ name: pre-commit on: pull_request: push: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: jobs: pre-commit: runs-on: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1fce4d4d..5c1c185b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,6 +22,7 @@ repos: - mdformat-config - mdformat-gfm - setuptools + - beautysh - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: From 505f50ddae62f5b3bfbc68a8f96999163ada8d7d Mon Sep 17 00:00:00 2001 From: meek2100 Date: Fri, 31 Oct 2025 09:51:11 -0700 Subject: [PATCH 92/95] Pin beautysh to v6.2.1 to resolve mdformat ImportError --- .pre-commit-config.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5c1c185b..bb2781c7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,9 @@ repos: - mdformat-config - mdformat-gfm - setuptools - - beautysh + # Pin beautysh==6.2.1. Required by mdformat-beautysh, as + # v6.3.0+ removes the 'Beautify' class and causes an ImportError. + - beautysh==6.2.1 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: From 5dac784258ca3097c297841d52691eb474213957 Mon Sep 17 00:00:00 2001 From: meek2100 <34908880+meek2100@users.noreply.github.com> Date: Tue, 4 Nov 2025 11:58:29 -0800 Subject: [PATCH 93/95] Precommit plugin has been fixed upstream and additional dependencies should no longer be required, also added .gitattributes and enhanced .gitignore. (#153) * Remove pinned version, now that upstream has it pinned in its dependencies. * Test upstream mdformat fix. * Try live test upstream. * revert git since wouldn't work. * Test repo. * Move to new-api branch. * Update mdformat-beautysh target to upstream repo for testing. * Test standard structure. * mdformat-beautysh has been fixed upstream. * Fix lint --- .gitattributes | 46 +++++++++++++++++++++++++ .gitignore | 76 ++++++++++++++++++++++++++++++++++++++--- .pre-commit-config.yaml | 3 -- 3 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..f79260aa --- /dev/null +++ b/.gitattributes @@ -0,0 +1,46 @@ +# ----------------------------------------------------------------- +# gitwatch .gitattributes +# ----------------------------------------------------------------- +# +# See: https://git-scm.com/docs/gitattributes + +# Set the default behavior for all files. +# - text=auto: Git will auto-detect if a file is text or binary. +# - eol=lf: Force all text files to use Unix-style (LF) line endings +# on checkout. This is CRITICAL for shell scripts. +* text=auto eol=lf + +# --- Shell Scripts (Must be LF) --- +# Explicitly enforce LF for all shell scripts, as CRLF will break them. +# Also, correctly identify .bats files as Shell for GitHub's linguist. +gitwatch.sh linguist-language=Shell +entrypoint.sh linguist-language=Shell +*.sh linguist-language=Shell +*.bash linguist-language=Shell +*.bats linguist-language=Shell + +# --- Windows Scripts (Must be CRLF) --- +# Override the 'eol=lf' default and enforce CRLF for Windows-native +# scripts, as cmd.exe requires them. +*.bat eol=crlf linguist-language=Batchfile +*.ps1 eol=crlf linguist-language=PowerShell + +# --- Build & Config Files --- +# Identify specific file types for better language statistics and diffs. +Makefile linguist-language=Makefile +Dockerfile linguist-language=Dockerfile +*.nix linguist-language=Nix + +# --- Generated Files --- +# Mark generated lock files to hide them from diffs by default on GitHub. +flake.lock linguist-generated=true + +# --- Documentation / Text --- +# These are already covered by the '*' rule, but are here for clarity. +*.md +*.txt +*.yaml +*.json +.editorconfig +.gitignore +.shellcheckrc diff --git a/.gitignore b/.gitignore index 8b5f7f2a..40ba0653 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,77 @@ -#Directories +# =================================================================== +# IDE & Editor Configuration +# +# Ignore user-specific settings, but force-include +# tracked workspace recommendations. +# =================================================================== +.vscode/* +!.vscode/extensions.json +!.vscode/settings.json + + +# =================================================================== +# Dependencies +# =================================================================== node_modules/ -#Archives +tests/bats-assert +tests/bats-file +tests/bats-support + +# =================================================================== +# Build & Packaging Artifacts +# =================================================================== +# Nix build results +result +result-* + +# Archives *.gz *.tar *.zip -#Backup files + + +# =================================================================== +# Testing & Local Artifacts +# =================================================================== +# General logs and temporary files +*.log +*.tmp + +# BATS test artifacts (e.g., mktemp output.XXXXX) +*.XXXXX + +# Docker-compose test volume +watched-repo/ + +# Folders excluded from pre-commit, likely for demo/debug +demo/ +debug/ + + +# =================================================================== +# Caches +# =================================================================== +.pre-commit-cache/ + + +# =================================================================== +# OS & Editor Backups +# =================================================================== +# macOS +.DS_Store + +# Windows +Thumbs.db + +# Vim/Editor backups +*~ .*.swp -*.bk *.bak -*~ +*.bk + + +# =================================================================== +# Local Environment +# =================================================================== +# Docker environment variables (often contain secrets) +.env diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bb2781c7..1fce4d4d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,9 +22,6 @@ repos: - mdformat-config - mdformat-gfm - setuptools - # Pin beautysh==6.2.1. Required by mdformat-beautysh, as - # v6.3.0+ removes the 'Beautify' class and causes an ImportError. - - beautysh==6.2.1 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 hooks: From fca05d782214f17072d631afdf8cf6bd235ac4d4 Mon Sep 17 00:00:00 2001 From: mdjdev <18183474+mdjdev@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:07:18 +0100 Subject: [PATCH 94/95] Add git_dir option to docker-compose setup (#154) --- README.md | 27 ++++++++++++++------------- docker-compose.yml | 11 +++++++++-- entrypoint.sh | 5 +++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f4591325..11643804 100644 --- a/README.md +++ b/README.md @@ -171,19 +171,20 @@ Here's a breakdown of the important parts of the `docker-compose.yml` file: The following environment variables are available for configuring the `gitwatch` container: -| Variable | Default Value | Description | -| :----------------- | :--------------------- | :---------------------------------------------------------------------------------------------------------------------------- | -| `GIT_WATCH_DIR` | `/app/watched-repo` | The directory inside the container to watch for changes. This must match the container path you set in the `volumes` section. | -| `GIT_REMOTE` | `origin` | The name of the remote repository to push to. | -| `GIT_BRANCH` | `main` | The branch to push to. | -| `PULL_BEFORE_PUSH` | `"false"` | Set to `"true"` to run `git pull --rebase` before every push. | -| `SLEEP_TIME` | `2` | Time in seconds to wait after a file change before committing. | -| `COMMIT_MSG` | `"Auto-commit: %d"` | The commit message format. `%d` is replaced with the date/time. | -| `DATE_FMT` | `"+%Y-%m-%d %H:%M:%S"` | The date format used in the commit message (see `man date` for options). | -| `EXCLUDE_PATTERN` | `""` | A comma-separated list of patterns to exclude from monitoring (e.g., `"*.log, *.tmp, tmp/"`). | -| `SKIP_IF_MERGING` | `"false"` | Set to `"true"` to prevent commits when a merge is in progress. | -| `COMMIT_ON_START` | `"false"` | Set to "true" to commit any pending changes on startup. | -| `VERBOSE` | `"false"` | Set to "true" to enable verbose output for debugging. | +| Variable | Default Value | Description | +| :----------------- | :--------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `GIT_WATCH_DIR` | `/app/watched-repo` | The directory inside the container to watch for changes. This must match the container path you set in the `volumes` section. | +| `GIT_DIR` | `/app/.git` | Optional, but required if the .git directory is not located under `$GIT_WATCH_DIR/.git`. If used, this must match the container path you set in the `volumes` section. | +| `GIT_REMOTE` | `origin` | The name of the remote repository to push to. | +| `GIT_BRANCH` | `main` | The branch to push to. | +| `PULL_BEFORE_PUSH` | `"false"` | Set to `"true"` to run `git pull --rebase` before every push. | +| `SLEEP_TIME` | `2` | Time in seconds to wait after a file change before committing. | +| `COMMIT_MSG` | `"Auto-commit: %d"` | The commit message format. `%d` is replaced with the date/time. | +| `DATE_FMT` | `"+%Y-%m-%d %H:%M:%S"` | The date format used in the commit message (see `man date` for options). | +| `EXCLUDE_PATTERN` | `""` | A comma-separated list of patterns to exclude from monitoring (e.g., `"*.log, *.tmp, tmp/"`). | +| `SKIP_IF_MERGING` | `"false"` | Set to `"true"` to prevent commits when a merge is in progress. | +| `COMMIT_ON_START` | `"false"` | Set to "true" to commit any pending changes on startup. | +| `VERBOSE` | `"false"` | Set to "true" to enable verbose output for debugging. | diff --git a/docker-compose.yml b/docker-compose.yml index fbf9db19..aaa98567 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,10 +6,13 @@ services: image: ghcr.io/gitwatch/gitwatch:latest restart: always volumes: - # The local directory on your machine that you want to watch + # The local directory on your machine that you want to watch (see GIT_WATCH_DIR in the 'environment' section below) # IMPORTANT: You must create this directory first (e.g., ./my-notes-repo) - ./watched-repo:/app/watched-repo + # Optional: Use a separate Git directory (see GIT_DIR in the 'environment' section below) + # - ~/.git:/app/.git + # Mount your SSH key for pushing to remote repositories - ~/.ssh/id_rsa:/root/.ssh/id_rsa:ro @@ -17,9 +20,13 @@ services: - ~/.gitconfig:/root/.gitconfig:ro environment: # --- Watched Directory --- - # This should match the container path in the 'volumes' section above + # This should match the watch path in the container from the 'volumes' section above GIT_WATCH_DIR: /app/watched-repo + # --- Git Directory (optional) --- + # If set, this should match the .git dir path in the container from the 'volumes' section above + # GIT_DIR: /app/.git + # --- Git Remote Configuration --- # The remote repository to push to (e.g., 'origin') GIT_REMOTE: origin diff --git a/entrypoint.sh b/entrypoint.sh index 0dd28dc3..0cceef82 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,6 +7,7 @@ set -e # Target directory to watch GIT_WATCH_DIR=${GIT_WATCH_DIR:-/app/watched-repo} +GIT_DIR="${GIT_DIR:-}" # Git options GIT_REMOTE=${GIT_REMOTE:-origin} @@ -38,6 +39,10 @@ cmd+=( -s "${SLEEP_TIME}" ) cmd+=( -m "${COMMIT_MSG}" ) cmd+=( -d "${DATE_FMT}" ) +if [ -n "${GIT_DIR}" ]; then + cmd+=( -g "${GIT_DIR}" ) +fi + # --- Convert User-Friendly Exclude Pattern to Regex --- if [ -n "${USER_EXCLUDE_PATTERN}" ]; then # 1. Replace commas with spaces to treat as separate words. From 2947a8dd2df8c291207a60a600248295856d5de1 Mon Sep 17 00:00:00 2001 From: mdjdev <18183474+mdjdev@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:07:04 +0100 Subject: [PATCH 95/95] Improve Docker image tagging workflow (#155) Add SHA (full and short) tags for every build. Update a moving master tag for branch pushes. Add v* release and update latest tags. --- .github/workflows/publish-docker.yml | 36 ++++++++++++++++++++++++---- README.md | 30 +++++++++++++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml index 3c79e220..2377c3ae 100644 --- a/.github/workflows/publish-docker.yml +++ b/.github/workflows/publish-docker.yml @@ -1,7 +1,15 @@ --- name: Docker Build and Publish -# Allows you to run this workflow manually from the Actions tab -on: workflow_dispatch + +on: + push: + tags: + - "v*" + branches: + - master + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + jobs: build-and-push: runs-on: ubuntu-latest @@ -19,12 +27,30 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Generate Docker metadata + id: metadata + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + # Always add sha tags + type=sha,format=long,prefix= + type=sha,format=short,prefix= + + # Moving master branch tag (only on master pushes) + type=ref,event=branch,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }},prefix=,suffix=,pattern=master + + # Tag release versions + latest only when a vX.Y tag is present (push tag or manual run) + type=ref,event=tag,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=match,pattern=v(.*),group=1,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + flavor: | + latest=false - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . platforms: linux/amd64,linux/arm64 push: true - tags: | - ghcr.io/${{ github.repository }}:latest - ghcr.io/${{ github.repository }}:${{ github.sha }} + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} diff --git a/README.md b/README.md index 11643804..77de0815 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,27 @@ The easiest way to run `gitwatch` with Docker is by using the provided - **SSH Key**: For pushing to a remote repository, the container needs access to an SSH key that is authorized with your Git provider. -**2. Configuration:** +**2. Docker Image Tags:** + +This project publishes multiple Docker tags so you can choose the one that +best fits your use case. + +The following image tags are available: + +- `:`, `:`\ + Always published for every build (immutable). + +- `:master`\ + Moving tag for the latest image from `master` branch. + +- `:vX.Y`, `:X.Y`\ + Published when a release tag `vX.Y` is pushed. + +- `:latest`\ + Updated when a `vX.Y` tag is pushed, pointing to the newest release + image. + +**3. Configuration:** The `docker-compose.yml` file is configured using environment variables. You can either edit the `environment` section directly in the file or @@ -165,8 +185,10 @@ Here's a breakdown of the important parts of the `docker-compose.yml` file: into the container. This ensures that the commits made by `gitwatch` are attributed to you with the correct name and email. - **`environment`**: This section controls how `gitwatch` behaves. - - **3. Environment Variables** + + + +**4. Environment Variables** The following environment variables are available for configuring the `gitwatch` container: @@ -188,7 +210,7 @@ The following environment variables are available for configuring the -**4. Running gitwatch:** +**5. Running gitwatch:** - **Start the container** in the background (detached mode):