Subject: Re: bug#9597: 23.3; [diff-mode] Can't quite cope with git patches

From: Juri Linkov <[email-masked]>
Date: Sun, 25 Sep 2011 16:29:02 +0300

> When I try to apply the last diff in the enclosed file, I get the
> following backtrace:
>
> Debugger entered--Lisp error: (error "End of hunk ambiguously marked")
>   signal(error ("End of hunk ambiguously marked"))
>   error("End of hunk ambiguously marked")
>   diff-sanity-check-hunk()
>   diff-find-source-location(nil nil)
>   diff-apply-hunk(nil)
>   call-interactively(diff-apply-hunk nil nil)
>
> Seems to me that diff-mode should be able to handle the output of
> git-format-patch; after all, the patch command can.

bug#5302 reports the same problem with Bzr diffs.

In both cases diff separators (a line of dashes)
are ignored by `diff-sanity-check-hunk'.

Both bugs can be fixed with the patch below.

I suppose that modifying the buffer with inserting a newline
is a temporary workaround for some earlier bug, so I added
a check for the diff separator in another condition branch.
The comment says that adding an empty line is for code which
doesn't count lines.  I don't know where this code is located,
but perhaps this code should also check for diff separators?

=== modified file 'lisp/vc/diff-mode.el'
--- lisp/vc/diff-mode.el	2011-05-22 19:22:37 +0000
+++ lisp/vc/diff-mode.el	2011-09-25 13:28:50 +0000
@@ -429,6 +429,9 @@ (defvar diff-valid-unified-empty-line t
 (defconst diff-hunk-header-re
   (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
 (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1)))
+
+(defconst diff-separator-re "^--+ ?$")
+
 (defvar diff-narrowed-to nil)
 
 (defun diff-hunk-style (&optional style)
@@ -1414,15 +1417,20 @@ (defun diff-sanity-check-hunk ()
                 (case (char-after)
                   (?\s (decf before) (decf after) t)
                   (?-
-                   (if (and (looking-at diff-file-header-re)
-                            (zerop before) (zerop after))
-                       ;; No need to query: this is a case where two patches
-                       ;; are concatenated and only counting the lines will
-                       ;; give the right result.  Let's just add an empty
-                       ;; line so that our code which doesn't count lines
-                       ;; will not get confused.
-                       (progn (save-excursion (insert "\n")) nil)
-                     (decf before) t))
+                   (cond
+                    ((and (looking-at diff-separator-re)
+                          (zerop before) (zerop after))
+                     nil)
+                    ((and (looking-at diff-file-header-re)
+                          (zerop before) (zerop after))
+                     ;; No need to query: this is a case where two patches
+                     ;; are concatenated and only counting the lines will
+                     ;; give the right result.  Let's just add an empty
+                     ;; line so that our code which doesn't count lines
+                     ;; will not get confused.
+                     (save-excursion (insert "\n")) nil)
+                    (t
+                     (decf before) t)))
                   (?+ (decf after) t)
                   (t
                    (cond