bug#81395: 32.0.50; diff-apply-hunk should be case-sensitive

Ihor Radchenko <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <87wlufq766.fsf@localhost>
Stefan Monnier <[email protected]> writes:

>> Fair. Especially considering my reproducer. However, I ran into case
>> sensitivity issue when the diff was on a single file, without any
>> ambiguity.
>>
>> I can try to produce a cleaner reproducer, but I think it is clear that
>> the code does case-insensitive regexp search.
>
> Indeed the matching is case-insensitive.  I remember making this
> decision consciously.  🙂
> I'm OK if you make it case-sensitive, tho.

See the attached patch.

>> 2. Create test.diff
>> diff -u /tmp/test.txt /tmp/test2.txt
>> --- /tmp/test.txt	2026-07-26 11:42:55.175090850 +0200
>> +++ /tmp/test.txt	2026-07-26 11:43:11.705090601 +0200
>> @@ -6,4 +6,4 @@
>>  
>>  bar
>>  
>> -This is test.
>> +This is test2.
>>
>> Diff finished.  Sun Jul 26 11:43:23 2026
>>
>> 3. emacs -Q /tmp/test.diff
>> 4. C-c C-a
>> 5. Observe "hunk already applied".
>
> Hmm... so single-stepping through `diff-find-source-location` shows that
> it finds both the "old" text and the "new" text, so it sees that the
> patch can be applied *and* it can be reverse-applied.
> Then it uses the funny:
>
>     (and maybe-new maybe-old (null reverse) (setq switched t) maybe-new)
>
> to decide that it should say "hunk already applied" (which it does by
> setting SWITCHED to t).  I can't remember enough what REVERSE means
> here, but it looks like a plain bug: either we should test `reverse`
> rather than `(null reverse)` or maybe the caller should pass `(not
> reverse)` or something.
>
> I guess we could also check the "offset" between the diff's line-number
> and the place where we found the corresponding text to see which of OLD
> or NEW is closest to what the hunk describes (if it's exactly at the
> right place, it's more likely to be what was meant), but if we apply
> hunks one by one starting from the beginning, then the line numbers end
> up not matching exactly anyway (because of the changes made from previous
> hunks), so distrusting approximate matches is not a panacea.

May we keep track of the applied hunks in some way? For example,
applying hunks can be recorded, so that line offsets can be tracked.


-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
0001-lisp-vc-diff-mode.el-diff-find-text-Match-case-sensi.patch (text/x-patch, 915 B)
From 15f478218ebc9245caffe86916b0f1c7543aaf86 Mon Sep 17 00:00:00 2001
Message-ID: <15f478218ebc9245caffe86916b0f1c7543aaf86.1785264195.git.yantar92@posteo.net>
From: Ihor Radchenko <[email protected]>
Date: Tue, 28 Jul 2026 20:42:30 +0200
Subject: [PATCH] lisp/vc/diff-mode.el (diff-find-text): Match case-sensitively
 (bug#81395)

---
 lisp/vc/diff-mode.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index c1227573351..89052dbc7cd 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2069,6 +2069,7 @@ diff-find-text
   "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
 If TEXT isn't found, nil is returned."
   (let* ((orig (point))
+         (case-fold-search nil)
 	 (forw (and (search-forward text nil t)
 		    (cons (match-beginning 0) (match-end 0))))
 	 (back (and (goto-char (+ orig (length text)))
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.