From debbugs-submit-bounces@debbugs.gnu.org Wed Sep 20 02:50:06 2023 Received: (at submit) by debbugs.gnu.org; 20 Sep 2023 06:50:06 +0000 Received: from localhost ([127.0.0.1]:58132 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qir2E-0001wY-E7 for submit@debbugs.gnu.org; Wed, 20 Sep 2023 02:50:06 -0400 Received: from lists.gnu.org ([2001:470:142::17]:50050) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qir28-0001vx-QN for submit@debbugs.gnu.org; Wed, 20 Sep 2023 02:50:04 -0400 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 1qir1u-0007Pq-8y for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:49:46 -0400 Received: from relay9-d.mail.gandi.net ([2001:4b98:dc4:8::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qir1r-0003R1-Ui for bug-gnu-emacs@gnu.org; Wed, 20 Sep 2023 02:49:46 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id AF523FF811 for ; Wed, 20 Sep 2023 06:49:36 +0000 (UTC) From: Juri Linkov To: bug-gnu-emacs@gnu.org Subject: Apply the entire diff buffer Organization: LINKOV.NET Date: Wed, 20 Sep 2023 09:46:18 +0300 Message-ID: <86o7hx4a11.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-Sasl: juri@linkov.net Received-SPF: pass client-ip=2001:4b98:dc4:8::229; envelope-from=juri@linkov.net; helo=relay9-d.mail.gandi.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.7 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.3 (/) --=-=-= Content-Type: text/plain After discussing on emacs-devel, here is a complete patch for the feature of applying the entire diff buffer: --=-=-= 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..4633d5896a7 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,40 @@ 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 ((buffers 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)) + (with-current-buffer buf + (goto-char (car pos)) + (delete-region (car pos) (cdr pos)) + (insert (car dst)) + (when buffer-file-name + (push (current-buffer) buffers)))) + (t (setq failures (1+ failures)))) + (not (or (eq (prog1 (point) (diff-hunk-next)) (point)) + (eobp)))))) + (setq buffers (delete-dups buffers)) + (cond ((zerop failures) + (dolist (buf (reverse buffers)) + (with-current-buffer buf + (save-buffer))) + (message "Saved %d buffers" (length buffers))) + (t + (dolist (buf buffers) + (with-current-buffer buf + (display-buffer buf))) + (message "%d hunks failed; no buffers saved" failures))))) + (defalias 'diff-mouse-goto-source #'diff-goto-source) (defun diff-goto-source (&optional other-file event) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Thu Sep 21 21:38:45 2023 Received: (at 66113) by debbugs.gnu.org; 22 Sep 2023 01:38:45 +0000 Received: from localhost ([127.0.0.1]:35043 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjV80-000880-VI for submit@debbugs.gnu.org; Thu, 21 Sep 2023 21:38:45 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:58591) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjV7y-00087l-K2 for 66113@debbugs.gnu.org; Thu, 21 Sep 2023 21:38:43 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 276195C0266; Thu, 21 Sep 2023 21:38:27 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 21 Sep 2023 21:38:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695346707; x=1695433107; bh=EQ9v3prKvVofx6XPw//nfIwpHhczGj/60dR 0d550c5M=; b=KcALrCQz97N2OB8dTVEzIHWMphH867LbXrbycp13cCkeiS5ZyX/ Q5plo9N1YezSZZ9wSUap2CSVKbeVxJFO210WzlIPOOTy0CqschbRKrow/USFmEze 7g59NVI2vosj27Nb44b/DTKkYueevNRAhIk5XxwcXX1P3g4znZR+isyVWTOKzVJF ZFvrFjJX3JiBzZwa1Zwi5Q5tuJDx0dvTegBUEY7QIzA8pcdvH2t/D8/HaLwGGZnk KUYA+RDGFiQYEyyrer73K/Ulf/sDoSIBxdNat4cGfivE1zBcW2OioUQj6U8jwGim x8f//pvYOGJ6EVfK0Uzq1F9l76yUtMu9swQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1695346707; x= 1695433107; bh=EQ9v3prKvVofx6XPw//nfIwpHhczGj/60dR0d550c5M=; b=R GDOEqtZDKX8KRH8weDutnDeLWAkVP2h/duu6JIFzOerCDpBwHHQh5FxBhXOsFoXl WbLcFkci5fndpJ8sOT+HnbiiMjOgDUA4DqO8Y6WRw3xCtCW5iPbg6dTvIfp7M7T6 y/LcNp34CwcQ3J1wMGKKpQAenry+CkeDbfCAm9QxPVoiLVmcvWnqxFZeoPITkENb 48huUxHNt1G6G+mFqIFKdcEDg7J26qSG+O64cgu/pDfktBFQeCK5KVnAkgmwJeG8 4swPZtS34QDEPttooVcD0iGBlQNXABY9qBZI2a2ZBQ9JED+inYhE8I4uquMoKCjG ctLDGQURWyp1+QZqnF/cg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudekjedggeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthejredttdefjeenucfhrhhomhepffhmihht rhihucfiuhhtohhvuceoughmihhtrhihsehguhhtohhvrdguvghvqeenucggtffrrghtth gvrhhnpeeghedthedujeeiteeutddtjeekheejteeukeehffdutdejuedvfeevueeviedu udenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegumh hithhrhiesghhuthhovhdruggvvh X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 21 Sep 2023 21:38:26 -0400 (EDT) Message-ID: Date: Fri, 22 Sep 2023 04:38:25 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: bug#66113: Apply the entire diff buffer Content-Language: en-US To: Juri Linkov , 66113@debbugs.gnu.org References: <86o7hx4a11.fsf@mail.linkov.net> From: Dmitry Gutov In-Reply-To: <86o7hx4a11.fsf@mail.linkov.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.2 (--) X-Debbugs-Envelope-To: 66113 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.2 (---) Hi Juri, On 20/09/2023 09:46, Juri Linkov wrote: > + (t > + (dolist (buf buffers) > + (with-current-buffer buf > + (display-buffer buf))) > + (message "%d hunks failed; no buffers saved" failures))))) What happens next in this case? How do you undo in the buffers that had the patch hunks already applied? Any change you wanted to work on the idea of the "atomic rollback" as well? From debbugs-submit-bounces@debbugs.gnu.org Fri Sep 22 03:09:41 2023 Received: (at 66113) by debbugs.gnu.org; 22 Sep 2023 07:09:41 +0000 Received: from localhost ([127.0.0.1]:35186 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjaIG-0008VM-Ol for submit@debbugs.gnu.org; Fri, 22 Sep 2023 03:09:40 -0400 Received: from relay2-d.mail.gandi.net ([217.70.183.194]:54845) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjaIE-0008V2-Pz for 66113@debbugs.gnu.org; Fri, 22 Sep 2023 03:09:39 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 2F31E40008; Fri, 22 Sep 2023 07:09:20 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: (Dmitry Gutov's message of "Fri, 22 Sep 2023 04:38:25 +0300") Organization: LINKOV.NET References: <86o7hx4a11.fsf@mail.linkov.net> Date: Fri, 22 Sep 2023 09:45:32 +0300 Message-ID: <86v8c2u34b.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >> + (t >> + (dolist (buf buffers) >> + (with-current-buffer buf >> + (display-buffer buf))) >> + (message "%d hunks failed; no buffers saved" failures))))) > > What happens next in this case? How do you undo in the buffers that had the > patch hunks already applied? Manually, like in case of ediff-patch-buffer. > Any change you wanted to work on the idea of the "atomic rollback" as well? This would be an unreliable feature: in case of diff-apply creates a mess, such automatic undo can create more mess, because there are many different strategies to undo the mess such as using undo-auto-amalgamate, or applying the reverse diff partially, doing more damage in case when buffers were already modified before diff-apply. But fortunately the need to undo will be extremely rare, because when patch hunks are already applied, it reports the failure, but doesn't modify the source buffer. Therefore there is nothing to undo! From debbugs-submit-bounces@debbugs.gnu.org Fri Sep 22 09:49:48 2023 Received: (at 66113) by debbugs.gnu.org; 22 Sep 2023 13:49:48 +0000 Received: from localhost ([127.0.0.1]:35685 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjgXU-0001KL-GO for submit@debbugs.gnu.org; Fri, 22 Sep 2023 09:49:48 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:60067) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjgXT-0001K8-6X for 66113@debbugs.gnu.org; Fri, 22 Sep 2023 09:49:47 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 6084E5C00BC; Fri, 22 Sep 2023 09:49:31 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 22 Sep 2023 09:49:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695390571; x=1695476971; bh=7x0It2K0rehUmygGNKhdXks/u2rUfd4IIz7 eQ6PIIRw=; b=drgffQz83eCb4jTqoddct2D1RWbLcKJhclYk880JGlGRsGHGZOc cVpXEZdLQkU+TVJAuqjpLSB/5vMeSXRrpzKcxWc5uzm5mMwkjA/ir4fKkRPvbXvZ 5UlLZmfVNxci1GSQDYEqm0BICScsTuUtbT2KzDa6tNqP+IArZTEfjxk75EcuRweu bGFU+/PGfOA21oz4/1/8LsBYKSNZuNF/qPRgqM/NFMF79rMn2XXh42AoMCy8ypU4 z/HQevPk9gxdKYqGd3DlKbQ1REZoeeFMFV0VHvQFr6qCAEioBrdQ46O8mce63uES IFfnhALC/d58nPT4aEJCDNg4e7j5UET8i7w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1695390571; x=1695476971; bh=7x0It2K0rehUmygGNKhdXks/u2rUfd4IIz7 eQ6PIIRw=; b=f6wMz9x6pmfWuyg5CF+O3gz7+UoSa2QNW9Gq+Kl0Bc2fId62WTw il6pucM54ySqHgZI0/icTthakqf2maAI78E75QpJx1bh5Ostx1JZuXGpft5jDsU2 q3I8i/6vhq3soMtKBeS88IZvoTDJqHDbd4RBvH4TK3QgB/b/Hwoct5OCYpZlBtls cwS+/dYGNQMYHf5N9elRsmPuGSE/zwGn17OXyU897WXvX5kj5E9mkwtK++AIcMqA D3jfNxklRciO1fBrYaZ+pEcsLenThjJ80Syb+BEsgNLwcJq3Yp+5qHhw+K68j3IV jmrHA+wGcpDq9C/pU06UC73KedfGMgm56KQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudekkedggeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhepiefgteevheevveffheeltdeukeeiieekueefgedugfefgefhudelgfefveel vdevnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepug hmihhtrhihsehguhhtohhvrdguvghv X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 22 Sep 2023 09:49:30 -0400 (EDT) Message-ID: <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> Date: Fri, 22 Sep 2023 16:49:29 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: bug#66113: Apply the entire diff buffer Content-Language: en-US To: Juri Linkov References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> From: Dmitry Gutov In-Reply-To: <86v8c2u34b.fsf@mail.linkov.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.2 (--) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.2 (---) On 22/09/2023 09:45, Juri Linkov wrote: >>> + (t >>> + (dolist (buf buffers) >>> + (with-current-buffer buf >>> + (display-buffer buf))) >>> + (message "%d hunks failed; no buffers saved" failures))))) >> >> What happens next in this case? How do you undo in the buffers that had the >> patch hunks already applied? > > Manually, like in case of ediff-patch-buffer. I've never used this, so I don't understand. >> Any change you wanted to work on the idea of the "atomic rollback" as well? > > This would be an unreliable feature: in case of diff-apply creates a mess, > such automatic undo can create more mess, because there are many different > strategies to undo the mess such as using undo-auto-amalgamate, or > applying the reverse diff partially, doing more damage in case when > buffers were already modified before diff-apply. How about we save the tips of buffer-undo-list, then in case the buffer needs reverting, basically 'undo' each of the buffers until the saved "tip" is reached? I don't have a quick code anippet, but that seems doable. > But fortunately the need to undo will be extremely rare, because > when patch hunks are already applied, it reports the failure, > but doesn't modify the source buffer. Therefore there is > nothing to undo! The problem situations is, of course, when one of the hunks (somewhere in the middle or near the end) fails to apply cleanly or at all. 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. From debbugs-submit-bounces@debbugs.gnu.org Fri Sep 22 12:03:16 2023 Received: (at 66113) by debbugs.gnu.org; 22 Sep 2023 16:03:16 +0000 Received: from localhost ([127.0.0.1]:37164 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjice-0005Go-Cx for submit@debbugs.gnu.org; Fri, 22 Sep 2023 12:03:16 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:50881) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qjicW-0005G7-42 for 66113@debbugs.gnu.org; Fri, 22 Sep 2023 12:03:11 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 1CB7660002; Fri, 22 Sep 2023 16:02:49 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> (Dmitry Gutov's message of "Fri, 22 Sep 2023 16:49:29 +0300") Organization: LINKOV.NET References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> Date: Fri, 22 Sep 2023 18:48:49 +0300 Message-ID: <86msxep69i.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >>>> + (t >>>> + (dolist (buf buffers) >>>> + (with-current-buffer buf >>>> + (display-buffer buf))) >>>> + (message "%d hunks failed; no buffers saved" failures))))) >>> >>> What happens next in this case? How do you undo in the buffers that had the >>> patch hunks already applied? >> Manually, like in case of ediff-patch-buffer. > > I've never used this, so I don't understand. ediff-patch-buffer is an example when trying to roll back a mess creates more mess with .orig/.rej files. >>> Any change you wanted to work on the idea of the "atomic rollback" as well? >> This would be an unreliable feature: in case of diff-apply creates >> a mess, >> such automatic undo can create more mess, because there are many different >> strategies to undo the mess such as using undo-auto-amalgamate, or >> applying the reverse diff partially, doing more damage in case when >> buffers were already modified before diff-apply. > > How about we save the tips of buffer-undo-list, then in case the buffer > needs reverting, basically 'undo' each of the buffers until the saved "tip" > is reached? I don't have a quick code anippet, but that seems doable. Maybe, but I definitely won't use such an option. >> But fortunately the need to undo will be extremely rare, because >> when patch hunks are already applied, it reports the failure, >> but doesn't modify the source buffer. Therefore there is >> nothing to undo! > > The problem situations is, of course, when one of the hunks (somewhere in > the middle or near the end) fails to apply cleanly or at all. Indeed, this is a problematic case when one of the hunks fails whereas all other hunks are applied cleanly. This sometimes happens when receiving an external patch, but the source already changed. > 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. From debbugs-submit-bounces@debbugs.gnu.org Sat Sep 23 14:15:41 2023 Received: (at 66113) by debbugs.gnu.org; 23 Sep 2023 18:15:41 +0000 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 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 Received: by mail.gandi.net (Postfix) with ESMTPSA id 769961C0002; Sat, 23 Sep 2023 18:15:05 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: <86msxep69i.fsf@mail.linkov.net> (Juri Linkov's message of "Fri, 22 Sep 2023 18:48:49 +0300") Organization: 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> Date: Sat, 23 Sep 2023 20:52:27 +0300 Message-ID: <86lecwvjw4.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-Sasl: juri@linkov.net X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= 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) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Sat Sep 23 21:35:13 2023 Received: (at 66113) by debbugs.gnu.org; 24 Sep 2023 01:35:13 +0000 Received: from localhost ([127.0.0.1]:40776 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkE1R-0004TH-UE for submit@debbugs.gnu.org; Sat, 23 Sep 2023 21:35:13 -0400 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]:53809) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkE1P-0004T3-0u for 66113@debbugs.gnu.org; Sat, 23 Sep 2023 21:34:56 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 72139320093D; Sat, 23 Sep 2023 21:34:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sat, 23 Sep 2023 21:34:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695519276; x=1695605676; bh=invZc5kVhrKokuvOpOkbOMZV74PGeRsB888 cyJ4Ne88=; b=OEk+jG4peQp4LSEVeTG5dVMvZ/T8nDXdnoQUVGTPLH8Pl4Zxfck JbbTNkDSSDJSIy9arMXEiUOqEB48/f4cv0a7Q6lb7b9Pqx/K3wIdGOpHaH9vGEU4 Wr6UoP/87IdFnfzLTZPihuM/tV5iNYEdwrepo2r8GLCzaOmEhNR0WxBDkH4MHvkx UYlebe8J/WUylwZeLq+fcLFa3LGyLQqqUpiWb7EUiO8/wFM9VHBqxdF3uOT3ViH7 pVuNbBekArXAszs5T2Tf15uWO8iiMQ6CUIGaGFpOIbc3TDBUSauW4gwL13dzOcrc XI4Tyxhj6csaSxBKVHCfCX3y+0+XzNGbzjQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1695519276; x=1695605676; bh=invZc5kVhrKokuvOpOkbOMZV74PGeRsB888 cyJ4Ne88=; b=L0qzafbnuYwLR2pScBAUJQUPTnr3w/amYMyDvupdSB8qrp4E3YW 2S45vpq/1EwN1gD4iXFVwHVkaamAgMObhtCx/StWmt6qk497/Po1cvIB/wj7ZekZ T9p3LdiAiNmwH/wLg1juZnDZ2mL0KndfhxsREXP0OnW3VoFG4laLPWnjZpbISrDl JGOV7LvV9lUFzacv6m1Um8N/pmiv5jbLhv7Oo354Rd+1mbvVSQZUMT3eTRq341lA xesFbkdHufEofkBfu2U9FQ+5uly5RT1x7t1eqwCP8ruDggTnfQQbLcX3XEMmwn8K +m7dxidwHK5u9xRIUcrLUwDSAH+X6Yy+oDg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudeluddggeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhepiefgteevheevveffheeltdeukeeiieekueefgedugfefgefhudelgfefveel vdevnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepug hmihhtrhihsehguhhtohhvrdguvghv X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 23 Sep 2023 21:34:35 -0400 (EDT) Message-ID: <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> Date: Sun, 24 Sep 2023 04:34:33 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: bug#66113: Apply the entire diff buffer Content-Language: en-US To: Juri Linkov References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> <86msxep69i.fsf@mail.linkov.net> <86lecwvjw4.fsf@mail.linkov.net> From: Dmitry Gutov In-Reply-To: <86lecwvjw4.fsf@mail.linkov.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.2 (--) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.2 (---) On 23/09/2023 20:52, Juri Linkov wrote: > +(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))))) Sorry, was there supposed to be a call to diff-test-hunk here? Or I'm just not following the implementation. 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". From debbugs-submit-bounces@debbugs.gnu.org Sun Sep 24 03:42:23 2023 Received: (at 66113) by debbugs.gnu.org; 24 Sep 2023 07:42:23 +0000 Received: from localhost ([127.0.0.1]:40927 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkJl0-0006AE-Np for submit@debbugs.gnu.org; Sun, 24 Sep 2023 03:42:23 -0400 Received: from relay5-d.mail.gandi.net ([217.70.183.197]:41127) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkJky-00069w-5l for 66113@debbugs.gnu.org; Sun, 24 Sep 2023 03:42:21 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id BBD091C0003; Sun, 24 Sep 2023 07:42:00 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> (Dmitry Gutov's message of "Sun, 24 Sep 2023 04:34:33 +0300") Organization: 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> <86lecwvjw4.fsf@mail.linkov.net> <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> Date: Sun, 24 Sep 2023 10:34:32 +0300 Message-ID: <86a5tcxbw7.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) >> +(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))))) > > Sorry, was there supposed to be a call to diff-test-hunk here? Or I'm just > not following the implementation. diff-test-hunk was just an example of implementation. But the same way as diff-apply-hunk is not used here, also only the logic of diff-test-hunk is used. > 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. From debbugs-submit-bounces@debbugs.gnu.org Sun Sep 24 06:59:28 2023 Received: (at 66113) by debbugs.gnu.org; 24 Sep 2023 10:59:28 +0000 Received: from localhost ([127.0.0.1]:41158 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkMpj-0003KH-Qn for submit@debbugs.gnu.org; Sun, 24 Sep 2023 06:59:28 -0400 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:56161) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkMpe-0003Js-5i for 66113@debbugs.gnu.org; Sun, 24 Sep 2023 06:59:26 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 361853200976; Sun, 24 Sep 2023 06:59:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 24 Sep 2023 06:59:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695553143; x=1695639543; bh=aClTR6OyUjNKx+0qdMftdisQJzZdj6HIDyT qNjjnPa8=; b=VRqOBG5t6U72MRNG7JsLQtFkJ2inFJ2RfXAnbWOezcpE99sQUfH xXkMrpo+9QmehKWVjNz1srtfcxZNJIkxE9/9vp+7lsruD+t3lbggGRq8U18itt6e I8CPN9lPgQn5CWO7X3ZybHFR+bDLVwzhnRneTGvuzosk9upxbAODZWW/6oR9HCOz pmPakCZeX+dJIyArK+ZWDpwAtJmt9Qy3EeufYJUgr5eC+kz13z/Rajfk7cwgP4GT jmqIA1XkOwKMEuv7EWHrvoN3YG+HfF7AgO0t292VvfxTC7y5xa015becAAzloLtA lnIKVVSlGRbMbKNtd+bR8GUoz2uP/1nEDSA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1695553143; x=1695639543; bh=aClTR6OyUjNKx+0qdMftdisQJzZdj6HIDyT qNjjnPa8=; b=VtTMopEKc7H/bnrWPviVFSIdYGkrnA+bPxS6UwA+ftNklA++kv5 55b2gjqnlh7LLmDx+KcjeWNuFfBMVbD+Ec9ohO7Kj0gArxaz7PW/8wm+15z325h7 ePMC177leAwsvz2yIl5Y950751k0dFR6zoc7bWCPclVfkiFcCgyIr8Z3h1tCevD8 rcJbwD6cIT7myERiDMkkKj3MM6oRClCLv45YZjLFSvBLO7SqaLV25fIk6jc6NThd 5sMI0qNMdvRn0AeJ/h34ZZUy76rIhkBVXI5Rx2AQqHhB3625yy9159+feRjA5Z8W HR92B8aEwOSr7t6PT0gIDpXsE4LodJUWA1g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudelvddgfeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhephfeutdekveeggeetteekfeejffegudduudfhueevleeftdffffeggeeivddv jeelnecuffhomhgrihhnpehgnhhurdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepughmihhtrhihsehguhhtohhvrdguvghv X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Sep 2023 06:59:02 -0400 (EDT) Message-ID: <57031fed-65d6-731b-756b-093a20065f1c@gutov.dev> Date: Sun, 24 Sep 2023 13:58:59 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: bug#66113: Apply the entire diff buffer Content-Language: en-US To: Juri Linkov References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> <86msxep69i.fsf@mail.linkov.net> <86lecwvjw4.fsf@mail.linkov.net> <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> <86a5tcxbw7.fsf@mail.linkov.net> From: Dmitry Gutov In-Reply-To: <86a5tcxbw7.fsf@mail.linkov.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.2 (--) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.2 (---) On 24/09/2023 10:34, Juri Linkov wrote: >> Sorry, was there supposed to be a call to diff-test-hunk here? Or I'm just >> not following the implementation. > diff-test-hunk was just an example of implementation. But the same way as > diff-apply-hunk is not used here, also only the logic of diff-test-hunk is used. Fair enough. >> 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 From debbugs-submit-bounces@debbugs.gnu.org Mon Sep 25 13:54:09 2023 Received: (at 66113) by debbugs.gnu.org; 25 Sep 2023 17:54:09 +0000 Received: from localhost ([127.0.0.1]:46595 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkpma-0003Np-SH for submit@debbugs.gnu.org; Mon, 25 Sep 2023 13:54:09 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:43941) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkpmZ-0003NK-Hh for 66113@debbugs.gnu.org; Mon, 25 Sep 2023 13:54:08 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 68DE660002; Mon, 25 Sep 2023 17:53:46 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: <57031fed-65d6-731b-756b-093a20065f1c@gutov.dev> (Dmitry Gutov's message of "Sun, 24 Sep 2023 13:58:59 +0300") Organization: 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> <86lecwvjw4.fsf@mail.linkov.net> <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> <86a5tcxbw7.fsf@mail.linkov.net> <57031fed-65d6-731b-756b-093a20065f1c@gutov.dev> Date: Mon, 25 Sep 2023 20:49:04 +0300 Message-ID: <86r0mmywhb.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) --=-=-= Content-Type: text/plain >>> 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: --=-=-= 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..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) --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Mon Sep 25 19:08:00 2023 Received: (at 66113) by debbugs.gnu.org; 25 Sep 2023 23:08:00 +0000 Received: from localhost ([127.0.0.1]:46874 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkugJ-0003Rq-Lm for submit@debbugs.gnu.org; Mon, 25 Sep 2023 19:07:59 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:57993) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkugG-0003Ra-L0 for 66113@debbugs.gnu.org; Mon, 25 Sep 2023 19:07:58 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 16E9F5C0D6D; Mon, 25 Sep 2023 19:07:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 25 Sep 2023 19:07:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gutov.dev; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1695683257; x=1695769657; bh=6KOPtB1I3alXahm0Ad8eoLZVijopiztga6h F6mrvqtw=; b=TFGVlCaOGekOryqxLywkDSzvvZZjscI00kLsdIonR1J/+kTfDQp e2SrCv4Wwi+BCgAPazhaw8RqXjBffOP+edUAbsA2TwvWwYvITSqTABNG0Snx6Prf K2HZ/7DjG2mqooa0cpH0QCYaerVwTDOa8HuvIzKtnSuGXiIkCwYcuCNAUikwZeuf YAo8zRK50mwAIlqyAJbbnjT8Xwr7tYwyEBHWBhFUjW1Puo6NLnpkQ504kKA0IGI5 0sKpRIxEAVNb64bfouiNbqG2wKvyDvLUMEB8J5YWXrAFE679RN4OnqRgFvTNGw5B GV21rjIAFSZhWwMKAMmo9YabkXZx6jvG20w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1695683257; x=1695769657; bh=6KOPtB1I3alXahm0Ad8eoLZVijopiztga6h F6mrvqtw=; b=RU7rIKPoY4x/bT2cdwZPGkIMyPGKGBEsq5TGTcGFrSbOyTZ74Q/ Y5Mo8bVvqtPCEMy9xN/UjTgOFn/fiBPgZE49ljwyUnIMHL6ERqBfMV51o8ZZDdWy WuNFCX/GA8ZEg7vdj03H9rH2ljvsP7Nof344kS80+Bm1qJ+gYuXOf9S/pm5A41cz aBBrCOnMHbSBz3UBzei46o9SITQoCFS/1RD+pU6PWdJGv+f7UOjb3UItn3lURGpI nyf0genY11pF9bi3HjwqCIh8WmRHSomam0nC+xJqIewAKsYUdhMC6QNj218Str/F Kkle5DzRnJfvv9t0DNHm7h3mHwuB0bFq2xw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudelhedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeffmhhi thhrhicuifhuthhovhcuoegumhhithhrhiesghhuthhovhdruggvvheqnecuggftrfgrth htvghrnhephfeutdekveeggeetteekfeejffegudduudfhueevleeftdffffeggeeivddv jeelnecuffhomhgrihhnpehgnhhurdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenuc frrghrrghmpehmrghilhhfrhhomhepughmihhtrhihsehguhhtohhvrdguvghv X-ME-Proxy: Feedback-ID: i0e71465a:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Sep 2023 19:07:35 -0400 (EDT) Message-ID: <9555856c-6b67-1806-3f76-b418825f0046@gutov.dev> Date: Tue, 26 Sep 2023 02:07:33 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: bug#66113: Apply the entire diff buffer Content-Language: en-US To: Juri Linkov References: <86o7hx4a11.fsf@mail.linkov.net> <86v8c2u34b.fsf@mail.linkov.net> <9cb6c329-8a81-414a-00e0-b3d8aa73d037@gutov.dev> <86msxep69i.fsf@mail.linkov.net> <86lecwvjw4.fsf@mail.linkov.net> <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> <86a5tcxbw7.fsf@mail.linkov.net> <57031fed-65d6-731b-756b-093a20065f1c@gutov.dev> <86r0mmywhb.fsf@mail.linkov.net> From: Dmitry Gutov In-Reply-To: <86r0mmywhb.fsf@mail.linkov.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Score: -2.2 (--) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.2 (---) On 25/09/2023 20:49, Juri Linkov wrote: >>>> 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: That works. Thanks! Ideally, I think point would move to the first failing hunk. But that's not urgent, could be a TODO for later. Aside from that, I'm still not crazy about the binding. But that's not a technical issue. From debbugs-submit-bounces@debbugs.gnu.org Wed Sep 27 13:41:28 2023 Received: (at 66113) by debbugs.gnu.org; 27 Sep 2023 17:41:28 +0000 Received: from localhost ([127.0.0.1]:52462 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qlYXQ-0006Sf-0f for submit@debbugs.gnu.org; Wed, 27 Sep 2023 13:41:28 -0400 Received: from relay6-d.mail.gandi.net ([217.70.183.198]:54793) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qlYXA-0006Rm-Dh; Wed, 27 Sep 2023 13:41:13 -0400 Received: by mail.gandi.net (Postfix) with ESMTPSA id 85343C0003; Wed, 27 Sep 2023 17:40:49 +0000 (UTC) From: Juri Linkov To: Dmitry Gutov Subject: Re: bug#66113: Apply the entire diff buffer In-Reply-To: <9555856c-6b67-1806-3f76-b418825f0046@gutov.dev> (Dmitry Gutov's message of "Tue, 26 Sep 2023 02:07:33 +0300") Organization: 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> <86lecwvjw4.fsf@mail.linkov.net> <095e4596-7c67-21e2-a69f-c0b7945773a3@gutov.dev> <86a5tcxbw7.fsf@mail.linkov.net> <57031fed-65d6-731b-756b-093a20065f1c@gutov.dev> <86r0mmywhb.fsf@mail.linkov.net> <9555856c-6b67-1806-3f76-b418825f0046@gutov.dev> Date: Wed, 27 Sep 2023 20:36:49 +0300 Message-ID: <86ttrfmsb2.fsf@mail.linkov.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain X-GND-Sasl: juri@linkov.net X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 66113 Cc: 66113@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) close 66113 30.0.50 thanks >> ;; 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: > > That works. Thanks! Actually this doesn't work for bzr that has a bug that adds an extra line at the end. So I pushed a better fix. > Ideally, I think point would move to the first failing hunk. But that's not > urgent, could be a TODO for later. There are many variants what would be better to do here. Maybe also to show all failing hunks. And leaving point at the beginning also makes sense, because then the user could start applying hunks one by one with 'C-c C-a' from the beginning until encountering the failing hunk and handle it manually. > Aside from that, I'm still not crazy about the binding. But that's not > a technical issue. Agreed, the key binding is the best among bad variants. From unknown Sun Jun 15 08:52:56 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Thu, 26 Oct 2023 11:24:07 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator