Search code examples
emacspre-commit.commagit

Magit: show buffer with pre-commit hooks output on error


This is similar to Magit : how to see pre-commit hooks before editing commit log?

I use a package called pre-commit (unfortunate name) that runs several checks on git's pre-commit hook. When these checks fail, I can see their output in magit process buffer if I type $ (in magit status buffer).

My question is if there is way to automatically bring up either magit process buffer or a dedicate buffer with the errors (preferred).

As an added bonus, the pre-commit output is colored, but the colors do not show in the process buffer. Is it possible to maintain the color?


Solution

  • In the end I wrote a function to run the same script configured in git's pre-commit hook. It is a bit dirty, because this function doesn't really know about git's hook, it just runs some process and displays the output in case of error. Also, I think that the commit-hook is now run one more time: by this function, by magit just before starting the commit message and by git itself just before the actual commit.

    (defun run-pre-commit ()
      "Run `pre-commit`, collect output and, in case of errors, raise a
    buffer with the collected output."
      (let ((pre-commit-buffer (get-buffer-create "*pre-commit*")))
        ;; TODO: maybe set default-directory?
        (if (not (zerop (call-process "pre-commit" nil pre-commit-buffer nil "run" "--color" "always")))
            (let ((display-buffer-alist '((".*" display-buffer-below-selected))))
              (with-current-buffer pre-commit-buffer
                (ansi-color-apply-on-region (point-min) (point-max)))
              (display-buffer pre-commit-buffer )))))
    
    (add-hook 'magit-pre-start-git-hook #'run-pre-commit)