From a91f553f58d486e1480434ff6fba24995c87d846 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 4 Feb 2024 21:26:13 +0100 Subject: [PATCH] task: Add git-sync script hook Synchronizes a git repository for the taskwarrior data directory, automatically committing any changes after each command; and pushing and pulling on syncing taskwarrior. --- .../.local/share/task/hooks/on-exit.git-sync | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 office/.local/share/task/hooks/on-exit.git-sync diff --git a/office/.local/share/task/hooks/on-exit.git-sync b/office/.local/share/task/hooks/on-exit.git-sync new file mode 100755 index 0000000..9de25c4 --- /dev/null +++ b/office/.local/share/task/hooks/on-exit.git-sync @@ -0,0 +1,39 @@ +#!/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