Emacs, scripting and anything text oriented.

Narrowing the Author column in Magit

The Org and Worg git repos can be committed to only via ssh protocol (and not https), and for technical reasons, I cannot commit to those repos via ssh from my primary development machine. So I ended up with a flow that involves pushing commits to those repos using my Nexus 6p phone.

Magit is how I git, and I love to primarily work from the ∗magit-log∗ buffer (M-x magit-status, l b). But the default column widths were not optimal on a Nexus 6p 5.7" screen. And that’s what inspired this tweak, which looks great on a regular desktop monitor too.

Before I jump to the code, you can see in the below figure how the author and commit age columns took up roughly half the screen width on my phone before the tweak (left), and how much easier it is to read the commit messages after the tweak (right).

Org-mode Worg commit log : Before (Left), After (Right)

Org-mode Worg commit log : Before (Left), After (Right)

So the Magit Log columns tweaks are basically:

  1. Abbreviate the authors’ first names to just their initials.
  2. Abbreviate the commit ages to 1-character time units.

To implement those tweaks, I started digging through magit-log.el and found that I would need to customize (i) the magit-log-margin variable and (ii) magit-log-format-margin function.

  • Customizing magit-log-margin variable allows me to use the abbreviated age instead of the verbose age strings (minute→m, hour→h, day→d, week→w, month→M, year→Y). It’s also where I specify how wide I want the author column to be.. I can now reduce that column width from the default value of 18 to 11 as I am abbreviating the author name too.
  • Reviewing the magit-log.el code, I realized that I also need to advise the magit-log-format-margin function to implement the author name abbreviation.

In the below code snippet, I am using use-package1 to customize that variable and function in magit-log.

(use-package magit-log
  :init
  (progn
    ;; (setq magit-log-margin '(t age magit-log-margin-width t 18)) ;Default value
    (setq magit-log-margin '(t age-abbreviated magit-log-margin-width :author 11)))
  :config
  (progn
    ;; Abbreviate author name. I added this so that I can view Magit log without
    ;; too much commit message truncation even on narrow screens (like on phone).
    (defun modi/magit-log--abbreviate-author (&rest args)
      "The first arg is AUTHOR, abbreviate it.
First Last  -> F Last
First.Last  -> F Last
Last, First -> F Last
First       -> First (no change).

It is assumed that the author has only one or two names."
      ;; ARGS             -> '((AUTHOR DATE))
      ;; (car ARGS)       -> '(AUTHOR DATE)
      ;; (car (car ARGS)) -> AUTHOR
      (let* ((author (car (car args)))
             (author-abbr (if (string-match-p "," author)
                              ;; Last, First -> F Last
                              (replace-regexp-in-string "\\(.*?\\), *\\(.\\).*" "\\2 \\1" author)
                            ;; First Last -> F Last
                            (replace-regexp-in-string "\\(.\\).*?[. ]+\\(.*\\)" "\\1 \\2" author))))
        (setf (car (car args)) author-abbr))
      (car args))                       ;'(AUTHOR-ABBR DATE)
    (advice-add 'magit-log-format-margin :filter-args #'modi/magit-log--abbreviate-author)))

NOTES

  • I need to set the magit-log-margin value in the :init block because that variable dynamically sets many other variables in magit-log at the time of loading. So we cannot wait for the whole magit-log to load before setting that variable. I wouldn’t need to worry about this if I were using the Emacs Customize interface to set this variable.
  • I use modi/magit-log--abbreviate-author to add a :filter-args advice to magit-log-format-margin. The signature of the advised function is (magit-log-format-margin AUTHOR DATE). So the advice basically replaces the AUTHOR arg with its abbreviated form as explained in the modi/magit-log--abbreviate-author doc-string.

  1. Feel free to ask for more explanation in comments in the case you don’t use use-package. [return]

Comments

comments powered by Disqus