unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#11894: 24.1.50; [PATCH] diff-apply-hunk can be off by 1 line when the hunk is 0-context pure removal
@ 2012-07-10  1:46 Dmitry Gutov
  2012-07-18 12:53 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Gutov @ 2012-07-10  1:46 UTC (permalink / raw)
  To: 11894

[-- Attachment #1: Type: text/plain, Size: 1116 bytes --]

This applies both to context and unified diffs.

To get a 0-context hunk, you invoke diff command with -c0 or -U0 argument.

1. Example files (4 and 3 lines long):

test:
z
abc

def

test2:
z
abc
def

2. Run `diff -c0 test test2 > test.diff`:
*** test	2012-07-09 06:04:04.572209000 +0400
--- test2	2012-07-09 06:04:11.987150600 +0400
***************
*** 3 ****
-
--- 2 ----

3. Open test.diff in Emacs, then:
a) Do `C-u C-c C-a' (reverse hunk) -> see the empty line appear after 
"z", instead of after "abc".
b) Open test2, add empty line after "abc", go to test.diff window, do 
`C-c C-a' (apply hunk), see the newline between "z" and "abc" disappear 
instead.

Not sure if we can rely on the line number being always off by 1 in such 
hunks (there's no insertion, so, technically, the second line number in 
the header could be arbitrary), but at least 3 versions of diff across 2 
different OSes work the same in this regard.

Note that if you try to create such hunk with `diff-split-hunk' (by 
slicing it off a bigger hunk), the line number won't be off by 1.
Maybe that's a bug in `diff-split-hunk'.

--Dmitry

[-- Attachment #2: diff-mode.diff --]
[-- Type: text/plain, Size: 817 bytes --]

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 9034ffe..e27b60f 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -1675,7 +1675,11 @@ NOPROMPT, if non-nil, means not to prompt the user."
       (when (> (prefix-numeric-value other-file) 8)
 	(setq diff-jump-to-old-file other))
       (with-current-buffer buf
-        (goto-char (point-min)) (forward-line (1- (string-to-number line)))
+        (let ((line-num (string-to-number line)))
+          ;; When the hunk is pure deletion, line number is off by 1.
+          (when (string= (if reverse (car old) (car new)) "")
+            (incf line-num))
+          (goto-char (point-min)) (forward-line (1- line-num)))
 	(let* ((orig-pos (point))
 	       (switched nil)
 	       ;; FIXME: Check for case where both OLD and NEW are found.

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-07-19  8:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-10  1:46 bug#11894: 24.1.50; [PATCH] diff-apply-hunk can be off by 1 line when the hunk is 0-context pure removal Dmitry Gutov
2012-07-18 12:53 ` Stefan Monnier
2012-07-18 13:28   ` Andreas Schwab
2012-07-19  7:58     ` Stefan Monnier
2012-07-19  8:40       ` Andreas Schwab
2012-07-18 17:25   ` Dmitry Gutov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).