Bläddra i källkod

Support Python3 in write_commit_patch.

Jelmer Vernooij 10 år sedan
förälder
incheckning
d0d4c94d4f
2 ändrade filer med 28 tillägg och 26 borttagningar
  1. 14 11
      dulwich/patch.py
  2. 14 15
      dulwich/tests/test_patch.py

+ 14 - 11
dulwich/patch.py

@@ -35,20 +35,23 @@ from dulwich.objects import (
 FIRST_FEW_BYTES = 8000
 
 
-def write_commit_patch(f, commit, contents, progress, version=None):
+def write_commit_patch(f, commit, contents, progress, version=None, encoding=None):
     """Write a individual file patch.
 
     :param commit: Commit object
     :param progress: Tuple with current patch number and total.
     :return: tuple with filename and contents
     """
+    encoding = getattr(f, "encoding", encoding) or "ascii"
+    if type(contents) is str:
+        contents = contents.encode(encoding)
     (num, total) = progress
-    f.write("From %s %s\n" % (commit.id, time.ctime(commit.commit_time)))
-    f.write("From: %s\n" % commit.author)
-    f.write("Date: %s\n" % time.strftime("%a, %d %b %Y %H:%M:%S %Z"))
-    f.write("Subject: [PATCH %d/%d] %s\n" % (num, total, commit.message))
-    f.write("\n")
-    f.write("---\n")
+    f.write(b"From " + commit.id + b" " + time.ctime(commit.commit_time).encode(encoding) + b"\n")
+    f.write(b"From: " + commit.author + b"\n")
+    f.write(b"Date: " + time.strftime("%a, %d %b %Y %H:%M:%S %Z").encode(encoding) + b"\n")
+    f.write(("Subject: [PATCH %d/%d] " % (num, total)).encode(encoding) + commit.message + b"\n")
+    f.write(b"\n")
+    f.write(b"---\n")
     try:
         import subprocess
         p = subprocess.Popen(["diffstat"], stdout=subprocess.PIPE,
@@ -58,14 +61,14 @@ def write_commit_patch(f, commit, contents, progress, version=None):
     else:
         (diffstat, _) = p.communicate(contents)
         f.write(diffstat)
-        f.write("\n")
+        f.write(b"\n")
     f.write(contents)
-    f.write("-- \n")
+    f.write(b"-- \n")
     if version is None:
         from dulwich import __version__ as dulwich_version
-        f.write("Dulwich %d.%d.%d\n" % dulwich_version)
+        f.write(b"Dulwich %d.%d.%d\n" % dulwich_version)
     else:
-        f.write("%s\n" % version)
+        f.write(version.encode(encoding) + b"\n")
 
 
 def get_summary(commit):

+ 14 - 15
dulwich/tests/test_patch.py

@@ -45,35 +45,34 @@ from dulwich.tests.utils import (
     )
 
 
-@skipIfPY3
 class WriteCommitPatchTests(TestCase):
 
-    def test_simple(self):
+    def test_simple_bytesio(self):
         f = BytesIO()
         c = Commit()
-        c.committer = c.author = "Jelmer <jelmer@samba.org>"
+        c.committer = c.author = b"Jelmer <jelmer@samba.org>"
         c.commit_time = c.author_time = 1271350201
         c.commit_timezone = c.author_timezone = 0
-        c.message = "This is the first line\nAnd this is the second line.\n"
+        c.message = b"This is the first line\nAnd this is the second line.\n"
         c.tree = Tree().id
         write_commit_patch(f, c, "CONTENTS", (1, 1), version="custom")
         f.seek(0)
         lines = f.readlines()
-        self.assertTrue(lines[0].startswith("From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
-        self.assertEqual(lines[1], "From: Jelmer <jelmer@samba.org>\n")
-        self.assertTrue(lines[2].startswith("Date: "))
+        self.assertTrue(lines[0].startswith(b"From 0b0d34d1b5b596c928adc9a727a4b9e03d025298"))
+        self.assertEqual(lines[1], b"From: Jelmer <jelmer@samba.org>\n")
+        self.assertTrue(lines[2].startswith(b"Date: "))
         self.assertEqual([
-            "Subject: [PATCH 1/1] This is the first line\n",
-            "And this is the second line.\n",
-            "\n",
-            "\n",
-            "---\n"], lines[3:8])
+            b"Subject: [PATCH 1/1] This is the first line\n",
+            b"And this is the second line.\n",
+            b"\n",
+            b"\n",
+            b"---\n"], lines[3:8])
         self.assertEqual([
-            "CONTENTS-- \n",
-            "custom\n"], lines[-2:])
+            b"CONTENTS-- \n",
+            b"custom\n"], lines[-2:])
         if len(lines) >= 12:
             # diffstat may not be present
-            self.assertEqual(lines[8], " 0 files changed\n")
+            self.assertEqual(lines[8], b" 0 files changed\n")
 
 
 @skipIfPY3