Prechádzať zdrojové kódy

Allow users to change object types, use helper functions for setting raw strings.

Jelmer Vernooij 16 rokov pred
rodič
commit
fa98db8ec8
3 zmenil súbory, kde vykonal 26 pridanie a 13 odobranie
  1. 1 1
      dulwich/object_store.py
  2. 21 9
      dulwich/objects.py
  3. 4 3
      dulwich/pack.py

+ 1 - 1
dulwich/object_store.py

@@ -133,7 +133,7 @@ class ObjectStore(object):
             hexsha = sha_to_hex(name)
         ret = self._get_shafile(hexsha)
         if ret is not None:
-            return ret.as_raw_string()
+            return ret.type, ret.as_raw_string()
         raise KeyError(hexsha)
 
     def __getitem__(self, sha):

+ 21 - 9
dulwich/objects.py

@@ -90,14 +90,22 @@ class ShaFile(object):
         object._size = size
         assert text[0] == "\0", "Size not followed by null"
         text = text[1:]
-        object._text = text
+        object.set_raw_string(text)
         return object
 
     def as_legacy_object(self):
         return zlib.compress("%s %d\0%s" % (self._type, len(self._text), self._text))
   
     def as_raw_string(self):
-        return self._num_type, self._text
+        if self._needs_serialization:
+            self.serialize()
+        return self._text
+
+    def set_raw_string(self, text):
+        self._text = text
+        self._needs_parsing = True
+        self._needs_serialization = False
+        self._parse_text()
   
     @classmethod
     def _parse_object(cls, map):
@@ -114,7 +122,7 @@ class ShaFile(object):
             byte = ord(map[used])
             used += 1
         raw = map[used:]
-        object._text = _decompress(raw)
+        object.set_raw_string(_decompress(raw))
         return object
   
     @classmethod
@@ -153,9 +161,8 @@ class ShaFile(object):
         """
         real_class = num_type_map[type]
         obj = real_class()
-        obj._num_type = type
-        obj._text = string
-        obj._parse_text()
+        obj.type = type
+        obj.set_raw_string(string)
         return obj
   
     def _header(self):
@@ -175,7 +182,10 @@ class ShaFile(object):
     def get_type(self):
         return self._num_type
 
-    type = property(get_type)
+    def set_type(self, type):
+        self._num_type = type
+
+    type = property(get_type, set_type)
   
     def __repr__(self):
         return "<%s %s>" % (self.__class__.__name__, self.id)
@@ -211,7 +221,7 @@ class Blob(ShaFile):
     def from_string(cls, string):
         """Create a blob from a string."""
         shafile = cls()
-        shafile._text = string
+        shafile.set_raw_string(string)
         return shafile
 
 
@@ -232,7 +242,7 @@ class Tag(ShaFile):
     def from_string(cls, string):
         """Create a blob from a string."""
         shafile = cls()
-        shafile._text = string
+        shafile.set_raw_string(string)
         return shafile
 
     def _parse_text(self):
@@ -411,6 +421,7 @@ class Tree(ShaFile):
         self._text = ""
         for name, mode, hexsha in self.iteritems():
             self._text += "%04o %s\0%s" % (mode, name, hex_to_sha(hexsha))
+        self._needs_serialization = False
 
 
 class Commit(ShaFile):
@@ -518,6 +529,7 @@ class Commit(ShaFile):
         self._text += "%s %s %s %+05d\n" % (COMMITTER_ID, self._committer, str(self._commit_time), self._commit_timezone)
         self._text += "\n" # There must be a new line after the headers
         self._text += self._message
+        self._needs_serialization = False
 
     def get_tree(self):
         """Returns the tree that is the state of this commit"""

+ 4 - 3
dulwich/pack.py

@@ -680,7 +680,7 @@ def write_pack_data(f, objects, num_objects, window=10):
     # This helps us find good objects to diff against us
     magic = []
     for obj, path in recency:
-        magic.append( (obj.type, path, 1, -len(obj.as_raw_string()[1]), obj) )
+        magic.append( (obj.type, path, 1, -len(obj.as_raw_string()), obj) )
     magic.sort()
     # Build a map of objects and their index in magic - so we can find preceeding objects
     # to diff against
@@ -695,14 +695,15 @@ def write_pack_data(f, objects, num_objects, window=10):
     f.write(struct.pack(">L", num_objects)) # Number of objects in pack
     for o, path in recency:
         sha1 = o.sha().digest()
-        orig_t, raw = o.as_raw_string()
+        orig_t = o.type
+        raw = o.as_raw_string()
         winner = raw
         t = orig_t
         #for i in range(offs[o]-window, window):
         #    if i < 0 or i >= len(offs): continue
         #    b = magic[i][4]
         #    if b.type != orig_t: continue
-        #    _, base = b.as_raw_string()
+        #    base = b.as_raw_string()
         #    delta = create_delta(base, raw)
         #    if len(delta) < len(winner):
         #        winner = delta