changeset 3464:ec4d38e2777d

make pack export work
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sat, 21 Oct 2006 18:48:23 -0400
parents 371833e0fc5c
children e07147ab4de4
files hgext/gitserve.py
diffstat 1 files changed, 62 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/gitserve.py	Sat Oct 21 17:18:24 2006 -0400
+++ b/hgext/gitserve.py	Sat Oct 21 18:48:23 2006 -0400
@@ -351,9 +351,12 @@
         save_map()
 
 class pack:
-    def __init__(self, hash):
+    def __init__(self, hash, t):
         self.hash = hash
+        self.type = t
         self.offset = None
+        self.reall = None
+	self.clen = None
 
     def __str__(self):
         return self.hash
@@ -367,6 +370,38 @@
     def bin(self):
         return node.bin(self.hash)
 
+    def dump(self):
+        f = open(".git/objects/%s/%s" % (self.hash[0:2], self.hash[2:]), "r")
+	buf = f.read()
+	f.close()
+
+        decbuf = zlib.decompress(buf)
+	header, decbuf = decbuf.split("\0", 1)
+	self.strtype, self.reall = header.split(" ")
+
+	buf = zlib.compress(decbuf)
+
+	self.reall = int(self.reall)
+	self.clen = len(buf)
+
+	return buf
+
+    def reallen(self):
+        if self.reall is None:
+            self.dump()
+	
+	print "%s %s %d (%d) %d" % (self.hash, self.strtype, self.reall,
+			self.clen, self.offset)
+        return self.reall
+
+OBJ_NONE = 0
+OBJ_COMMIT = 1
+OBJ_TREE = 2
+OBJ_BLOB = 3
+OBJ_TAG = 4
+# 5/6 for future expansion 
+OBJ_DELTA = 7
+
 def gitpack(ui, repo):
     "Create a git pack file for a given range of revisions"
 
@@ -380,12 +415,27 @@
         f.write(d)
 	return len(d)
 
+    def typelen(t, l):
+        buf = chr(0x80 | (t & 0x7) << 4 | (l & 0xf))
+        l >>= 4
+
+        while l > 0x7f:
+	    buf += chr((l & 0x7f) | 0x80)
+            l >>= 7
+
+	if l or len(buf) == 1:
+            buf += chr(l & 0x7f)
+
+        print repr(buf)
+        return buf
+
     ##
 
-    objs = [pack("66f2467906e280f956e45318ab7c153f0e32bc3e"),
-	    pack("56d5e5ba85c84cb8deb38f40c294405c5650e079"),
-	    pack("8baef1b4abc478178b004d62031cf7fe6db6f903")]
-    objs.sort()
+    objs = [
+	    pack("66f2467906e280f956e45318ab7c153f0e32bc3e", OBJ_COMMIT),
+	    pack("56d5e5ba85c84cb8deb38f40c294405c5650e079", OBJ_TREE),
+	    pack("8baef1b4abc478178b004d62031cf7fe6db6f903", OBJ_BLOB),
+	   ]
     print objs
 
     hash = sha.sha()
@@ -398,7 +448,11 @@
     off = 12 # header is 12 bytes long
     for obj in objs:
 	obj.offset = off
-        off += hashed_write(hash, packf, str(obj))
+
+        rawobj = obj.dump()
+
+        off += hashed_write(hash, packf, typelen(obj.type, obj.reallen()))
+        off += hashed_write(hash, packf, rawobj)
 
     packhash = hash.digest()
     packf.write(packhash)
@@ -410,6 +464,8 @@
     hash = sha.sha()
     idx = open("git.idx", "w")
 
+    objs.sort()
+
     # header
     count = 0
     oidx = 0