branch: elpa/magit
commit 051fecf3f704bb2a8ee4d49c3df700386f07bf8d
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    magit-{before-change,after-apply}-functions: New hooks
---
 lisp/magit-apply.el  | 27 +++++++++++++--------------
 lisp/magit-extras.el |  2 +-
 lisp/magit-files.el  |  6 ++----
 lisp/magit-reset.el  |  8 ++++----
 lisp/magit-wip.el    | 26 +++++++++++++++++---------
 5 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/lisp/magit-apply.el b/lisp/magit-apply.el
index e5b69d5946b..c19e8860c61 100644
--- a/lisp/magit-apply.el
+++ b/lisp/magit-apply.el
@@ -225,8 +225,8 @@ adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 
@@\"."
          (ignore-context (magit-diff-ignore-any-space-p)))
     (unless (magit-diff-context-p)
       (user-error "Not enough context to apply patch.  Increase the context"))
-    (when (and magit-wip-before-change-mode (not magit-inhibit-refresh))
-      (magit-wip-commit-before-change files (concat " before " command)))
+    (unless magit-inhibit-refresh
+      (magit-run-before-change-functions files command))
     (with-temp-buffer
       (insert patch)
       (let ((magit-inhibit-refresh t))
@@ -235,8 +235,7 @@ adjusted as \"@@ -10,6 +10,7 @@\" and \"@@ -18,6 +19,7 
@@\"."
          (if ignore-context "-C0" (format "-C%s" context))
          "--ignore-space-change" "-")))
     (unless magit-inhibit-refresh
-      (when magit-wip-after-apply-mode
-        (magit-wip-commit-after-apply files (concat " after " command)))
+      (magit-run-after-apply-functions files command)
       (magit-refresh))))
 
 (defun magit-apply--get-selection ()
@@ -339,11 +338,11 @@ ignored) files."
     (magit-stage-1 (if all "--all" "-u") magit-buffer-diff-files)))
 
 (defun magit-stage-1 (arg &optional files)
-  (magit-wip-commit-before-change files " before stage")
+  (magit-run-before-change-functions files "stage")
   (magit-run-git "add" arg (if files (cons "--" files) "."))
   (when magit-auto-revert-mode
     (mapc #'magit-turn-on-auto-revert-mode-if-desired files))
-  (magit-wip-commit-after-apply files " after stage"))
+  (magit-run-after-apply-functions files "stage"))
 
 (defun magit-stage-untracked (&optional intent)
   (let* ((section (magit-current-section))
@@ -357,7 +356,7 @@ ignored) files."
                (magit-git-repo-p file t))
           (push file repos)
         (push file plain)))
-    (magit-wip-commit-before-change files " before stage")
+    (magit-run-before-change-functions files "stage")
     (when plain
       (magit-run-git "add" (and intent "--intent-to-add")
                      "--" plain)
@@ -389,7 +388,7 @@ ignored) files."
                    (expand-file-name ".gitmodules" topdir))
                   (let ((default-directory borg-user-emacs-directory))
                     (borg--maybe-absorb-gitdir package)))))))))
-    (magit-wip-commit-after-apply files " after stage")))
+    (magit-run-after-apply-functions files "stage")))
 
 (defvar magit-post-stage-hook-commands
   (list #'magit-stage
@@ -443,11 +442,11 @@ ignored) files."
     (magit-unstage-1 files)))
 
 (defun magit-unstage-1 (files)
-  (magit-wip-commit-before-change files " before unstage")
+  (magit-run-before-change-functions files "unstage")
   (if (magit-no-commit-p)
       (magit-run-git "rm" "--cached" "--" files)
     (magit-run-git "reset" "HEAD" "--" files))
-  (magit-wip-commit-after-apply files " after unstage"))
+  (magit-run-after-apply-functions files "unstage"))
 
 (defun magit-unstage-intent (files)
   (if-let* ((staged (magit-staged-files))
@@ -464,9 +463,9 @@ ignored) files."
   (when (or (magit-anything-unstaged-p)
             (magit-untracked-files))
     (magit-confirm 'unstage-all-changes))
-  (magit-wip-commit-before-change nil " before unstage")
+  (magit-run-before-change-functions nil "unstage")
   (magit-run-git "reset" "HEAD" "--" magit-buffer-diff-files)
-  (magit-wip-commit-after-apply nil " after unstage"))
+  (magit-run-after-apply-functions nil "unstage"))
 
 (defvar magit-post-unstage-hook-commands
   (list #'magit-unstage
@@ -571,7 +570,7 @@ of a side, then keep that side without prompting."
           (`(?X ?R ,(or ?  ?M ?D)) (push file rename)))))
     (unwind-protect
         (let ((magit-inhibit-refresh t))
-          (magit-wip-commit-before-change files " before discard")
+          (magit-run-before-change-functions files "discard")
           (when resolve
             (magit-discard-files--resolve (nreverse resolve)))
           (when resurrect
@@ -583,7 +582,7 @@ of a side, then keep that side without prompting."
           (when (or discard discard-new)
             (magit-discard-files--discard (nreverse discard)
                                           (nreverse discard-new)))
-          (magit-wip-commit-after-apply files " after discard"))
+          (magit-run-after-apply-functions files "discard"))
       (magit-refresh))))
 
 (defun magit-discard-files--resolve (files)
diff --git a/lisp/magit-extras.el b/lisp/magit-extras.el
index e5e198f1720..fce0ae131f7 100644
--- a/lisp/magit-extras.el
+++ b/lisp/magit-extras.el
@@ -279,7 +279,7 @@ with two prefix arguments remove ignored files only.
                                (1 "untracked")
                                (4 "untracked and ignored")
                                (_ "ignored"))))
-    (magit-wip-commit-before-change)
+    (magit-run-before-change-functions nil "clean")
     (magit-run-git "clean" "-f" "-d" (pcase arg (4 "-x") (16 "-X")))))
 
 (put 'magit-clean 'disabled t)
diff --git a/lisp/magit-files.el b/lisp/magit-files.el
index 218376ec125..fde3922571c 100644
--- a/lisp/magit-files.el
+++ b/lisp/magit-files.el
@@ -211,8 +211,7 @@ is done using `magit-find-index-noselect'."
         (let ((index (make-temp-name
                       (expand-file-name "magit-update-index-" (magit-gitdir))))
               (buffer (current-buffer)))
-          (when magit-wip-before-change-mode
-            (magit-wip-commit-before-change (list file) " before un-/stage"))
+          (magit-run-before-change-functions file "un-/stage")
           (unwind-protect
               (progn
                 (let ((coding-system-for-write buffer-file-coding-system))
@@ -229,8 +228,7 @@ is done using `magit-find-index-noselect'."
                    file)))
             (ignore-errors (delete-file index)))
           (set-buffer-modified-p nil)
-          (when magit-wip-after-apply-mode
-            (magit-wip-commit-after-apply (list file) " after un-/stage")))
+          (magit-run-after-apply-functions file "un-/stage"))
       (message "Abort")))
   (when-let ((buffer (magit-get-mode-buffer 'magit-status-mode)))
     (with-current-buffer buffer
diff --git a/lisp/magit-reset.el b/lisp/magit-reset.el
index 246bccc2ad8..6bb742080ed 100644
--- a/lisp/magit-reset.el
+++ b/lisp/magit-reset.el
@@ -89,10 +89,10 @@ head this effectively unstages all changes.
   "Reset the worktree to COMMIT.
 Keep the `HEAD' and index as-is."
   (interactive (list (magit-read-branch-or-commit "Reset worktree to")))
-  (magit-wip-commit-before-change nil " before reset")
+  (magit-run-before-change-functions nil "reset")
   (magit-with-temp-index commit nil
     (magit-call-git "checkout-index" "--all" "--force"))
-  (magit-wip-commit-after-apply nil " after reset")
+  (magit-run-after-apply-functions nil "reset")
   (magit-refresh))
 
 ;;;###autoload
@@ -127,10 +127,10 @@ or \"detached head\" will be substituted for %s."
       (git-commit-setup-font-lock)
       (git-commit-save-message)))
   (let ((cmd (if (and (equal commit "HEAD") (not arg)) "unstage" "reset")))
-    (magit-wip-commit-before-change nil (concat " before " cmd))
+    (magit-run-before-change-functions nil cmd)
     (magit-run-git "reset" arg commit "--" path)
     (when (equal cmd "unstage")
-      (magit-wip-commit-after-apply nil " after unstage"))))
+      (magit-run-after-apply-functions nil "unstage"))))
 
 ;;; _
 (provide 'magit-reset)
diff --git a/lisp/magit-wip.el b/lisp/magit-wip.el
index 5332a517cc3..446318a9c83 100644
--- a/lisp/magit-wip.el
+++ b/lisp/magit-wip.el
@@ -135,11 +135,15 @@ in the worktree and the other contains snapshots of the 
entries
 in the index."
   :package-version '(magit . "2.1.0")
   :group 'magit-wip
-  :global t)
+  :global t
+  (if magit-wip-after-apply-mode
+      (add-hook  'magit-after-apply-functions #'magit-wip-commit)
+    (remove-hook 'magit-after-apply-functions #'magit-wip-commit)))
 
-(defun magit-wip-commit-after-apply (&optional files msg)
-  (when magit-wip-after-apply-mode
-    (magit-wip-commit files msg)))
+(defun magit-run-after-apply-functions (files task)
+  (run-hook-with-args 'magit-after-apply-functions
+                      (ensure-list files)
+                      (format " after %s" task)))
 
 ;;;###autoload
 (define-minor-mode magit-wip-before-change-mode
@@ -156,11 +160,15 @@ Only changes to files which could potentially be affected 
by the
 command which is about to be called are committed."
   :package-version '(magit . "2.1.0")
   :group 'magit-wip
-  :global t)
-
-(defun magit-wip-commit-before-change (&optional files msg)
-  (when magit-wip-before-change-mode
-    (magit-wip-commit files msg)))
+  :global t
+  (if magit-wip-before-change-mode
+      (add-hook  'magit-before-change-functions #'magit-wip-commit)
+    (remove-hook 'magit-before-change-functions #'magit-wip-commit)))
+
+(defun magit-run-before-change-functions (files task)
+  (run-hook-with-args 'magit-before-change-functions
+                      (ensure-list files)
+                      (format " before %s" task)))
 
 (define-minor-mode magit-wip-initial-backup-mode
   "Before saving a buffer for the first time, commit to a wip ref."

Reply via email to