Fix trailing newlines in generated patch files.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 12 Mar 2010 16:35:03 +0000 (17:35 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 12 Mar 2010 16:35:03 +0000 (17:35 +0100)
NEWS
dulwich/patch.py

diff --git a/NEWS b/NEWS
index 094c4baf10c019e16dac5babeb3de4c7ae25e806..778d1a6852025a8bce04ea87a84620bfd57d72ac 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@
   * Deal with capabilities required by the client, even if they 
     can not be disabled in the server. (Dave Borowitz)
 
+  * Fix trailing newlines in generated patch files.
+    (Jelmer Vernooij)
+
  FEATURES
 
   * Add include-tag capability to server. (Dave Borowitz)
@@ -25,7 +28,7 @@
 
  BUG FIXES
 
-  * Support custom fields in commits.
+  * Support custom fields in commits (readonly). (Jelmer Vernooij)
 
   * Improved ref handling. (Dave Borowitz)
 
 
  FEATURES
 
-  * Add ObjectStore.iter_tree_contents()
+  * Add ObjectStore.iter_tree_contents(). (Jelmer Vernooij)
 
-  * Add Index.changes_from_tree()
+  * Add Index.changes_from_tree(). (Jelmer Vernooij)
 
-  * Add ObjectStore.tree_changes()
+  * Add ObjectStore.tree_changes(). (Jelmer Vernooij)
 
   * Add functionality for writing patches in dulwich.patch.
+    (Jelmer Vernooij)
 
 0.4.0  2009-10-07
 
index 6693b564c6d765ade6a93f0f1dd5ebea49405830..3c934d713540d80ac16351dad6ec44a8acac1842 100644 (file)
@@ -22,7 +22,7 @@ These patches are basically unified diffs with some extra metadata tacked
 on.
 """
 
-import difflib
+from difflib import SequenceMatcher
 import subprocess
 import time
 
@@ -68,6 +68,32 @@ def get_summary(commit):
     return commit.message.splitlines()[0].replace(" ", "-")
 
 
+def unified_diff(a, b, fromfile='', tofile='', n=3, lineterm='\n'):
+    """difflib.unified_diff that doesn't write any dates or trailing spaces.
+
+    Based on the same function in Python2.6.5-rc2's difflib.py
+    """
+    started = False
+    for group in SequenceMatcher(None, a, b).get_grouped_opcodes(3):
+        if not started:
+            yield '--- %s\n' % fromfile
+            yield '+++ %s\n' % tofile
+            started = True
+        i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
+        yield "@@ -%d,%d +%d,%d @@\n" % (i1+1, i2-i1, j1+1, j2-j1)
+        for tag, i1, i2, j1, j2 in group:
+            if tag == 'equal':
+                for line in a[i1:i2]:
+                    yield ' ' + line
+                continue
+            if tag == 'replace' or tag == 'delete':
+                for line in a[i1:i2]:
+                    yield '-' + line
+            if tag == 'replace' or tag == 'insert':
+                for line in b[j1:j2]:
+                    yield '+' + line
+
+
 def write_blob_diff(f, (old_path, old_mode, old_blob), 
                        (new_path, new_mode, new_blob)):
     """Write diff file header.
@@ -106,5 +132,5 @@ def write_blob_diff(f, (old_path, old_mode, old_blob),
         blob_id(old_blob), blob_id(new_blob), new_mode))
     old_contents = lines(old_blob)
     new_contents = lines(new_blob)
-    f.writelines(difflib.unified_diff(old_contents, new_contents, 
+    f.writelines(unified_diff(old_contents, new_contents, 
         old_path, new_path))