From 35987b5cf2dc4538951ab87208eabfe76635a1f4 Mon Sep 17 00:00:00 2001 From: hesk <24hesk@gmail.com> Date: Tue, 8 Dec 2020 22:23:35 +0100 Subject: [PATCH 1/4] Fix: exclude .git repository on macOS --- gitwatch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index 6f7de9a..0869b88 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -206,7 +206,7 @@ if [ -d "$1" ]; then # if the target is a directory 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" "--exclude" "'(\.git/|\.git$)'" "\"$TARGETDIR\"") + 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 From 32bf8872a008fb418c8ce8a466a4e2c6795e1f68 Mon Sep 17 00:00:00 2001 From: hesk <24hesk@gmail.com> Date: Tue, 8 Dec 2020 22:25:15 +0100 Subject: [PATCH 2/4] Add ability to configure path for readline --- gitwatch.sh | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 0869b88..92dc698 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -97,12 +97,12 @@ 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\" and \"inotifywait\"," - echo "expecting to find them in the PATH (it uses 'which' to check this and will" - echo "abort with an error if they cannot be found). If you want to use binaries" - echo "that are named differently and/or located outside of your PATH, you can define" - echo "replacements in the environment variables GW_GIT_BIN and GW_INW_BIN for git" - echo "and inotifywait, respectively." + 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 @@ -149,7 +149,7 @@ if [ $# -ne 1 ]; then # If no command line arguments are left (that's bad: no ta exit # and exit fi -# if custom bin names are given for git or inotifywait, use those; otherwise fall back to "git" and "inotifywait" +# 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 @@ -169,6 +169,8 @@ else 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 1; } @@ -184,15 +186,15 @@ 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=$(readlink -f "$1") + IN=$($RL -f "$1") else if is_command "greadlink"; then IN=$(greadlink -f "$1") else - IN=$(readlink -f "$1") + IN=$($RL -f "$1") if [ $? -eq 1 ]; then echo "Seems like your readlink doesn't support '-f'. Running without. Please 'brew install coreutils'." - IN=$(readlink "$1") + IN=$($RL "$1") fi fi; fi; From 255d385a175d973126788c7396653a81b726841a Mon Sep 17 00:00:00 2001 From: hesk <24hesk@gmail.com> Date: Tue, 8 Dec 2020 22:26:07 +0100 Subject: [PATCH 3/4] Use different exit code for each error condition --- gitwatch.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gitwatch.sh b/gitwatch.sh index 92dc698..100ca57 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -173,7 +173,7 @@ 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 1; } + is_command "$cmd" || { stderr "Error: Required command '$cmd' not found." ; exit 2; } done unset cmd @@ -227,7 +227,7 @@ elif [ -f "$1" ]; then # if the target is a single file 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 1 + exit 3 fi # If $GIT_DIR is set, verify that it is a directory, and then add parameters to @@ -236,7 +236,7 @@ if [ -n "$GIT_DIR" ]; then if [ ! -d "$GIT_DIR" ]; then stderr ".git location is not a directory: $GIT_DIR"; - exit 1; + exit 4; fi GIT="$GIT --no-pager --work-tree $TARGETDIR --git-dir $GIT_DIR" @@ -249,7 +249,7 @@ if ! grep "%d" > /dev/null <<< "$COMMITMSG"; then # if commitmsg didn't contain fi # CD into right dir -cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 1; } +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 ? @@ -339,7 +339,7 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do fi # CD into right dir - cd "$TARGETDIR" || { stderr "Error: Can't change directory to '${TARGETDIR}'." ; exit 1; } + 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 From 2eec9bfbb2828c208495cb8768d87bb9d3db7904 Mon Sep 17 00:00:00 2001 From: hesk <24hesk@gmail.com> Date: Tue, 8 Dec 2020 22:48:52 +0100 Subject: [PATCH 4/4] Example launchd script --- com.github.gitwatch.plist | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 com.github.gitwatch.plist diff --git a/com.github.gitwatch.plist b/com.github.gitwatch.plist new file mode 100644 index 0000000..65230e3 --- /dev/null +++ b/com.github.gitwatch.plist @@ -0,0 +1,34 @@ + + + + + Label + com.github.gitwatch + ProgramArguments + + /usr/local/bin/gitwatch.sh + -s + 60 + -m + Auto-commit %d + /path/to/watch + + RunAtLoad + + EnvironmentVariables + + GW_GIT_BIN + /usr/local/bin/git + GW_INW_BIN + /usr/local/bin/fswatch + GW_RL_BIN + /usr/local/bin/greadlink + + + +