Skip to content

Instantly share code, notes, and snippets.

@RickCogley
Forked from wbsch/on-exit_git.py
Last active August 10, 2021 15:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickCogley/1893227ea64ed5d020aa7398a0419820 to your computer and use it in GitHub Desktop.
Save RickCogley/1893227ea64ed5d020aa7398a0419820 to your computer and use it in GitHub Desktop.
Proof of concept for a Taskwarrior on-exit hook that manages a git repository in ~/.task
#!/usr/bin/env python
#
# PoC: Manage a git repository in ~/.task that gets updated on changes.
# Only pending.data and completed.data are included by default.
# You can use "git add" to add whatever files you want to track in your
# task folder.
#
# Inspired by https://gist.github.com/Unode/9366218
#
# Works with any Taskwarrior version that supports hooks.
# Needs Taskwarrior 2.4.3 in order to show fancy commit messages.
#
# NOTE: Little to no error handling. This is a PoC, remember?
# RickCogley notes:
# - script has to be named on-exit_whatever.py, and apparently it can be any script language, not just python
# - commented the check if there's a .git folder already
# - install under .task/hooks, and make executable with chmod +x (mac, *nix)
# - assumes all files are to be added (or not, via .gitignore) and just goes ahead and adds
# - remote origin set up so it does "git push origin master" assuming you're just using the master branch
import os
import subprocess
import sys
TASK_DIR = os.path.dirname(__file__) + os.sep + os.pardir
c = {}
if len(sys.argv) > 1:
c = {k:v for k, v in (s.split(":", 1) for s in sys.argv[1:])}
if 'args' not in c:
c['args'] = "Taskwarrior version too old, no info available."
os.chdir(TASK_DIR)
#if not os.path.isdir(TASK_DIR + os.sep + ".git"):
# subprocess.call("git init".split())
# with open(".gitignore", "w") as f:
# f.write("*\n")
# subprocess.call("git add -f .gitignore pending.data completed.data".split())
# subprocess.call(["git", "commit", "-mInitial log"])
if subprocess.call("git diff --exit-code --quiet".split()) != 0:
subprocess.call("git add --all".split())
subprocess.call("git commit -a".split() + ["-m" + c['args']])
subprocess.call("git push origin master".split())
@RickCogley
Copy link
Author

Working fine as of taskwarrior 2.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment