Lazily revert affected buffers after magit

This commit is contained in:
Henrik Lissner 2020-02-21 13:40:14 -05:00
parent 900ec70ee3
commit 731e1c503d
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 36 additions and 25 deletions

View file

@ -41,28 +41,37 @@
;; ;;
;; Commands ;;; Auto-revert
(defun +magit--refresh-vc-in-buffer (buffer) (defvar-local +magit--stale-p nil)
(defun +magit--revert-buffer (buffer)
(with-current-buffer buffer (with-current-buffer buffer
(when (and vc-mode (fboundp 'vc-refresh-state)) (setq +magit--stale-p nil)
(vc-refresh-state)) (revert-buffer t (not (buffer-modified-p)))))
(when (and (bound-and-true-p git-gutter-mode)
(fboundp '+version-control|update-git-gutter))
(+version-control|update-git-gutter))
(setq +magit--vc-is-stale-p nil)))
;;;###autoload ;;;###autoload
(defvar-local +magit--vc-is-stale-p nil) (defun +magit-mark-stale-buffers-h ()
"Revert all visible buffers and mark buried buffers as stale.
Stale buffers are reverted when they are switched to, assuming they haven't been
modified."
(dolist (buffer (buffer-list))
(when (buffer-live-p buffer)
(if (get-buffer-window buffer)
(+magit--revert-buffer buffer)
(with-current-buffer buffer
(setq +magit--stale-p t))))))
;;;###autoload ;;;###autoload
(defun +magit-refresh-vc-state-maybe-h () (defun +magit-revert-buffer-maybe-h ()
"Update `vc' and `git-gutter' if out of date." "Update `vc' and `git-gutter' if out of date."
(when +magit--vc-is-stale-p (when +magit--stale-p
(+magit--refresh-vc-in-buffer (current-buffer)))) (+magit--revert-buffer (current-buffer))))
;;;###autoload
(add-hook 'doom-switch-buffer-hook #'+magit-refresh-vc-state-maybe-h) ;;
;;; Commands
;;;###autoload ;;;###autoload
(defun +magit/quit (&optional kill-buffer) (defun +magit/quit (&optional kill-buffer)
@ -76,12 +85,7 @@ control in buffers."
(eq major-mode 'magit-status-mode))) (eq major-mode 'magit-status-mode)))
(window-list))) (window-list)))
(mapc #'+magit--kill-buffer (magit-mode-get-buffers)) (mapc #'+magit--kill-buffer (magit-mode-get-buffers))
(dolist (buffer (buffer-list)) (+magit-mark-stale-buffers-h)))
(when (buffer-live-p buffer)
(if (get-buffer-window buffer)
(+magit--refresh-vc-in-buffer buffer)
(with-current-buffer buffer
(setq +magit--vc-is-stale-p t)))))))
(defun +magit--kill-buffer (buf) (defun +magit--kill-buffer (buf)
"TODO" "TODO"

View file

@ -7,7 +7,7 @@
:commands magit-file-delete :commands magit-file-delete
:defer-incrementally (dash f s with-editor git-commit package eieio lv transient) :defer-incrementally (dash f s with-editor git-commit package eieio lv transient)
:init :init
(setq magit-auto-revert-mode nil) ; we do this ourselves (setq magit-auto-revert-mode nil) ; we do this ourselves further down
;; Must be set early to prevent ~/.emacs.d/transient from being created ;; Must be set early to prevent ~/.emacs.d/transient from being created
(setq transient-levels-file (concat doom-etc-dir "transient/levels") (setq transient-levels-file (concat doom-etc-dir "transient/levels")
transient-values-file (concat doom-etc-dir "transient/values") transient-values-file (concat doom-etc-dir "transient/values")
@ -21,10 +21,16 @@
;; formatters. Trust us to know what we're doing. ;; formatters. Trust us to know what we're doing.
magit-save-repository-buffers nil) magit-save-repository-buffers nil)
(defadvice! +magit-invalidate-projectile-cache-a (&rest _args) (defadvice! +magit-revert-repo-buffers-deferred-a (&rest _)
;; We ignore the args to `magit-checkout'.
:after '(magit-checkout magit-branch-and-checkout) :after '(magit-checkout magit-branch-and-checkout)
(projectile-invalidate-cache nil)) ;; Since the project likely now contains new files, best we undo the
;; projectile cache so it can be regenerated later.
(projectile-invalidate-cache nil)
;; Use a more efficient strategy to auto-revert buffers whose git state has
;; changed: refresh the visible buffers immediately...
(+magit-mark-stale-buffers-h))
;; ...then refresh the rest only when we switch to them, not all at once.
(add-hook 'doom-switch-buffer-hook #'+magit-revert-buffer-maybe-h)
;; The default location for git-credential-cache is in ;; The default location for git-credential-cache is in
;; ~/.cache/git/credential. However, if ~/.git-credential-cache/ exists, then ;; ~/.cache/git/credential. However, if ~/.git-credential-cache/ exists, then
@ -63,7 +69,8 @@
(and (derived-mode-p 'magit-mode) (and (derived-mode-p 'magit-mode)
(not (eq major-mode 'magit-process-mode)))))) (not (eq major-mode 'magit-process-mode))))))
;; properly kill leftover magit buffers on quit ;; Clean up after magit by killing leftover magit buffers and reverting
;; affected buffers (or at least marking them as need-to-be-reverted).
(define-key magit-status-mode-map [remap magit-mode-bury-buffer] #'+magit/quit) (define-key magit-status-mode-map [remap magit-mode-bury-buffer] #'+magit/quit)
;; Close transient with ESC ;; Close transient with ESC