[PATCH] internal_diff incorrectly handles sequences lacking trailing newlines

Robert Collins robertc at robertcollins.net
Fri Sep 23 10:46:42 BST 2005


On Wed, 2005-07-27 at 12:48 -0400, Aaron Bentley wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi all,
> 
> If you ask internal_diff to produce the difference between ['boo'] and
> ['boo\n'], you get ''\\ No newline at end of file\n'.  No patch header,
> no nothing.
> 
> Because internal_diff appends a \n to lines that lack them, there is no
> difference between the sequences passed to difflib, so no patch is
> produced.  So we just get the '\\ No newline at end of file\n'.
> 
> Here's what diff produces:
> - --- boo2        2005-07-27 11:28:24.000000000 -0400
> +++ boo1        2005-07-27 11:28:16.000000000 -0400
> @@ -1 +1 @@
> - -boo
> \ No newline at end of file
> +boo
> 
> So we have no patch, but one of the files lacks a terminating newline,
> so we get a \ No newline at end of file marker.
> 
> Let's introduce a change in the text and see if internal_diff gets it
> right.  We'll use ['boo'] and ['goo\n']
> 
> >>> diff.internal_diff('boo2', ['boo'], 'boo1', ['goo\n'], sys.stdout)
> - --- boo2
> +++ boo1
> @@ -1,1 +1,1 @@
> - -boo
> +goo
> \ No newline at end of file
> 
> Still wrong; the '\ No newline at end of file' string must appear in the
> middle of the unified diff to match the example generated by diff.
> Worse yet, you get an identical patch if you move the newline to the
> opposite text:
> 
> >>> diff.internal_diff('boo2', ['boo\n'], 'boo1', ['goo'], sys.stdout)
> - --- boo2
> +++ boo1
> @@ -1,1 +1,1 @@
> - -boo
> +goo
> \ No newline at end of file
> 
> It would appear that internal_diff doesn't really understand what '\ No
> newline at end of file' really does.  It's a line terminator.  Lines
> whose real contents end with a newline are terminated with '\n'.  Lines
> whose real contents don't end with a newline are terminated with
> '\n\\ No newline at end of file\n'.
> 
> I have attached patches that fix this behavior, with unit tests.

Martin, was this merged ?

Rob


-- 
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050923/c417034a/attachment.pgp 


More information about the bazaar mailing list