Merge dumppack and fetch-pack into a single binary.
[jelmer/dulwich-libgit2.git] / bin / dulwich
1 #!/usr/bin/python
2 # dul-daemon - Simple git smart server client
3 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
4
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.
9
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.
14
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,
18 # MA  02110-1301, USA.
19
20 import sys
21 from getopt import getopt
22
23 def cmd_fetch_pack(args):
24         from dulwich.client import TCPGitClient, SimpleFetchGraphWalker
25         from dulwich.repo import Repo
26         opts, args = getopt(args, "", ["all"])
27         opts = dict(opts)
28         if not ":" in args[0]:
29                 print "Usage: dulwich fetch-pack [--all] host:path [REF...]"
30                 sys.exit(1)
31         (host, path) = args.pop(0).split(":", 1)
32         client = TCPGitClient(host)
33         if "--all" in opts:
34                 determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
35         else:
36                 determine_wants = lambda x: [y for y in args if not y in r.object_store]
37         r = Repo(".")
38         graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
39         f, commit = r.object_store.add_pack()
40         try:
41                 client.fetch_pack(path, determine_wants, graphwalker, f.write, sys.stdout.write)
42                 f.close()
43                 commit()
44         except:
45                 f.close()
46                 raise
47
48 def cmd_dump_pack(args):
49         from dulwich.errors import ApplyDeltaError
50         from dulwich.pack import Pack, sha_to_hex
51         import os
52         import sys
53
54         opts, args = getopt(args, "", [])
55
56         if args == []:
57                 print "Usage: dulwich dump-pack FILENAME"
58                 sys.exit(1)
59
60         basename, _ = os.path.splitext(args[0])
61         x = Pack(basename)
62         print "Object names checksum: %s" % x.name()
63         print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
64         if not x.check():
65                 print "CHECKSUM DOES NOT MATCH"
66         print "Length: %d" % len(x)
67         for name in x:
68                 try:
69                         print "\t%s" % x[name]
70                 except KeyError, k:
71                         print "\t%s: Unable to resolve base %s" % (name, k)
72                 except ApplyDeltaError, e:
73                         print "\t%s: Unable to apply delta: %r" % (name, e)
74
75 commands = {
76         "fetch-pack": cmd_fetch_pack,
77         "dump-pack": cmd_dump_pack,
78         }
79
80 if len(sys.argv) < 2:
81         print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
82         sys.exit(1)
83
84 cmd = sys.argv[1]
85 if not cmd in commands:
86         print "No such subcommand: %s" % cmd
87         sys.exit(1)
88 commands[cmd](sys.argv[2:])