add git_am_patch_split.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 16 Apr 2010 00:49:03 +0000 (02:49 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 16 Apr 2010 00:49:03 +0000 (02:49 +0200)
dulwich/patch.py
dulwich/tests/test_patch.py

index b96c0399e1873d1f6ca47ea3224327a6bf804c77..75e5c374f8d93107e16e170f71b41c5b37b31598 100644 (file)
@@ -23,9 +23,13 @@ on.
 """
 
 from difflib import SequenceMatcher
+import rfc822
 import subprocess
 import time
 
+from dulwich.objects import (
+    Commit,
+    )
 
 def write_commit_patch(f, commit, contents, progress, version=None):
     """Write a individual file patch.
@@ -134,3 +138,32 @@ def write_blob_diff(f, (old_path, old_mode, old_blob),
     new_contents = lines(new_blob)
     f.writelines(unified_diff(old_contents, new_contents, 
         old_path, new_path))
+
+
+def git_am_patch_split(f):
+    """Parse a git-am-style patch and split it up into bits.
+
+    :param f: File-like object to parse
+    :return: Tuple with commit object, diff contents and git version
+    """
+    msg = rfc822.Message(f)
+    c = Commit()
+    c.author = msg["from"]
+    c.committer = msg["from"]
+    if msg["subject"].startswith("[PATCH "):
+        subject = msg["subject"].split("]", 1)[1]
+    else:
+        subject = msg["subject"]
+    c.message = subject
+    for l in f:
+        if l == "---\n":
+            break
+        c.message += l
+    diff = ""
+    for l in f:
+        if l == "-- \n":
+            break
+        diff += l
+    version = f.readline().rstrip("\n")
+    assert f.read() == ""
+    return c, diff, version
index 19e42db18a30c216f544b16537fec19c5722cfe7..76975aacb6941a80c3ea362f9e6dcbd08310a471 100644 (file)
@@ -26,6 +26,7 @@ from dulwich.objects import (
     Tree,
     )
 from dulwich.patch import (
+    git_am_patch_split,
     write_commit_patch,
     )
 
@@ -59,3 +60,30 @@ class WriteCommitPatchTests(TestCase):
         if len(lines) >= 12:
             # diffstat may not be present
             self.assertEquals(lines[8], " 0 files changed\n")
+
+
+class ReadGitAmPatch(TestCase):
+
+    def test_extract(self):
+        text = """From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
+From: Jelmer Vernooij <jelmer@samba.org>
+Date: Thu, 15 Apr 2010 15:40:28 +0200
+Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a lintian warning).
+
+---
+ pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
+ 1 files changed, 0 insertions(+), 0 deletions(-)
+ mode change 100755 => 100644 pixmaps/prey.ico
+
+-- 
+1.7.0.4
+"""
+        c, diff, version = git_am_patch_split(StringIO(text))
+        self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.committer)
+        self.assertEquals("Jelmer Vernooij <jelmer@samba.org>", c.author)
+        self.assertEquals(""" pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
+ 1 files changed, 0 insertions(+), 0 deletions(-)
+ mode change 100755 => 100644 pixmaps/prey.ico
+
+""", diff)
+        self.assertEquals("1.7.0.4", version)