Parcourir la source

Add clone command.

Jelmer Vernooij il y a 16 ans
Parent
commit
cc8a271a82
3 fichiers modifiés avec 56 ajouts et 1 suppressions
  1. 42 0
      bin/dulwich
  2. 8 0
      dulwich/object_store.py
  3. 6 1
      dulwich/repo.py

+ 42 - 0
bin/dulwich

@@ -118,11 +118,53 @@ def cmd_init(args):
 	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 - 1
dulwich/repo.py

@@ -34,7 +34,6 @@ from objects import (
         Tree,
         Blob,
         )
-import tempfile
 
 OBJECTDIR = 'objects'
 SYMREF = 'ref: '
@@ -268,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"],