1 # server.py -- Implementation of the server side git protocols
2 # Copryight (C) 2008 John Carr <john.carr@unrouted.co.uk>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 from dulwich.protocol import Protocol, ProtocolFile, TCP_GIT_PORT, extract_capabilities
21 from dulwich.repo import Repo
22 from dulwich.pack import PackData, Pack, write_pack_data
23 import os, sha, tempfile
25 class Backend(object):
29 Get all the refs in the repository
31 :return: dict of name -> sha
33 raise NotImplementedError
35 def apply_pack(self, refs, read):
36 """ Import a set of changes into a repository and update the refs
38 :param refs: list of tuple(name, sha)
39 :param read: callback to read from the incoming pack
41 raise NotImplementedError
43 def fetch_objects(self, determine_wants, graph_walker, progress):
45 Yield the objects required for a list of commits.
47 :param progress: is a callback to send progress messages to the client
49 raise NotImplementedError
52 class GitBackend(Backend):
54 def __init__(self, gitdir=None):
58 self.gitdir = tempfile.mkdtemp()
59 Repo.create(self.gitdir)
61 self.repo = Repo(self.gitdir)
62 self.fetch_objects = self.repo.fetch_objects
63 self.get_refs = self.repo.get_refs
65 def apply_pack(self, refs, read):
66 fd, commit = self.repo.object_store.add_pack()
70 for oldsha, sha, ref in refs:
72 self.repo.remove_ref(ref)
74 self.repo.set_ref(ref, sha)
79 class Handler(object):
81 def __init__(self, backend, read, write):
82 self.backend = backend
83 self.proto = Protocol(read, write)
85 def capabilities(self):
86 return " ".join(self.default_capabilities())
89 class UploadPackHandler(Handler):
91 def default_capabilities(self):
92 return ("multi_ack", "side-band-64k", "thin-pack", "ofs-delta")
95 def determine_wants(heads):
98 self.proto.write_pkt_line("%s %s\x00%s\n" % ( heads[keys[0]], keys[0], self.capabilities()))
100 self.proto.write_pkt_line("%s %s\n" % (heads[k], k))
103 self.proto.write("0000")
105 # Now client will either send "0000", meaning that it doesnt want to pull.
106 # or it will start sending want want want commands
107 want = self.proto.read_pkt_line()
111 want, self.client_capabilities = extract_capabilities(want)
114 while want and want[:4] == 'want':
115 want_revs.append(want[5:45])
116 want = self.proto.read_pkt_line()
119 progress = lambda x: self.proto.write_sideband(2, x)
120 write = lambda x: self.proto.write_sideband(1, x)
122 class ProtocolGraphWalker(object):
124 def __init__(self, proto):
126 self._last_sha = None
128 def ack(self, have_ref):
129 self.proto.write_pkt_line("ACK %s continue\n" % have_ref)
132 have = self.proto.read_pkt_line()
133 if have[:4] == 'have':
136 #if have[:4] == 'done':
140 # Oddness: Git seems to resend the last ACK, without the "continue" statement
141 self.proto.write_pkt_line("ACK %s\n" % self._last_sha)
143 # The exchange finishes with a NAK
144 self.proto.write_pkt_line("NAK\n")
146 graph_walker = ProtocolGraphWalker(self.proto)
147 objects = list(self.backend.fetch_objects(determine_wants, graph_walker, progress))
148 progress("dul-daemon says what\n")
149 progress("counting objects: %d, done.\n" % len(objects))
150 write_pack_data(ProtocolFile(None, write), objects, len(objects))
151 progress("how was that, then?\n")
153 self.proto.write("0000")
156 class ReceivePackHandler(Handler):
158 def default_capabilities(self):
159 return ("report-status", "delete-refs")
162 refs = self.backend.get_refs().items()
165 self.proto.write_pkt_line("%s %s\x00%s\n" % (refs[0][1], refs[0][0], self.capabilities()))
166 for i in range(1, len(refs)):
168 self.proto.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
170 self.proto.write_pkt_line("0000000000000000000000000000000000000000 capabilities^{} %s" % self.capabilities())
172 self.proto.write("0000")
175 ref = self.proto.read_pkt_line()
177 # if ref is none then client doesnt want to send us anything..
181 ref, client_capabilities = extract_capabilities(ref)
183 # client will now send us a list of (oldsha, newsha, ref)
185 client_refs.append(ref.split())
186 ref = self.proto.read_pkt_line()
188 # backend can now deal with this refs and read a pack using self.read
189 self.backend.apply_pack(client_refs, self.proto.read)
191 # when we have read all the pack from the client, it assumes everything worked OK
192 # there is NO ack from the server before it reports victory.
195 class TCPGitRequestHandler(SocketServer.StreamRequestHandler):
198 proto = Protocol(self.rfile.read, self.wfile.write)
199 command, args = proto.read_cmd()
201 # switch case to handle the specific git command
202 if command == 'git-upload-pack':
203 cls = UploadPackHandler
204 elif command == 'git-receive-pack':
205 cls = ReceivePackHandler
209 h = cls(self.server.backend, self.rfile.read, self.wfile.write)
213 class TCPGitServer(SocketServer.TCPServer):
215 allow_reuse_address = True
216 serve = SocketServer.TCPServer.serve_forever
218 def __init__(self, backend, listen_addr, port=TCP_GIT_PORT):
219 self.backend = backend
220 SocketServer.TCPServer.__init__(self, (listen_addr, port), TCPGitRequestHandler)