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, "", [])
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
123 opts, args = getopt(args, "", ["--bare"])
131 if not os.path.exists(path):
141 from dulwich.repo import Repo
144 opts, args = getopt(args, "", [])
148 print "usage: dulwich clone host:path [PATH]"
150 client, host_path = get_transport_and_path(args.pop(0))
155 path = host_path.split("/")[-1]
157 if not os.path.exists(path):
160 remote_refs = client.fetch(host_path, r,
161 determine_wants=r.object_store.determine_wants_all,
162 progress=sys.stdout.write)
163 r["HEAD"] = remote_refs["HEAD"]
166 def cmd_commit(args):
167 from dulwich.repo import Repo
169 opts, args = getopt(args, "", ["message"])
172 committer = "%s <%s>" % (os.getenv("GIT_COMMITTER_NAME"),
173 os.getenv("GIT_COMMITTER_EMAIL"))
174 author = "%s <%s>" % (os.getenv("GIT_AUTHOR_NAME"),
175 os.getenv("GIT_AUTHOR_EMAIL"))
176 r.do_commit(committer=committer, author=author, message=opts["--message"])
179 "commit": cmd_commit,
180 "fetch-pack": cmd_fetch_pack,
181 "dump-pack": cmd_dump_pack,
182 "dump-index": cmd_dump_index,
188 if len(sys.argv) < 2:
189 print "Usage: %s <%s> [OPTIONS...]" % (sys.argv[0], "|".join(commands.keys()))
193 if not cmd in commands:
194 print "No such subcommand: %s" % cmd
196 commands[cmd](sys.argv[2:])