Fix typo.
[jelmer/dulwich-libgit2.git] / dulwich / client.py
1 # server.py -- Implementation of the server side git protocols
2 # Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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; either version 2
7 # or (at your option) a 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 import os
20 import select
21 import socket
22 import subprocess
23
24 from dulwich.protocol import (
25     Protocol,
26     TCP_GIT_PORT,
27     extract_capabilities,
28     )
29 from dulwich.pack import (
30     write_pack_data,
31     )
32
33
34 def _fileno_can_read(fileno):
35     return len(select.select([fileno], [], [], 0)[0]) > 0
36
37
38 class SimpleFetchGraphWalker(object):
39
40     def __init__(self, local_heads, get_parents):
41         self.heads = set(local_heads)
42         self.get_parents = get_parents
43         self.parents = {}
44
45     def ack(self, ref):
46         if ref in self.heads:
47             self.heads.remove(ref)
48         if ref in self.parents:
49             for p in self.parents[ref]:
50                 self.ack(p)
51
52     def next(self):
53         if self.heads:
54             ret = self.heads.pop()
55             ps = self.get_parents(ret)
56             self.parents[ret] = ps
57             self.heads.update(ps)
58             return ret
59         return None
60
61
62 CAPABILITIES = ["multi_ack", "side-band-64k", "ofs-delta"]
63
64
65 class GitClient(object):
66     """Git smart server client.
67
68     """
69
70     def __init__(self, can_read, read, write, thin_packs=True):
71         self.proto = Protocol(read, write)
72         self._can_read = can_read
73         self._capabilities = list(CAPABILITIES)
74         if thin_packs:
75             self._capabilities.append("thin-pack")
76
77     def capabilities(self):
78         return " ".join(self._capabilities)
79
80     def read_refs(self):
81         server_capabilities = None
82         refs = {}
83         # Receive refs from server
84         for pkt in self.proto.read_pkt_seq():
85             (sha, ref) = pkt.rstrip("\n").split(" ", 1)
86             if server_capabilities is None:
87                 (ref, server_capabilities) = extract_capabilities(ref)
88             refs[ref] = sha
89         return refs, server_capabilities
90
91     def send_pack(self, path, generate_pack_contents):
92         refs, server_capabilities = self.read_refs()
93         changed_refs = [] # FIXME
94         if not changed_refs:
95             self.proto.write_pkt_line(None)
96             return
97         self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
98         want = []
99         have = []
100         for changed_ref in changed_refs[:]:
101             self.proto.write_pkt_line("%s %s %s" % changed_refs)
102             want.append(changed_refs[1])
103             if changed_refs[0] != "0"*40:
104                 have.append(changed_refs[0])
105         self.proto.write_pkt_line(None)
106         shas = generate_pack_contents(want, have, None)
107         write_pack_data(self.write, shas, len(shas))
108
109     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
110         """Retrieve a pack from a git smart server.
111
112         :param determine_wants: Callback that returns list of commits to fetch
113         :param graph_walker: Object with next() and ack().
114         :param pack_data: Callback called for each bit of data in the pack
115         :param progress: Callback for progress reports (strings)
116         """
117         (refs, server_capabilities) = self.read_refs()
118         wants = determine_wants(refs)
119         if not wants:
120             self.proto.write_pkt_line(None)
121             return
122         self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
123         for want in wants[1:]:
124             self.proto.write_pkt_line("want %s\n" % want)
125         self.proto.write_pkt_line(None)
126         have = graph_walker.next()
127         while have:
128             self.proto.write_pkt_line("have %s\n" % have)
129             if self._can_read():
130                 pkt = self.proto.read_pkt_line()
131                 parts = pkt.rstrip("\n").split(" ")
132                 if parts[0] == "ACK":
133                     graph_walker.ack(parts[1])
134                     assert parts[2] == "continue"
135             have = graph_walker.next()
136         self.proto.write_pkt_line("done\n")
137         pkt = self.proto.read_pkt_line()
138         while pkt:
139             parts = pkt.rstrip("\n").split(" ")
140             if parts[0] == "ACK":
141                 graph_walker.ack(pkt.split(" ")[1])
142             if len(parts) < 3 or parts[2] != "continue":
143                 break
144             pkt = self.proto.read_pkt_line()
145         for pkt in self.proto.read_pkt_seq():
146             channel = ord(pkt[0])
147             pkt = pkt[1:]
148             if channel == 1:
149                 pack_data(pkt)
150             elif channel == 2:
151                 progress(pkt)
152             else:
153                 raise AssertionError("Invalid sideband channel %d" % channel)
154
155
156 class TCPGitClient(GitClient):
157
158     def __init__(self, host, port=TCP_GIT_PORT, *args, **kwargs):
159         self._socket = socket.socket(type=socket.SOCK_STREAM)
160         self._socket.connect((host, port))
161         self.rfile = self._socket.makefile('rb', -1)
162         self.wfile = self._socket.makefile('wb', 0)
163         self.host = host
164         super(TCPGitClient, self).__init__(lambda: _fileno_can_read(self._socket.fileno()), self.rfile.read, self.wfile.write, *args, **kwargs)
165
166     def send_pack(self, path):
167         self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
168         super(TCPGitClient, self).send_pack(path)
169
170     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
171         self.proto.send_cmd("git-upload-pack", path, "host=%s" % self.host)
172         super(TCPGitClient, self).fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
173
174
175 class SubprocessGitClient(GitClient):
176
177     def __init__(self, *args, **kwargs):
178         self.proc = None
179         self._args = args
180         self._kwargs = kwargs
181
182     def _connect(self, service, *args):
183         argv = [service] + list(args)
184         self.proc = subprocess.Popen(argv, bufsize=0,
185                                 stdin=subprocess.PIPE,
186                                 stdout=subprocess.PIPE)
187         def read_fn(size):
188             return self.proc.stdout.read(size)
189         def write_fn(data):
190             self.proc.stdin.write(data)
191             self.proc.stdin.flush()
192         return GitClient(lambda: _fileno_can_read(self.proc.stdout.fileno()), read_fn, write_fn, *args, **kwargs)
193
194     def send_pack(self, path):
195         client = self._connect("git-receive-pack", path)
196         client.send_pack(path)
197
198     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
199         client = self._connect("git-upload-pack", path)
200         client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
201
202
203 class SSHSubprocess(object):
204     """A socket-like object that talks to an ssh subprocess via pipes."""
205
206     def __init__(self, proc):
207         self.proc = proc
208
209     def send(self, data):
210         return os.write(self.proc.stdin.fileno(), data)
211
212     def recv(self, count):
213         return self.proc.stdout.read(count)
214
215     def close(self):
216         self.proc.stdin.close()
217         self.proc.stdout.close()
218         self.proc.wait()
219
220
221 class SSHVendor(object):
222
223     def connect_ssh(self, host, command, username=None, port=None):
224         #FIXME: This has no way to deal with passwords..
225         args = ['ssh', '-x']
226         if port is not None:
227             args.extend(['-p', str(port)])
228         if username is not None:
229             host = "%s@%s" % (username, host)
230         args.append(host)
231         proc = subprocess.Popen(args + command,
232                                 stdin=subprocess.PIPE,
233                                 stdout=subprocess.PIPE)
234         return SSHSubprocess(proc)
235
236 # Can be overridden by users
237 get_ssh_vendor = SSHVendor
238
239
240 class SSHGitClient(GitClient):
241
242     def __init__(self, host, port=None):
243         self.host = host
244         self.port = port
245
246     def send_pack(self, path):
247         remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack %s" % path], port=self.port)
248         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send)
249         client.send_pack(path)
250
251     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
252         remote = get_ssh_vendor().connect_ssh(self.host, ["git-upload-pack %s" % path], port=self.port)
253         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send)
254         client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
255