# HG changeset patch # User Sean E. Russell # Date 1698346572 18000 # Thu Oct 26 13:56:12 2023 -0500 # Node ID 6f5e8c0a419991247a4a54533802bd0cd94d4591 # Parent 972d9ff36bf2ba0f015cdc74f2c8adf756e07c6b Makes the script work with both tempname and mktemp; switches to fsnotify diff --git a/synctodo b/synctodo --- a/synctodo +++ b/synctodo @@ -1,7 +1,31 @@ #!/usr/bin/zsh +# Cascade merges todotxt SyncThing conflicts. Watches a SINGLE file. +# Call -h for usage. +# +# Depends on +# - Either mktemp (Arch) or tempname (Debian) +# - getopt (thanks, Void, for the abject lesson) +# - todotxt-merge (https://hg.sr.ht/~ser/todotxt-merge) +# - fsnotify (https://github.com/fsnotify/fsnotify) +# - zsh, because it's the best shell STTDIR=$XDG_CACHE_HOME/synctodo -backupdir=$(tempname -d $STTDIR -D backup) + +# There really should be standards for crap like this +if [[ -n $(command -v mktemp) ]]; then + func myMkTmp() { + mktemp --tempdir "$1" + } + backupdir=$(mktemp -p $STTDIR -d) +elif [[ -n $(command -v tempname) ]]; then + func myMkTmp() { + tempname -d /tmp -D "$1" + } + backupdir=$(tempname -d $STTDIR -D backup) +else + printf "this script needs either tempname or mktemp, and can't find either\n" + exit 1 +fi function usage() { printf "USAGE: synctodo [-dh] @@ -65,12 +89,12 @@ local onetime=1 ;; 2) - local tmpfile=$(tempname -d /tmp mergefile) + local tmpfile=$(myMkTmp mergefile) todotxt-merge ${@[1]} ${@[2]} > $tmpfile print $tmpfile ;; *) - local tmpfile=$(tempname -d /tmp mergefile) + local tmpfile=$(myMkTmp mergefile) todotxt-merge ${@[1]} ${@[2]} > $tmpfile shift 2 merge $tmpfile $@ @@ -79,7 +103,11 @@ esac } +# Merges all of the sync conflicts for a file into the file function mergeBased() { + # Collect all of the sync conflicts; syncthing conflict files + # are named in dictionary order, and zsh globs in dictionary + # order, so the sync conflicts should be in order. CONFL=(${ROOT}.sync-conflict*.${SUFF}) RES=$(merge $CONFL $FILE) # cleanup @@ -91,12 +119,6 @@ set +e } -## TESTING. Expects some files to merge in the test directory -#K=($(ls ~/tmp/testmerge/*)) -#O=$(merge $K) -#echo move $O to OUTPUT -#exit 0 - mkdir -p $STTDIR printf "Backups will be in %s\n" "$backupdir" @@ -108,10 +130,9 @@ [[ -n $onetime ]] && exit 0 printf "Watching %s" "$FILE" -fswatch -l 5 --event Created --event IsFile "$(dirname $FILE)" | \ +fsnotify watch "$(dirname $FILE)" |\ while read -r event ; do - # because of the fswatch bug, we have to check this the hard way - echo "$event" | grep -qP "^${ROOT}\.sync-conflict.*\.${SUFF}$" + echo "$event" | grep -qP "CREATE\s+\"${ROOT}\.sync-conflict.*\.${SUFF}\"$" if [[ $? -eq 0 ]]; then mergeBased fi