2
0
Jelmer Vernooij 16 жил өмнө
parent
commit
9adaa2a0e9

+ 1 - 1
dulwich/object_store.py

@@ -19,7 +19,7 @@
 from objects import (
         ShaFile,
         )
-import os
+import os, tempfile
 from pack import (
         iter_sha1, 
         load_packs, 

+ 37 - 2
dulwich/objects.py

@@ -52,6 +52,13 @@ def sha_to_hex(sha):
          len(hexsha)
   return hexsha
 
+def hex_to_sha(hex):
+  """Takes a hex sha and returns a binary sha"""
+  sha = ''
+  for i in range(0,20):
+    sha += chr(int(hex[i*2:i*2+2], 16))
+  assert len(sha) == 20, "Incorrent length of sha1: %d" % len(sha)
+  return sha
 
 class ShaFile(object):
   """A git SHA file."""
@@ -178,6 +185,7 @@ class Blob(ShaFile):
   """A Git Blob object."""
 
   _type = BLOB_ID
+  _num_type = 3
 
   @property
   def data(self):
@@ -223,6 +231,10 @@ class Tree(ShaFile):
   """A Git tree object"""
 
   _type = TREE_ID
+  _num_type = 2
+
+  def __init__(self):
+    self._entries = []
 
   @classmethod
   def from_file(cls, filename):
@@ -231,13 +243,15 @@ class Tree(ShaFile):
       raise NotTreeError(filename)
     return tree
 
+  def add(self, mode, name, hexsha):
+    self._entries.append((mode, name, hexsha))
+
   def entries(self):
     """Return a list of tuples describing the tree entries"""
     return self._entries
 
   def _parse_text(self):
     """Grab the entries in the tree"""
-    self._entries = []
     count = 0
     while count < len(self._text):
       mode = 0
@@ -258,13 +272,23 @@ class Tree(ShaFile):
       chr = self._text[count]
       sha = self._text[count:count+20]
       hexsha = sha_to_hex(sha)
-      self._entries.append((mode, name, hexsha))
+      self.add(mode, name, hexsha)
       count = count + 20
 
+  def serialize(self):
+    self._text = ""
+    for mode, name, hexsha in self._entries:
+        self._text += "%04o %s\0%s" % (mode, name, hex_to_sha(hexsha))
+
+
 class Commit(ShaFile):
   """A git commit object"""
 
   _type = COMMIT_ID
+  _num_type = 1
+
+  def __init__(self):
+    self._parents = []
 
   @classmethod
   def from_file(cls, filename):
@@ -339,6 +363,16 @@ class Commit(ShaFile):
     # XXX: There can be an encoding field.
     self._message = text[count:]
 
+  def serialize(self):
+    self._text = ""
+    self._text += "%s %s\n" % (TREE_ID, self._tree)
+    for p in self._parents:
+      self._text += "%s %s\n" % (PARENT_ID, p)
+    self._text += "%s %s %s +0000\n" % (AUTHOR_ID, self._author, str(self._commit_time))
+    self._text += "%s %s %s +0000\n" % (COMMITTER_ID, self._committer, str(self._commit_time))
+    self._text += "\n" # There must be a new line after the headers
+    self._text += self._message
+
   @property
   def tree(self):
     """Returns the tree that is the state of this commit"""
@@ -372,6 +406,7 @@ class Commit(ShaFile):
     """
     return self._commit_time
 
+
 type_map = {
   BLOB_ID : Blob,
   TREE_ID : Tree,

+ 1 - 1
dulwich/protocol.py

@@ -112,7 +112,7 @@ class Protocol(object):
         splice_at = line.find(" ")
         cmd, args = line[:splice_at], line[splice_at+1:]
         return cmd, args.split(chr(0))
- 
+
 
 def extract_capabilities(text):
     if not "\0" in text:

+ 6 - 13
dulwich/server.py

@@ -19,8 +19,8 @@
 import SocketServer
 from dulwich.protocol import Protocol, ProtocolFile, TCP_GIT_PORT, extract_capabilities
 from dulwich.repo import Repo
-from dulwich.pack import PackData, Pack, write_pack_data
-import os, sha, tempfile
+from dulwich.pack import write_pack_data
+import tempfile
 
 class Backend(object):
 
@@ -63,17 +63,10 @@ class GitBackend(Backend):
         self.get_refs = self.repo.get_refs
 
     def apply_pack(self, refs, read):
-        # store the incoming pack in the repository
-        fd, name = tempfile.mkstemp(suffix='.pack', prefix='pack-', dir=self.repo.pack_dir())
-        os.write(fd, read())
-        os.close(fd)
-
-        # strip '.pack' off our filename
-        basename = name[:-5]
-
-        # generate an index for it
-        pd = PackData(name)
-        pd.create_index_v2(basename+".idx")
+        fd, commit = self.repo.object_store.add_pack()
+        fd.write(read())
+        fd.close()
+        commit()
 
         for oldsha, sha, ref in refs:
             if ref == "0" * 40: