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
32 def cmd_fetch_pack(args):
33 from dulwich.client import SimpleFetchGraphWalker
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 = lambda x: [y for y in x.values() if not y in r.object_store]
41 determine_wants = lambda x: [y for y in args if not y in r.object_store]
43 graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
44 f, commit = r.object_store.add_pack()
46 client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
55 from dulwich.repo import Repo
56 opts, args = getopt(args, "", [])
62 assert isinstance(sha, str)
66 commit = r.commit(sha)
68 print "commit: %s" % sha
69 if len(commit.parents) > 1:
70 print "merge: %s" % "...".join(commit.parents[1:])
71 print "author: %s" % commit.author
72 print "committer: %s" % commit.committer
76 todo.extend([p for p in commit.parents if p not in done])
79 def cmd_dump_pack(args):
80 from dulwich.errors import ApplyDeltaError
81 from dulwich.pack import Pack, sha_to_hex
85 opts, args = getopt(args, "", [])
88 print "Usage: dulwich dump-pack FILENAME"
91 basename, _ = os.path.splitext(args[0])
93 print "Object names checksum: %s" % x.name()
94 print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
96 print "CHECKSUM DOES NOT MATCH"
97 print "Length: %d" % len(x)
100 print "\t%s" % x[name]
102 print "\t%s: Unable to resolve base %s" % (name, k)
103 except ApplyDeltaError, e:
104 print "\t%s: Unable to apply delta: %r" % (name, e)
107 def cmd_dump_index(args):
108 from dulwich.index import Index
110 opts, args = getopt(args, "", [])
113 print "Usage: dulwich dump-pack FILENAME"
117 idx = Index(filename)
124 from dulwich.repo import Repo
127 opts, args = getopt(args, "", ["--bare"])
135 if not os.path.exists(path):
145 from dulwich.client import SimpleFetchGraphWalker
146 from dulwich.repo import Repo
149 opts, args = getopt(args, "", [])
153 print "usage: dulwich clone host:path [PATH]"
156 client, host_path = get_transport_and_path(args.pop(0))
161 path = host_path.split("/")[-1]
163 if not os.path.exists(path):
167 determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
168 graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
169 f, commit = r.object_store.add_pack()
171 client.fetch_pack(host_path, determine_wants, graphwalker, f.write,
181 "fetch-pack": cmd_fetch_pack,
182 "dump-pack": cmd_dump_pack,
183 "dump-index": cmd_dump_index,
189 if len(sys.argv) < 2:
190 print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
194 if not cmd in commands:
195 print "No such subcommand: %s" % cmd
197 commands[cmd](sys.argv[2:])