fix uses of old api (Repo.{set,remove}_ref ObjectStore.add_pack)
[jelmer/dulwich-libgit2.git] / dulwich / server.py
1 # server.py -- Implementation of the server side git protocols
2 # Copryight (C) 2008 John Carr <john.carr@unrouted.co.uk>
3 #
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.
8 #
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.
13 #
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,
17 # MA  02110-1301, USA.
18
19
20 """Git smart network protocol server implementation."""
21
22
23 import SocketServer
24 import tempfile
25
26 from dulwich.protocol import (
27     Protocol,
28     ProtocolFile,
29     TCP_GIT_PORT,
30     extract_capabilities,
31     )
32 from dulwich.repo import (
33     Repo,
34     )
35 from dulwich.pack import (
36     write_pack_data,
37     )
38
39 class Backend(object):
40
41     def get_refs(self):
42         """
43         Get all the refs in the repository
44
45         :return: dict of name -> sha
46         """
47         raise NotImplementedError
48
49     def apply_pack(self, refs, read):
50         """ Import a set of changes into a repository and update the refs
51
52         :param refs: list of tuple(name, sha)
53         :param read: callback to read from the incoming pack
54         """
55         raise NotImplementedError
56
57     def fetch_objects(self, determine_wants, graph_walker, progress):
58         """
59         Yield the objects required for a list of commits.
60
61         :param progress: is a callback to send progress messages to the client
62         """
63         raise NotImplementedError
64
65
66 class GitBackend(Backend):
67
68     def __init__(self, gitdir=None):
69         self.gitdir = gitdir
70
71         if not self.gitdir:
72             self.gitdir = tempfile.mkdtemp()
73             Repo.create(self.gitdir)
74
75         self.repo = Repo(self.gitdir)
76         self.fetch_objects = self.repo.fetch_objects
77         self.get_refs = self.repo.get_refs
78
79     def apply_pack(self, refs, read):
80         f, commit = self.repo.object_store.add_thin_pack()
81         try:
82             f.write(read())
83         finally:
84             commit()
85
86         for oldsha, sha, ref in refs:
87             if ref == "0" * 40:
88                 del self.repo.refs[ref]
89             else:
90                 self.repo.refs[ref] = sha
91
92         print "pack applied"
93
94
95 class Handler(object):
96     """Smart protocol command handler base class."""
97
98     def __init__(self, backend, read, write):
99         self.backend = backend
100         self.proto = Protocol(read, write)
101
102     def capabilities(self):
103         return " ".join(self.default_capabilities())
104
105
106 class UploadPackHandler(Handler):
107     """Protocol handler for uploading a pack to the server."""
108
109     def default_capabilities(self):
110         return ("multi_ack", "side-band-64k", "thin-pack", "ofs-delta")
111
112     def handle(self):
113         def determine_wants(heads):
114             keys = heads.keys()
115             if keys:
116                 self.proto.write_pkt_line("%s %s\x00%s\n" % ( heads[keys[0]], keys[0], self.capabilities()))
117                 for k in keys[1:]:
118                     self.proto.write_pkt_line("%s %s\n" % (heads[k], k))
119
120             # i'm done..
121             self.proto.write("0000")
122
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()
126             if want == None:
127                 return []
128
129             want, self.client_capabilities = extract_capabilities(want)
130
131             want_revs = []
132             while want and want[:4] == 'want':
133                 want_revs.append(want[5:45])
134                 want = self.proto.read_pkt_line()
135             return want_revs
136
137         progress = lambda x: self.proto.write_sideband(2, x)
138         write = lambda x: self.proto.write_sideband(1, x)
139
140         class ProtocolGraphWalker(object):
141
142             def __init__(self, proto):
143                 self.proto = proto
144                 self._last_sha = None
145
146             def ack(self, have_ref):
147                 self.proto.write_pkt_line("ACK %s continue\n" % have_ref)
148
149             def next(self):
150                 have = self.proto.read_pkt_line()
151                 if have[:4] == 'have':
152                     return have[5:45]
153
154                 #if have[:4] == 'done':
155                 #    return None
156
157                 if self._last_sha:
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)
160
161                 # The exchange finishes with a NAK
162                 self.proto.write_pkt_line("NAK\n")
163
164         graph_walker = ProtocolGraphWalker(self.proto)
165         objects_iter = self.backend.fetch_objects(determine_wants, graph_walker, progress)
166
167         # Do they want any objects?
168         if len(objects_iter) == 0:
169             return
170
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, 
174                         len(objects_iter))
175         progress("how was that, then?\n")
176         # we are done
177         self.proto.write("0000")
178
179
180 class ReceivePackHandler(Handler):
181     """Protocol handler for downloading a pack to the client."""
182
183     def default_capabilities(self):
184         return ("report-status", "delete-refs")
185
186     def handle(self):
187         refs = self.backend.get_refs().items()
188
189         if refs:
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)):
192                 ref = refs[i]
193                 self.proto.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
194         else:
195             self.proto.write_pkt_line("0000000000000000000000000000000000000000 capabilities^{} %s" % self.capabilities())
196
197         self.proto.write("0000")
198
199         client_refs = []
200         ref = self.proto.read_pkt_line()
201
202         # if ref is none then client doesnt want to send us anything..
203         if ref is None:
204             return
205
206         ref, client_capabilities = extract_capabilities(ref)
207
208         # client will now send us a list of (oldsha, newsha, ref)
209         while ref:
210             client_refs.append(ref.split())
211             ref = self.proto.read_pkt_line()
212
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)
215
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.
219
220
221 class TCPGitRequestHandler(SocketServer.StreamRequestHandler):
222
223     def handle(self):
224         proto = Protocol(self.rfile.read, self.wfile.write)
225         command, args = proto.read_cmd()
226
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
232         else:
233             return
234
235         h = cls(self.server.backend, self.rfile.read, self.wfile.write)
236         h.handle()
237
238
239 class TCPGitServer(SocketServer.TCPServer):
240
241     allow_reuse_address = True
242     serve = SocketServer.TCPServer.serve_forever
243
244     def __init__(self, backend, listen_addr, port=TCP_GIT_PORT):
245         self.backend = backend
246         SocketServer.TCPServer.__init__(self, (listen_addr, port), TCPGitRequestHandler)
247
248