Skip to content

Commit 6852936

Browse files
committed
magit-wip-mode: Speedup activation by avoiding duplicated work
Reduce work to one call to `magit-toplevel' for each directory from which one or more files are being visited, and to one call to `magit-tracked-files' for each repository from which one or more files are being visited. Unfortunately we cannot do the same if `magit-wip-after-save-mode' is enabled directly, but that is only provided as a separate mode for backward compatibility anyway. Closes #4768.
1 parent 430a52c commit 6852936

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

lisp/magit-wip.el

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ is used as `branch-ref'."
103103

104104
;;; Modes
105105

106+
(defvar magit--wip-activation-cache nil)
107+
106108
;;;###autoload
107109
(define-minor-mode magit-wip-mode
108110
"Save uncommitted changes to work-in-progress refs.
@@ -119,7 +121,8 @@ but that is discouraged."
119121
:lighter magit-wip-mode-lighter
120122
:global t
121123
(let ((arg (if magit-wip-mode 1 -1)))
122-
(magit-wip-after-save-mode arg)
124+
(let ((magit--wip-activation-cache (list t)))
125+
(magit-wip-after-save-mode arg))
123126
(magit-wip-after-apply-mode arg)
124127
(magit-wip-before-change-mode arg)
125128
(magit-wip-initial-backup-mode arg)))
@@ -144,8 +147,30 @@ variant `magit-wip-after-save-mode'."
144147

145148
(defun magit-wip-after-save-local-mode-turn-on ()
146149
(when (and buffer-file-name
147-
(magit-inside-worktree-p t)
148-
(magit-file-tracked-p buffer-file-name))
150+
(if magit--wip-activation-cache
151+
(if-let ((elt (assoc default-directory
152+
magit--wip-activation-cache)))
153+
(and-let* ((top (cadr elt)))
154+
(member (file-relative-name buffer-file-name top)
155+
(cddr elt)))
156+
(if-let ((top (magit-toplevel)))
157+
(let (files)
158+
(if-let ((elt (assoc top magit--wip-activation-cache)))
159+
(setq files (cddr elt))
160+
(progn
161+
(setq files (let ((default-directory top))
162+
(magit-tracked-files)))
163+
(push `(,top ,top ,@files)
164+
magit--wip-activation-cache)
165+
(unless (eq default-directory top)
166+
(push `(,default-directory ,top ,@files)
167+
magit--wip-activation-cache))))
168+
(member (file-relative-name buffer-file-name) files))
169+
(push (list default-directory nil)
170+
magit--wip-activation-cache)
171+
nil))
172+
(and (magit-inside-worktree-p t)
173+
(magit-file-tracked-p buffer-file-name))))
149174
(magit-wip-after-save-local-mode)))
150175

151176
;;;###autoload

0 commit comments

Comments
 (0)