From: Jelmer Vernooij Date: Fri, 16 Apr 2010 00:49:03 +0000 (+0200) Subject: add git_am_patch_split. X-Git-Tag: dulwich-0.6.0~33 X-Git-Url: http://git.samba.org/samba.git/?p=jelmer%2Fdulwich-libgit2.git;a=commitdiff_plain;h=1a19cb757b7c0cf51f9d2c80b0af51e05c54481d add git_am_patch_split. --- diff --git a/dulwich/patch.py b/dulwich/patch.py index b96c039..75e5c37 100644 --- a/dulwich/patch.py +++ b/dulwich/patch.py @@ -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 diff --git a/dulwich/tests/test_patch.py b/dulwich/tests/test_patch.py index 19e42db..76975aa 100644 --- a/dulwich/tests/test_patch.py +++ b/dulwich/tests/test_patch.py @@ -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 +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 ", c.committer) + self.assertEquals("Jelmer Vernooij ", 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)