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))
40 determine_wants = r.object_store.determine_wants_all
42 determine_wants = lambda x: [y for y in args if not y in r.object_store]
43 graphwalker = r.get_graph_walker()
44 client.fetch(path, r.object_store, determine_wants)
48 from dulwich.repo import Repo
49 opts, args = getopt(args, "", [])
55 assert isinstance(sha, str)
59 commit = r.commit(sha)
61 print "commit: %s" % sha
62 if len(commit.parents) > 1:
63 print "merge: %s" % "...".join(commit.parents[1:])
64 print "author: %s" % commit.author
65 print "committer: %s" % commit.committer
69 todo.extend([p for p in commit.parents if p not in done])
72 def cmd_dump_pack(args):
73 from dulwich.errors import ApplyDeltaError
74 from dulwich.pack import Pack, sha_to_hex
78 opts, args = getopt(args, "", [])
81 print "Usage: dulwich dump-pack FILENAME"
84 basename, _ = os.path.splitext(args[0])
86 print "Object names checksum: %s" % x.name()
87 print "Checksum: %s" % sha_to_hex(x.get_stored_checksum())
89 print "CHECKSUM DOES NOT MATCH"
90 print "Length: %d" % len(x)
93 print "\t%s" % x[name]
95 print "\t%s: Unable to resolve base %s" % (name, k)
96 except ApplyDeltaError, e:
97 print "\t%s: Unable to apply delta: %r" % (name, e)
100 def cmd_dump_index(args):
101 from dulwich.index import Index
103 opts, args = getopt(args, "", [])
106 print "Usage: dulwich dump-index FILENAME"
110 idx = Index(filename)
117 from dulwich.repo import Repo
119 opts, args = getopt(args, "", ["--bare"])
127 if not os.path.exists(path):
137 from dulwich.repo import Repo
140 opts, args = getopt(args, "", [])
144 print "usage: dulwich clone host:path [PATH]"
146 client, host_path = get_transport_and_path(args.pop(0))
151 path = host_path.split("/")[-1]
153 if not os.path.exists(path):
157 graphwalker = r.get_graph_walker()
158 f, commit = r.object_store.add_pack()
159 client.fetch_pack(host_path, r.object_store.determine_wants_all,
160 graphwalker, f.write, sys.stdout.write)
164 def cmd_commit(args):
165 from dulwich.repo import Repo
167 opts, args = getopt(args, "", ["message"])
170 committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
171 os.getenv("GIT_COMMITTER_EMAIL"))
172 author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
173 os.getenv("GIT_AUTHOR_EMAIL"))
174 r.do_commit(committer=committer, author=author, message=opts["--message"])
177 "commit": cmd_commit,
178 "fetch-pack": cmd_fetch_pack,
179 "dump-pack": cmd_dump_pack,
180 "dump-index": cmd_dump_index,
186 if len(sys.argv) < 2:
187 print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
191 if not cmd in commands:
192 print "No such subcommand: %s" % cmd
194 commands[cmd](sys.argv[2:])