Special-Casing Added Files With diff-hl-mode
diff-hl-mode is a very nice Emacs package - it marks added, deleted and changed lines in the
gutter. However, it is a bit annoying when you're in a newly staged and uncommitted file, since every line is
considered added, and with my usual themes that means marking every line with a fairly bright green. It
doesn't seem to have a configuration option for changing that behaviour, but because it's Elisp, you can just
advise a function:
(defun rweir/diff-hl-ignore-added-files (orig-fn &rest args)
"Return no hunks for a newly added (staged, uncommitted) file.
Without this, diff-hl marks every line of such a file as inserted."
(let ((file (diff-hl--buffer-file-name)))
(unless (and file (eq (vc-state file) 'added))
(apply orig-fn args))))
(advice-add 'diff-hl-changes :around #'rweir/diff-hl-ignore-added-files)
Once it's committed, the gutter will once again show modified lines. A nice bit of Elisp design from the diff-hl author -
diff-hl-changes handles producing the changes for a single buffer, so to ignore the changes for a
buffer we simply wrap it and don't call it.