[PATCH] internal_diff incorrectly handles sequences lacking trailing newlines

Aaron Bentley aaron.bentley at utoronto.ca
Wed Jul 27 17:48:37 BST 2005


-----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.

Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFC57rl0F+nu1YWqI0RAp2cAJ9c2YRKsEC+bxCNk116bUUdliqHOgCfRs61
WWAyIPOl/qoK2ns3fIvC+0I=
=y4K6
-----END PGP SIGNATURE-----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newline-fix.patch
Type: text/x-patch
Size: 2436 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050727/1644321e/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newline-fix-fix.patch
Type: text/x-patch
Size: 2687 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050727/1644321e/attachment-0001.bin 


More information about the bazaar mailing list