[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: |
Sat, 23 Sep 2023 20:52:27 +0300 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) |
>> Another approach would be to first go through the patch and check that all
>> hunks apply without problems, and then, on the second pass, actually apply
>> them.
>
> This is a better option, thanks for the idea, will try to do this with
> 'diff-test-hunk' in a loop.
Indeed, this is much better:
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index d776375d681..2b0daabb12c 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,39 @@ 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))))))
+ (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 <=
- 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, 2023/09/25
- bug#66113: Apply the entire diff buffer, Dmitry Gutov, 2023/09/25
- bug#66113: Apply the entire diff buffer, Juri Linkov, 2023/09/27