@@ 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] <file>
@@ 65,12 89,12 @@ function merge() {
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 @@ function merge() {
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 @@ function mergeBased() {
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 @@ fs=(${ROOT}.sync-conflict*.${SUFF}(N))
[[ -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