Browse Source

Support the encoding field in commit messages.

Jelmer Vernooij 16 years ago
parent
commit
2a572c6aef
2 changed files with 14 additions and 0 deletions
  1. 9 0
      dulwich/objects.py
  2. 5 0
      dulwich/tests/test_objects.py

+ 9 - 0
dulwich/objects.py

@@ -49,6 +49,7 @@ COMMITTER_ID = "committer"
 OBJECT_ID = "object"
 TYPE_ID = "type"
 TAGGER_ID = "tagger"
+ENCODING_ID = "encoding"
 
 S_IFGITLINK	= 0160000
 def S_ISGITLINK(m):
@@ -491,6 +492,7 @@ class Commit(ShaFile):
     def __init__(self):
         super(Commit, self).__init__()
         self._parents = []
+        self._encoding = None
         self._needs_parsing = False
         self._needs_serialization = True
 
@@ -523,6 +525,8 @@ class Commit(ShaFile):
                 self._committer, timetext, timezonetext = value.rsplit(" ", 2)
                 self._commit_time = int(timetext)
                 self._commit_timezone = parse_timezone(timezonetext)
+            elif field == ENCODING_ID:
+                self._encoding = value
             else:
                 raise AssertionError("Unknown field %s" % field)
         self._message = f.read()
@@ -535,6 +539,8 @@ class Commit(ShaFile):
             f.write("%s %s\n" % (PARENT_ID, p))
         f.write("%s %s %s %s\n" % (AUTHOR_ID, self._author, str(self._author_time), format_timezone(self._author_timezone)))
         f.write("%s %s %s %s\n" % (COMMITTER_ID, self._committer, str(self._commit_time), format_timezone(self._commit_timezone)))
+        if self.encoding:
+            f.write("%s %s\n" % (ENCODING_ID, self.encoding))
         f.write("\n") # There must be a new line after the headers
         f.write(self._message)
         self._text = f.getvalue()
@@ -576,6 +582,9 @@ class Commit(ShaFile):
     author_timezone = serializable_property("author_timezone", 
         "Returns the zone the author time is in.")
 
+    encoding = serializable_property("encoding",
+        "Encoding of the commit message.")
+
 
 type_map = {
     BLOB_ID : Blob,

+ 5 - 0
dulwich/tests/test_objects.py

@@ -158,6 +158,11 @@ class CommitSerializationTests(unittest.TestCase):
         c.message =  'Merge ../b\n'
         return c
 
+    def test_encoding(self):
+        c = self.make_base()
+        c.encoding = "iso8859-1"
+        self.assertTrue("encoding iso8859-1\n" in c.as_raw_string())        
+
     def test_short_timestamp(self):
         c = self.make_base()
         c.commit_time = 30