Browse Source

Merge upstream

John Carr 16 years ago
parent
commit
17f51f416b
4 changed files with 85 additions and 1 deletions
  1. 63 0
      bin/dulwich
  2. 8 0
      dulwich/object_store.py
  3. 6 0
      dulwich/repo.py
  4. 8 1
      dulwich/tests/test_object_store.py

+ 63 - 0
bin/dulwich

@@ -98,10 +98,73 @@ def cmd_dump_pack(args):
 		except ApplyDeltaError, e:
 			print "\t%s: Unable to apply delta: %r" % (name, e)
 
+def cmd_init(args):
+	from dulwich.repo import Repo
+	import os
+	import sys
+	opts, args = getopt(args, "", ["--bare"])
+	opts = dict(opts)
+
+	if args == []:
+		path = os.getcwd()
+	else:
+		path = args[0]
+
+	if not os.path.exists(path):
+		os.mkdir(path)
+
+	if "--bare" in opts:
+		Repo.init_bare(path)
+	else:
+		Repo.init(path)
+
+
+def cmd_clone(args):
+	from dulwich.client import TCPGitClient, SimpleFetchGraphWalker
+	from dulwich.repo import Repo
+	import os
+	import sys
+	opts, args = getopt(args, "", [])
+	opts = dict(opts)
+
+	if args == []:
+		print "usage: dulwich clone host:path [PATH]"
+		sys.exit(1)
+
+	if not ":" in args[0]:
+		print "Usage: dulwich clone host:path [PATH]"
+		sys.exit(1)
+	(host, host_path) = args.pop(0).split(":", 1)
+	client = TCPGitClient(host)
+
+	if len(args) > 0:
+		path = args.pop(0)
+	else:
+		path = host_path.split("/")[-1]
+
+	if not os.path.exists(path):
+		os.mkdir(path)
+	Repo.init(path)
+	r = Repo(path)
+	determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
+	graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
+	f, commit = r.object_store.add_pack()
+	try:
+		client.fetch_pack(host_path, determine_wants, graphwalker, f.write, 
+				          sys.stdout.write)
+		f.close()
+		commit()
+	except:
+		f.close()
+		raise
+
+
 commands = {
 	"fetch-pack": cmd_fetch_pack,
 	"dump-pack": cmd_dump_pack,
+	"init": cmd_init,
 	"log": cmd_log,
+	"clone": cmd_clone,
 	}
 
 if len(sys.argv) < 2:

+ 8 - 0
dulwich/object_store.py

@@ -26,6 +26,7 @@ from pack import (
         write_pack_index_v2,
         PackData, 
         )
+import tempfile
 PACKDIR = 'pack'
 
 class ObjectStore(object):
@@ -112,3 +113,10 @@ class ObjectStore(object):
             if os.path.getsize(path) > 0:
                 self.move_in_pack(path)
         return f, commit
+
+    def add_objects(self, objects):
+        if len(objects) == 0:
+            return
+        f, commit = self.add_pack()
+        write_pack_data(f, objects, len(objects))
+        commit()

+ 6 - 0
dulwich/repo.py

@@ -267,6 +267,12 @@ class Repo(object):
   def __repr__(self):
       return "<Repo at %r>" % self.path
 
+  @classmethod
+  def init(cls, path, mkdir=True):
+      controldir = os.path.join(path, ".git")
+      os.mkdir(controldir)
+      cls.init_bare(controldir)
+
   @classmethod
   def init_bare(cls, path, mkdir=True):
       for d in [["objects"], 

+ 8 - 1
dulwich/tests/test_object_store.py

@@ -29,5 +29,12 @@ class ObjectStoreTests(TestCase):
         o = ObjectStore("foo")
         self.assertEquals([], o.packs)
 
+    def test_add_objects_empty(self):
+        o = ObjectStore("foo")
+        o.add_objects([])
 
-
+    def test_add_commit(self):
+        o = ObjectStore("foo")
+        # TODO: Argh, no way to construct Git commit objects without 
+        # access to a serialized form.
+        o.add_objects([])