In the road to track more effectively my habits, and in the context of this article, my programming habits I put together a simple system for tracking the git commits I do on the various projects I'm working on. As always emacs, magit and org-mode make this pretty easy, and that's the reason that I'm so heavily invested in emacs.

For the implementation of the above goal I use the orgit library that enhances the org-link system with links for views in magit. This means that we can create hyperlinks in org-mode documents that lead to specific magit buffers, hence creating links to descriptions of commits, branches, revision etc.

To do so I configured the orgit package, using use-package:

(use-package orgit
:ensure t
:hook (git-commit-post-finish . orgit-capture-after-commit)
:config
(defun orgit-capture-after-commit ()
  "Capture the commit into an org-mode file, decided in the approptriate capture template"
  (let* ((repo (abbreviate-file-name default-directory))
         (rev (magit-git-string "rev-parse" "HEAD"))
         (link (format "orgit-rev:%s::%s" repo rev))
         (summary (substring-no-properties (magit-format-rev-summary rev)))
         (desc (format "%s (%s)" summary repo)))
    (kill-new (format "[[%s][%s]]" link desc))
    (org-capture nil "g"))))

The important thing here is that we added a hook on finishing a commit, that collects info for the commit, pushes a orgit link for it in the kill-ring and then invokes org-capture. The relevant template under the shortcut "g" is:

("g" "Git commit" entry (file ,org-default-commit-file)
         "* %c \n:PROPERTIES:\n:CLOSED: %T\n:END:\n\n"
         :immediate-finish t)

Briefly, this template takes the first element of the kill-ring and creates a new entry in the org-default-commit-file with a CLOSED property and an active timestamp. This is important in order to have them showing up in the agenda, which also requires to put the org-default-commit-file in the agenda files.

Now every commit I make in any repo in my computer, using magit, will create an entry like this:

2021-08-30_12-47-24_screenshot.png

And at the same time I can review all the commits I've done in the agenda:

2021-08-30_12-48-31_screenshot.png