dotfiles/office/.local/share/task/hooks/on-exit.git-sync

40 lines
1.2 KiB
Text
Raw Permalink Normal View History

#!/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