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
7 # or (at your option) any later version of the License.
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 """Git smart network protocol server implementation."""
26 from dulwich.protocol import (
32 from dulwich.repo import (
35 from dulwich.pack import (
39 class Backend(object):
43 Get all the refs in the repository
45 :return: dict of name -> sha
47 raise NotImplementedError
49 def apply_pack(self, refs, read):
50 """ Import a set of changes into a repository and update the refs
52 :param refs: list of tuple(name, sha)
53 :param read: callback to read from the incoming pack
55 raise NotImplementedError
57 def fetch_objects(self, determine_wants, graph_walker, progress):
59 Yield the objects required for a list of commits.
61 :param progress: is a callback to send progress messages to the client
63 raise NotImplementedError
66 class GitBackend(Backend):
68 def __init__(self, gitdir=None):
72 self.gitdir = tempfile.mkdtemp()
73 Repo.create(self.gitdir)
75 self.repo = Repo(self.gitdir)
76 self.fetch_objects = self.repo.fetch_objects
77 self.get_refs = self.repo.get_refs
79 def apply_pack(self, refs, read):
80 f, commit = self.repo.object_store.add_thin_pack()
86 for oldsha, sha, ref in refs:
88 del self.repo.refs[ref]
90 self.repo.refs[ref] = sha
95 class Handler(object):
96 """Smart protocol command handler base class."""
98 def __init__(self, backend, read, write):
99 self.backend = backend
100 self.proto = Protocol(read, write)
102 def capabilities(self):
103 return " ".join(self.default_capabilities())
106 class UploadPackHandler(Handler):
107 """Protocol handler for uploading a pack to the server."""
109 def default_capabilities(self):
110 return ("multi_ack", "side-band-64k", "thin-pack", "ofs-delta")
113 def determine_wants(heads):
116 self.proto.write_pkt_line("%s %s\x00%s\n" % ( heads[keys[0]], keys[0], self.capabilities()))
118 self.proto.write_pkt_line("%s %s\n" % (heads[k], k))
121 self.proto.write("0000")
123 # Now client will either send "0000", meaning that it doesnt want to pull.
124 # or it will start sending want want want commands
125 want = self.proto.read_pkt_line()
129 want, self.client_capabilities = extract_capabilities(want)
132 while want and want[:4] == 'want':
133 want_revs.append(want[5:45])
134 want = self.proto.read_pkt_line()
137 progress = lambda x: self.proto.write_sideband(2, x)
138 write = lambda x: self.proto.write_sideband(1, x)
140 class ProtocolGraphWalker(object):
142 def __init__(self, proto):
144 self._last_sha = None
146 def ack(self, have_ref):
147 self.proto.write_pkt_line("ACK %s continue\n" % have_ref)
150 have = self.proto.read_pkt_line()
151 if have[:4] == 'have':
154 #if have[:4] == 'done':
158 # Oddness: Git seems to resend the last ACK, without the "continue" statement
159 self.proto.write_pkt_line("ACK %s\n" % self._last_sha)
161 # The exchange finishes with a NAK
162 self.proto.write_pkt_line("NAK\n")
164 graph_walker = ProtocolGraphWalker(self.proto)
165 objects_iter = self.backend.fetch_objects(determine_wants, graph_walker, progress)
167 # Do they want any objects?
168 if len(objects_iter) == 0:
171 progress("dul-daemon says what\n")
172 progress("counting objects: %d, done.\n" % len(objects_iter))
173 write_pack_data(ProtocolFile(None, write), objects_iter,
175 progress("how was that, then?\n")
177 self.proto.write("0000")
180 class ReceivePackHandler(Handler):
181 """Protocol handler for downloading a pack to the client."""
183 def default_capabilities(self):
184 return ("report-status", "delete-refs")
187 refs = self.backend.get_refs().items()
190 self.proto.write_pkt_line("%s %s\x00%s\n" % (refs[0][1], refs[0][0], self.capabilities()))
191 for i in range(1, len(refs)):
193 self.proto.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
195 self.proto.write_pkt_line("0000000000000000000000000000000000000000 capabilities^{} %s" % self.capabilities())
197 self.proto.write("0000")
200 ref = self.proto.read_pkt_line()
202 # if ref is none then client doesnt want to send us anything..
206 ref, client_capabilities = extract_capabilities(ref)
208 # client will now send us a list of (oldsha, newsha, ref)
210 client_refs.append(ref.split())
211 ref = self.proto.read_pkt_line()
213 # backend can now deal with this refs and read a pack using self.read
214 self.backend.apply_pack(client_refs, self.proto.read)
216 # when we have read all the pack from the client, it assumes
217 # everything worked OK.
218 # there is NO ack from the server before it reports victory.
221 class TCPGitRequestHandler(SocketServer.StreamRequestHandler):
224 proto = Protocol(self.rfile.read, self.wfile.write)
225 command, args = proto.read_cmd()
227 # switch case to handle the specific git command
228 if command == 'git-upload-pack':
229 cls = UploadPackHandler
230 elif command == 'git-receive-pack':
231 cls = ReceivePackHandler
235 h = cls(self.server.backend, self.rfile.read, self.wfile.write)
239 class TCPGitServer(SocketServer.TCPServer):
241 allow_reuse_address = True
242 serve = SocketServer.TCPServer.serve_forever
244 def __init__(self, backend, listen_addr, port=TCP_GIT_PORT):
245 self.backend = backend
246 SocketServer.TCPServer.__init__(self, (listen_addr, port), TCPGitRequestHandler)