2 # dulwich - Simple command-line interface to Dulwich
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,
20 """Simple command-line interface to Dulwich>
22 This is a very simple command-line wrapper for Dulwich. It is by
23 no means intended to be a full-blown Git command-line interface but just
24 a way to test Dulwich.
29 from getopt import getopt
31 from dulwich.client import get_transport_and_path
32 from dulwich.errors import ApplyDeltaError
33 from dulwich.index import Index
34 from dulwich.pack import Pack, sha_to_hex
35 from dulwich.repo import Repo
38 def cmd_fetch_pack(args):
39 opts, args = getopt(args, "", ["all"])
41 client, path = get_transport_and_path(args.pop(0))
44 determine_wants = r.object_store.determine_wants_all
46 determine_wants = lambda x: [y for y in args if not y in r.object_store]
47 graphwalker = r.get_graph_walker()
48 client.fetch(path, r.object_store, determine_wants)
52 opts, args = getopt(args, "", [])
62 assert isinstance(sha, str)
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 opts, args = getopt(args, "", [])
83 print "Usage: dulwich dump-pack FILENAME"
86 basename, _ = os.path.splitext(args[0])
88 print "Object names checksum: %s" % x.name()
89 print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
91 print "CHECKSUM DOES NOT MATCH"
92 print "Length: %d" % len(x)
95 print "\t%s" % x[name]
97 print "\t%s: Unable to resolve base %s" % (name, k)
98 except ApplyDeltaError, e:
99 print "\t%s: Unable to apply delta: %r" % (name, e)
102 def cmd_dump_index(args):
103 opts, args = getopt(args, "", [])
106 print "Usage: dulwich dump-index FILENAME"
110 idx = Index(filename)
117 opts, args = getopt(args, "", ["--bare"])
125 if not os.path.exists(path):
135 opts, args = getopt(args, "", [])
139 print "usage: dulwich clone host:path [PATH]"
141 client, host_path = get_transport_and_path(args.pop(0))
146 path = host_path.split("/")[-1]
148 if not os.path.exists(path):
151 remote_refs = client.fetch(host_path, r,
152 determine_wants=r.object_store.determine_wants_all,
153 progress=sys.stdout.write)
154 r["HEAD"] = remote_refs["HEAD"]
157 def cmd_commit(args):
158 opts, args = getopt(args, "", ["message"])
161 committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
162 os.getenv("GIT_COMMITTER_EMAIL"))
163 author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
164 os.getenv("GIT_AUTHOR_EMAIL"))
165 r.do_commit(committer=committer, author=author, message=opts["--message"])
169 "commit": cmd_commit,
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:])