From 0150f879ea1928e367190cb95cd96b3022b52df3 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 10 Apr 2025 18:02:10 -0400 Subject: [PATCH] feat(magit): introduce +magit-auto-revert option Provides more control over our auto-reverting feature, and changes its behavior to only target non-remote buffers by default, because reverting remote buffers could be tremendously slow. This could be later improved to treat TRAMP buffers with local methods (like sudo) as local buffers. Fix: #8354 --- modules/tools/magit/autoload.el | 13 ++++++++++++- modules/tools/magit/config.el | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/tools/magit/autoload.el b/modules/tools/magit/autoload.el index 2ba4191a9..2bf29abc0 100644 --- a/modules/tools/magit/autoload.el +++ b/modules/tools/magit/autoload.el @@ -98,6 +98,17 @@ window that already exists in that direction. It will split otherwise." (defvar +magit--stale-p nil) +(defun +magit--revertable-buffer-p (buffer) + (when (buffer-live-p buffer) + (pcase +magit-auto-revert + (`t t) + (`local + (not (file-remote-p + (or (buffer-file-name buffer) + (buffer-local-value 'default-directory buffer))))) + ((pred functionp) + (funcall +magit-auto-revert buffer))))) + (defun +magit--revert-buffer (buffer) (with-current-buffer buffer (kill-local-variable '+magit--stale-p) @@ -116,7 +127,7 @@ Stale buffers are reverted when they are switched to, assuming they haven't been modified." (let ((visible-buffers (doom-visible-buffers nil t))) (dolist (buffer (buffer-list)) - (when (buffer-live-p buffer) + (when (+magit--revertable-buffer-p buffer) (if (memq buffer visible-buffers) (progn (+magit--revert-buffer buffer) diff --git a/modules/tools/magit/config.el b/modules/tools/magit/config.el index d7847275b..9564b5618 100644 --- a/modules/tools/magit/config.el +++ b/modules/tools/magit/config.el @@ -12,6 +12,28 @@ left and right fringe. Only has an effect in GUI Emacs.") +(defvar +magit-auto-revert 'local + "If non-nil, revert associated buffers after Git operations with side-effects. + +These buffers are auto-reverted immediately if they're visible or reverted next +time they're switched to. This is intended to be a much more efficient +replacement for `magit-auto-revert-mode' and `global-auto-revert-mode', and +should not be used together with them! Set this to `nil' if you plan to use the +above. + +Accepts one of three values OR a predicate function: + +t + Revert any associated buffers. +local + Same as `t', except remote (TRAMP) buffers are ignored. +nil + Don't do any auto-reverting at all. +FUNCTION + If given a function, it will be passed a buffer associated with the current + Magit session and must return non-nil to signal this is buffer is safe to + revert (now or later, when switched to).") + ;; ;;; Packages