[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#66113: Apply the entire diff buffer
From: |
Juri Linkov |
Subject: |
bug#66113: Apply the entire diff buffer |
Date: |
Mon, 25 Sep 2023 20:49:04 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) |
>>> I tried testing it out too. There is a patch where the third hunk doesn't
>>> apply (errors with "can't find the text to patch" in regular usage). This
>>> command ends with cryptic "No next hunk".
>> This is what I expected that diff-hunk-next might signal an error
>> that we should catch, but still couldn't find a patch that fails
>> with such error. So I will try to construct such a patch manually.
>
> Try the patch attached to this message:
> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=63896#5
Thanks, I forgot about signatures in git patches.
diff-mode already handles them in diff-fixup-modifs:
;; In git format-patch "^-- $" signifies
;; the end of the patch.
(and (eq diff-buffer-type 'git)
(looking-at "^-- $"))
So I just copied this code here:
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index d776375d681..8f70731c7dc 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -216,6 +216,7 @@ diff-mode-map
"C-x 4 A" #'diff-add-change-log-entries-other-window
;; Misc operations.
"C-c C-a" #'diff-apply-hunk
+ "C-c C-m a" #'diff-apply-buffer
"C-c C-e" #'diff-ediff-patch
"C-c C-n" #'diff-restrict-view
"C-c C-s" #'diff-split-hunk
@@ -2054,6 +2055,43 @@ diff-kill-applied-hunks
(diff-hunk-kill)
(diff-hunk-next)))))
+(defun diff-apply-buffer ()
+ "Apply the diff in the entire diff buffer.
+When applying all hunks was successful, then save the changed buffers."
+ (interactive)
+ (let ((buffer-edits nil)
+ (failures 0)
+ (diff-refine nil))
+ (save-excursion
+ (goto-char (point-min))
+ (diff-beginning-of-hunk t)
+ (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched)
+ (diff-find-source-location nil nil)))
+ (cond ((and line-offset (not switched))
+ (push (cons pos dst)
+ (alist-get buf buffer-edits)))
+ (t (setq failures (1+ failures))))
+ (not (or (eq (prog1 (point) (diff-hunk-next)) (point))
+ (eobp)
+ ;; In git format-patch "^-- $" signifies
+ ;; the end of the patch.
+ (and (eq diff-buffer-type 'git)
+ (looking-at-p "^-- $")))))))
+ (cond ((zerop failures)
+ (dolist (buf-edits (reverse buffer-edits))
+ (with-current-buffer (car buf-edits)
+ (dolist (edit (cdr buf-edits))
+ (let ((pos (car edit))
+ (dst (cdr edit))
+ (inhibit-read-only t))
+ (goto-char (car pos))
+ (delete-region (car pos) (cdr pos))
+ (insert (car dst))))
+ (save-buffer)))
+ (message "Saved %d buffers" (length buffer-edits)))
+ (t
+ (message "%d hunks failed; no buffers changed" failures)))))
+
(defalias 'diff-mouse-goto-source #'diff-goto-source)
(defun diff-goto-source (&optional other-file event)
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/20
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/21
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/22
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/22
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/22
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/23
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/23
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/24
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/24
- bug#66113: Apply the entire diff buffer,
Juri Linkov <=
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/25
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/27