2

Sometimes I am interrupted while coding and forget to commit. I would like to be warned about this next time I open a file with uncommited changes.

I tried

(defun my/magit-check-file-and-popup (file)
  "If the file is version controlled with git and has uncommitted
changes, open the magit status popup."
  (require 'magit-core)
  (when (and file (magit-anything-modified-p t file))
    (magit-status)))

(add-hook 'after-load-functions 'my/magit-check-file-and-popup)

which worked for a while, but after recent package updates it goes into recursive require, so I guess it is not the right approach. Using Emacs 25.2.2 if that matters, and latest magit from MELPA.

3
  • Maybe just use (require 'magit)?
    – npostavs
    Jan 3, 2018 at 13:06
  • Unfortunately that does not help, but I managed to find a fix (I will edit the question now).
    – Tamas Papp
    Jan 4, 2018 at 16:39
  • 2
    @TamasPapp Instead of answering your question within the description please write up your own answer and accept it after the grace period. That is important to mark the question as answered. Thanks in advance.
    – Tobias
    Jan 4, 2018 at 22:46

1 Answer 1

3

It turns out that due to a recent change in magit, I now need to (require 'magit-git), not 'magit-core. Also, find-file-hook avoids the recursion problem. The following works:

(defun tkpapp/magit-check-file-and-popup ()
  "If the file is version controlled with git and has uncommitted
changes, open the magit status popup."
  (let ((file (buffer-file-name)))
    (when (and file (magit-anything-modified-p t file))
      (magit-status))))

(require 'magit)
(require 'magit-git)
(add-hook 'find-file-hook 'tkpapp/magit-check-file-and-popup)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.