2 # dul-daemon - Simple git smart server client
3 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # or (at your option) a later version of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 from getopt import getopt
23 def get_transport_and_path(uri):
24 from dulwich.client import TCPGitClient, SSHGitClient, SubprocessGitClient
25 for handler, transport in (("git://", TCPGitClient), ("git+ssh://", SSHGitClient)):
26 if uri.startswith(handler):
27 host, path = uri[len(handler):].split("/", 1)
28 return transport(host), "/"+path
29 # if its not git or git+ssh, try a local url..
30 return SubprocessGitClient(), uri
33 def cmd_fetch_pack(args):
34 from dulwich.repo import Repo
35 opts, args = getopt(args, "", ["all"])
37 client, path = get_transport_and_path(args.pop(0))
39 determine_wants = r.object_store.determine_wants_all
41 determine_wants = lambda x: [y for y in args if not y in r.object_store]
43 graphwalker = r.get_graph_walker()
44 f, commit = r.object_store.add_pack()
46 client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
52 from dulwich.repo import Repo
53 opts, args = getopt(args, "", [])
59 assert isinstance(sha, str)
63 commit = r.commit(sha)
65 print "commit: %s" % sha
66 if len(commit.parents) > 1:
67 print "merge: %s" % "...".join(commit.parents[1:])
68 print "author: %s" % commit.author
69 print "committer: %s" % commit.committer
73 todo.extend([p for p in commit.parents if p not in done])
76 def cmd_dump_pack(args):
77 from dulwich.errors import ApplyDeltaError
78 from dulwich.pack import Pack, sha_to_hex
82 opts, args = getopt(args, "", [])
85 print "Usage: dulwich dump-pack FILENAME"
88 basename, _ = os.path.splitext(args[0])
90 print "Object names checksum: %s" % x.name()
91 print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
93 print "CHECKSUM DOES NOT MATCH"
94 print "Length: %d" % len(x)
97 print "\t%s" % x[name]
99 print "\t%s: Unable to resolve base %s" % (name, k)
100 except ApplyDeltaError, e:
101 print "\t%s: Unable to apply delta: %r" % (name, e)
104 def cmd_dump_index(args):
105 from dulwich.index import Index
107 opts, args = getopt(args, "", [])
110 print "Usage: dulwich dump-index FILENAME"
114 idx = Index(filename)
121 from dulwich.repo import Repo
124 opts, args = getopt(args, "", ["--bare"])
132 if not os.path.exists(path):
142 from dulwich.repo import Repo
145 opts, args = getopt(args, "", [])
149 print "usage: dulwich clone host:path [PATH]"
151 client, host_path = get_transport_and_path(args.pop(0))
156 path = host_path.split("/")[-1]
158 if not os.path.exists(path):
162 graphwalker = r.get_graph_walker()
163 f, commit = r.object_store.add_pack()
164 client.fetch_pack(host_path, r.object_store.determine_wants_all,
165 graphwalker, f.write, sys.stdout.write)
170 "fetch-pack": cmd_fetch_pack,
171 "dump-pack": cmd_dump_pack,
172 "dump-index": cmd_dump_index,
178 if len(sys.argv) < 2:
179 print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
183 if not cmd in commands:
184 print "No such subcommand: %s" % cmd
186 commands[cmd](sys.argv[2:])