task: Add new git backup hook for version 3

First exports tasks to plaintext before backing them up with git.
This commit is contained in:
Marty Oehme 2025-03-10 20:17:42 +01:00
parent 41754c5d24
commit c6de9b0686
Signed by: Marty
GPG key ID: 4E535BC19C61886E
3 changed files with 65 additions and 68 deletions

View file

@ -1,29 +0,0 @@
#!/usr/bin/env python3
# From: https://gist.github.com/varunagrawal/2b93c5dc721520ff6876e940c420ab05
# This hooks script syncs task warrior to the configured task server.
# The on-exit event is triggered once, after all processing is complete.
# Make sure hooks are enabled and this hook script is executable.
# Run `task diag` for diagnostics on the hook.
import sys
import json
import subprocess
try:
tasks = json.loads(sys.stdin.readline())
except:
# No input
sys.exit(0)
# no tasks to work through, don't error
if len(tasks) <= 0:
sys.exit(0)
# Call the `sync` command
# hooks=0 ensures that the sync command doesn't call the on-exit hook
# verbose=nothing sets the verbosity to print nothing at all
subprocess.Popen(["task", "rc.verbose=nothing", "rc.hooks=0", "sync"], close_fds=True)
sys.exit(0)

View file

@ -0,0 +1,65 @@
#!/bin/sh
# Automatically git commits, pushes and pulls if doable in the taskwarrior data directory
#
# Much of this taken from: https://github.com/mrschyte/taskwarrior-hooks/
# with much gratitude
#
# The minimum amount of time required between 2 commits in seconds.
# So only if the last commit is at least x seconds old will a new one
# be created. Set to 0 to sync each taskwarrior change.
MINIMUM_WAIT_TIME=600
# Do not display status information.
QUIET=true
# Removes the tasks.json file after each run, keeping the
# task directory clean.
REMOVE_JSON=false
if [ "${DISABLE_HOOKS}" = "true" ] || ! command -v git >/dev/null 2>&1; then
exit 0;
fi
if [ "$1" != "api:2" ]; then
printf "Taskwarrior uses different data API version than git plugin. Aborting!" 1>&2
exit 1
fi
data_dir="$(echo "$5" | cut -f2 -d:)"
command_run="$(echo "$3" | cut -f2 -d:)"
if [ "$command_run" = "synchronize" ]; then
DISABLE_HOOKS=true env task sync
git -C "$data_dir" pull >/dev/null 2>&1
pull_ret="$?"
git -C "$data_dir" push >/dev/null 2>&1
push_ret="$?"
if [ "$pull_ret" -eq 0 ] && [ "$push_ret" -eq 0 ]; then
[ $QUIET = "true" ] || echo "Git upstream synchronized."
fi
fi
last_commit=$(git -C "$data_dir" log -1 --format="%at")
# if now is not yet greater than last commit + wait time do nothing
if [ "$(date "+%s")" -lt $((last_commit + MINIMUM_WAIT_TIME)) ]; then
# TODO: Implement DEBUG msg level (info/debug) system
# echo "Too early to check for changes, exiting."
exit 0
fi
# echo "EXPORTING TASKS"
DISABLE_HOOKS=true env task export > "$data_dir/tasks.json"
# after any command, if there's changes add and commit
if ! git -C "$data_dir" diff --exit-code >/dev/null 2>&1; then
# echo "found changes"
# need to run to fully update tasks that just got done
DISABLE_HOOKS=true env task next >/dev/null 2>&1
header="auto: ${2##* }"
msg="full command: $2"
git -C "$data_dir" commit "$data_dir/tasks.json" -m "$header" -m "$msg" --no-gpg-sign >/dev/null 2>&1
[ $QUIET = "true" ] || echo "Backup up to git."
fi
[ "$REMOVE_JSON" = true ] && rm "$data_dir/tasks.json" >/dev/null 2>&1

View file

@ -1,39 +0,0 @@
#!/bin/sh
# Automatically git commits, pushes and pulls if doable in the taskwarrior data directory
#
# Much of this taken from: https://github.com/mrschyte/taskwarrior-hooks/
# with much gratitude
if [ "${DISABLE_HOOKS}" = "true" ] || ! command -v git >/dev/null 2>&1; then
exit 0;
fi
if [ "$1" != "api:2" ]; then
printf "Taskwarrior uses different data API version than git plugin. Aborting!" 1>&2
exit 1
fi
data_dir="$(echo "$5" | cut -f2 -d:)"
command_run="$(echo "$3" | cut -f2 -d:)"
# after any command, if there's changes add and commit
if ! git -C "$data_dir" diff --exit-code >/dev/null 2>&1; then
# need to run to fully update tasks that just got done
DISABLE_HOOKS=true env task next >/dev/null 2>&1
header="auto: ${2##* }"
msg="full command: $2"
git -C "$data_dir" commit -a -m "$header" -m "$msg" --no-gpg-sign >/dev/null 2>&1
fi
if [ "$command_run" = "synchronize" ]; then
DISABLE_HOOKS=true env task sync
git -C "$data_dir" pull >/dev/null 2>&1
pull_ret="$?"
git -C "$data_dir" push >/dev/null 2>&1
push_ret="$?"
if [ "$pull_ret" -eq 0 ] && [ "$push_ret" -eq 0 ]; then
echo Git upstream synchronized.
fi
fi