Support custom fields in commits.
authorJelmer Vernooij <jelmer@samba.org>
Thu, 21 Jan 2010 01:11:41 +0000 (14:11 +1300)
committerJelmer Vernooij <jelmer@samba.org>
Thu, 21 Jan 2010 01:11:41 +0000 (14:11 +1300)
NEWS
dulwich/__init__.py
dulwich/objects.py
dulwich/tests/test_objects.py
setup.py

diff --git a/NEWS b/NEWS
index f5d23822580b201273e12c3204c8578b55f08472..63069b26484e75e730239b0c500599434676c5d5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,9 @@
+0.4.2  UNRELEASED
+
+ BUG FIXES
+
+  * Support custom fields in commits.
+
 0.4.1  2010-01-03
 
  FEATURES
index 0f66cc0124f9539d4f41074a4cea83ee67db3458..442e6f66e1c02866e280c42c49cbccb867d65302 100644 (file)
@@ -27,4 +27,4 @@ import protocol
 import repo
 import server
 
-__version__ = (0, 4, 1)
+__version__ = (0, 4, 2)
index bbaba9e94ca37f024965642ebec4625006662cc8..1d11c1cdf58f06742962554cac7e33533cdd736f 100644 (file)
@@ -512,6 +512,7 @@ class Commit(ShaFile):
         self._encoding = None
         self._needs_parsing = False
         self._needs_serialization = True
+        self._extra = {}
 
     @classmethod
     def from_file(cls, filename):
@@ -522,6 +523,7 @@ class Commit(ShaFile):
 
     def _parse_text(self):
         self._parents = []
+        self._extra = {}
         self._author = None
         f = StringIO(self._text)
         for l in f:
@@ -545,7 +547,9 @@ class Commit(ShaFile):
             elif field == ENCODING_ID:
                 self._encoding = value
             else:
-                raise AssertionError("Unknown field %s" % field)
+                if field in self._extra:
+                    raise KeyError("%s already exists in extra" % field)
+                self._extra[field] = value
         self._message = f.read()
         self._needs_parsing = False
 
@@ -558,6 +562,10 @@ class Commit(ShaFile):
         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))
+        for k in sorted(self.extra.keys()):
+            if "\n" in k or "\n" in self.extra[k]:
+                raise AssertionError("newline in extra data: %r -> %r" % (k, self.extra[k]))
+            f.write("%s %s\n" % (k, self.extra[k]))
         f.write("\n") # There must be a new line after the headers
         f.write(self._message)
         self._text = f.getvalue()
@@ -578,6 +586,18 @@ class Commit(ShaFile):
 
     parents = property(get_parents, set_parents)
 
+    def get_extra(self):
+        """Return extra settings of this commit."""
+        self._ensure_parsed()
+        return self._extra
+
+    def set_extra(self, value):
+        self._ensure_parsed()
+        self._needs_serialization = True
+        self._extra = value
+
+    extra = property(get_extra, set_extra)
+
     author = serializable_property("author",
         "The name of the author of the commit")
 
index baea3b12389a4faaa23fa2636244e3b8400b74f2..38a7ac823d75c6fe4fbe946a50669647c777229f 100644 (file)
@@ -225,6 +225,18 @@ class CommitDeserializationTests(unittest.TestCase):
                           '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
             c.parents)
 
+    def test_custom(self):
+        c = Commit.from_string(
+                'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
+                'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
+                'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
+                'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
+                'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
+                'extra-field data\n'
+                '\n'
+                'Merge ../b\n')
+        self.assertEquals('data', c.extra['extra-field'])
+
 
 class TreeSerializationTests(unittest.TestCase):
 
index 94aba228935321d0bb6e60d80f2090a5588ee7c4..2bac894e436b4804d8edeba81a1dff373550f1fd 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -8,7 +8,7 @@ except ImportError:
     from distutils.core import setup
 from distutils.extension import Extension
 
-dulwich_version_string = '0.4.1'
+dulwich_version_string = '0.4.2'
 
 include_dirs = []
 # Windows MSVC support