GNU bug report logs - #66113
Apply the entire diff buffer

Previous Next

Package: emacs;

Reported by: Juri Linkov <juri <at> linkov.net>

Date: Wed, 20 Sep 2023 06:51:01 UTC

Severity: normal

Fixed in version 30.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Juri Linkov <juri <at> linkov.net>
To: Dmitry Gutov <dmitry <at> gutov.dev>
Cc: 66113 <at> debbugs.gnu.org
Subject: bug#66113: Apply the entire diff buffer
Date: Mon, 25 Sep 2023 20:49:04 +0300
[Message part 1 (text/plain, inline)]
>>> 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-apply-buffer.patch (text/x-diff, inline)]
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)

This bug report was last modified 1 year and 233 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.