Skip to main content
Linked to tarsius' advice
Source Link
izkon
  • 1.9k
  • 12
  • 24

Here is the working solution I ultimately wound up using npostavs' idea of advising. It is based on magit-mode-bury-buffertarsius' advice, and that at first it seemed to work fine....

Here'swho deserves all the code:credit.

(defun kill-magit-diff-buffersbuffer-in-current-repo (&rest _)
  "Delete allthe magit-diff buffers"buffer related to the current repo"
  (mapclet ((magit-diff-buffer-in-current-repo
   (lambda      (magit-mode-get-buffer 'magit-diff-mode)))
     (with-currentkill-buffer magit-diff-buffer-in-current-repo)))
;;
;; When 'C-c C-c' is pressed in (when
the magit commit message buffer,
;; delete the magit-diff buffer related to (eqthe majorcurrent repo.
;;
(add-modehook 'magit'git-diffcommit-mode)setup-hook
          (kill-bufferlambda buffer)))()
   (buffer-list)))

         (advice-add-hook 'magit'with-modeeditor-burypost-bufferfinish-hook
 :after                     #'kill-magit-diff-buffersbuffer-in-current-repo
                      nil t))) ; the t is important

WARNING: I think the magit-mode-bury-buffer is called by magit not just when you hit q from the magit-status buffer, but in other places as well. Unfortunately, it occasionally causes Git failed... errors (such as when trying to commit from the magit-status buffer by typing c c and answering y to the question "Stage and commit everything?"). So I've reverted this change and am still looking for another solution.

I wound up using npostavs' idea of advising magit-mode-bury-buffer, and that at first it seemed to work fine....

Here's the code:

(defun kill-magit-diff-buffers (&rest _)
  "Delete all magit-diff buffers"
  (mapc
   (lambda (buffer)
     (with-current-buffer buffer
       (when
           (eq major-mode 'magit-diff-mode)
         (kill-buffer buffer))))
   (buffer-list)))

(advice-add 'magit-mode-bury-buffer :after #'kill-magit-diff-buffers)

WARNING: I think the magit-mode-bury-buffer is called by magit not just when you hit q from the magit-status buffer, but in other places as well. Unfortunately, it occasionally causes Git failed... errors (such as when trying to commit from the magit-status buffer by typing c c and answering y to the question "Stage and commit everything?"). So I've reverted this change and am still looking for another solution.

Here is the working solution I ultimately wound up using. It is based on tarsius' advice, who deserves all the credit.

(defun kill-magit-diff-buffer-in-current-repo (&rest _)
  "Delete the magit-diff buffer related to the current repo"
  (let ((magit-diff-buffer-in-current-repo
         (magit-mode-get-buffer 'magit-diff-mode)))
    (kill-buffer magit-diff-buffer-in-current-repo)))
;;
;; When 'C-c C-c' is pressed in the magit commit message buffer,
;; delete the magit-diff buffer related to the current repo.
;;
(add-hook 'git-commit-setup-hook
          (lambda ()
            (add-hook 'with-editor-post-finish-hook
                      #'kill-magit-diff-buffer-in-current-repo
                      nil t))) ; the t is important
Documented how this solution breaks magit
Source Link
izkon
  • 1.9k
  • 12
  • 24

I wound up using npostavs' idea of advising magit-mode-bury-buffer, and that seemedat first it seemed to work fine....

Here's the code:

(defun kill-magit-diff-buffers (&rest _)
  "Delete all magit-diff buffers"
  (mapc
   (lambda (buffer)
     (with-current-buffer buffer
       (when
           (eq major-mode 'magit-diff-mode)
         (kill-buffer buffer))))
   (buffer-list)))

(advice-add 'magit-mode-bury-buffer :after #'kill-magit-diff-buffers)

WARNING: I think the magit-mode-bury-buffer is called by magit not just when you hit q from the magit-status buffer, but in other places as well. Unfortunately, it occasionally causes Git failed... errors (such as when trying to commit from the magit-status buffer by typing c c and answering y to the question "Stage and commit everything?"). So I've reverted this change and am still looking for another solution.

I wound up using npostavs' idea of advising magit-mode-bury-buffer, and that seemed to work fine.

Here's the code:

(defun kill-magit-diff-buffers (&rest _)
  "Delete all magit-diff buffers"
  (mapc
   (lambda (buffer)
     (with-current-buffer buffer
       (when
           (eq major-mode 'magit-diff-mode)
         (kill-buffer buffer))))
   (buffer-list)))

(advice-add 'magit-mode-bury-buffer :after #'kill-magit-diff-buffers)

I wound up using npostavs' idea of advising magit-mode-bury-buffer, and that at first it seemed to work fine....

Here's the code:

(defun kill-magit-diff-buffers (&rest _)
  "Delete all magit-diff buffers"
  (mapc
   (lambda (buffer)
     (with-current-buffer buffer
       (when
           (eq major-mode 'magit-diff-mode)
         (kill-buffer buffer))))
   (buffer-list)))

(advice-add 'magit-mode-bury-buffer :after #'kill-magit-diff-buffers)

WARNING: I think the magit-mode-bury-buffer is called by magit not just when you hit q from the magit-status buffer, but in other places as well. Unfortunately, it occasionally causes Git failed... errors (such as when trying to commit from the magit-status buffer by typing c c and answering y to the question "Stage and commit everything?"). So I've reverted this change and am still looking for another solution.

Source Link
izkon
  • 1.9k
  • 12
  • 24

I wound up using npostavs' idea of advising magit-mode-bury-buffer, and that seemed to work fine.

Here's the code:

(defun kill-magit-diff-buffers (&rest _)
  "Delete all magit-diff buffers"
  (mapc
   (lambda (buffer)
     (with-current-buffer buffer
       (when
           (eq major-mode 'magit-diff-mode)
         (kill-buffer buffer))))
   (buffer-list)))

(advice-add 'magit-mode-bury-buffer :after #'kill-magit-diff-buffers)