From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#66113: Apply the entire diff buffer Date: Sat, 23 Sep 2023 20:52:27 +0300 Organization: LINKOV.NET Message-ID: <86lecwvjw4.fsf@mail.linkov.net> References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> <86msxep69i.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="913"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 66113@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 23 20:16:09 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qk7An-000070-Rl for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 23 Sep 2023 20:16:09 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qk7AW-0004kD-Gw; Sat, 23 Sep 2023 14:15:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qk7AU-0004k0-Tg for bug-gnu-emacs@gnu.org; Sat, 23 Sep 2023 14:15:51 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qk7AU-00020u-Li for bug-gnu-emacs@gnu.org; Sat, 23 Sep 2023 14:15:50 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qk7Af-0000b0-UV for bug-gnu-emacs@gnu.org; Sat, 23 Sep 2023 14:16:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 23 Sep 2023 18:16:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66113 X-GNU-PR-Package: emacs Original-Received: via spool by 66113-submit@debbugs.gnu.org id=B66113.16954929412259 (code B ref 66113); Sat, 23 Sep 2023 18:16:01 +0000 Original-Received: (at 66113) by debbugs.gnu.org; 23 Sep 2023 18:15:41 +0000 Original-Received: from localhost ([127.0.0.1]:40596 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qk7AL-0000aM-C8 for submit@debbugs.gnu.org; Sat, 23 Sep 2023 14:15:41 -0400 Original-Received: from relay5-d.mail.gandi.net ([2001:4b98:dc4:8::225]:60359) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qk7A5-0000Zi-Mi for 66113@debbugs.gnu.org; Sat, 23 Sep 2023 14:15:39 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 769961C0002; Sat, 23 Sep 2023 18:15:05 +0000 (UTC) In-Reply-To: <86msxep69i.fsf@mail.linkov.net> (Juri Linkov's message of "Fri, 22 Sep 2023 18:48:49 +0300") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:271179 Archived-At: --=-=-= Content-Type: text/plain >> 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: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=diff-apply-buffer.patch 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) --=-=-=--