1e2fb6cc90f1a97f138ab2968fe808c1540fc539
[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; version 2
7 # 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 from dulwich.protocol import Protocol, TCP_GIT_PORT, extract_capabilities
24
25 class SimpleFetchGraphWalker(object):
26
27     def __init__(self, local_heads, get_parents):
28         self.heads = set(local_heads)
29         self.get_parents = get_parents
30         self.parents = {}
31
32     def ack(self, ref):
33         if ref in self.heads:
34             self.heads.remove(ref)
35         if ref in self.parents:
36             for p in self.parents[ref]:
37                 self.ack(p)
38
39     def next(self):
40         if self.heads:
41             ret = self.heads.pop()
42             ps = self.get_parents(ret)
43             self.parents[ret] = ps
44             self.heads.update(ps)
45             return ret
46         return None
47
48
49 class GitClient(object):
50     """Git smart server client.
51
52     """
53
54     def __init__(self, fileno, read, write):
55         self.proto = Protocol(read, write)
56         self.fileno = fileno
57
58     def capabilities(self):
59         return "multi_ack side-band-64k thin-pack ofs-delta"
60
61     def read_refs(self):
62         server_capabilities = None
63         refs = {}
64         # Receive refs from server
65         for pkt in self.proto.read_pkt_seq():
66             (sha, ref) = pkt.rstrip("\n").split(" ", 1)
67             if server_capabilities is None:
68                 (ref, server_capabilities) = extract_capabilities(ref)
69             if not (ref == "capabilities^{}" and sha == "0" * 40):
70                 refs[ref] = sha
71         return refs, server_capabilities
72
73     def send_pack(self, path):
74         refs, server_capabilities = self.read_refs()
75         changed_refs = [] # FIXME
76         if not changed_refs:
77             self.proto.write_pkt_line(None)
78             return
79         self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
80         want = []
81         have = []
82         for changed_ref in changed_refs[:]:
83             self.proto.write_pkt_line("%s %s %s" % changed_refs)
84             want.append(changed_refs[1])
85             if changed_refs[0] != "0"*40:
86                 have.append(changed_refs[0])
87         self.proto.write_pkt_line(None)
88         # FIXME: This is implementation specific
89         # shas = generate_pack_contents(want, have, None)
90         # write_pack_data(self.write, shas, len(shas))
91
92     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
93         """Retrieve a pack from a git smart server.
94
95         :param determine_wants: Callback that returns list of commits to fetch
96         :param graph_walker: Object with next() and ack().
97         :param pack_data: Callback called for each bit of data in the pack
98         :param progress: Callback for progress reports (strings)
99         """
100         (refs, server_capabilities) = self.read_refs()
101        
102         wants = determine_wants(refs)
103         if not wants:
104             self.proto.write_pkt_line(None)
105             return
106         self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
107         for want in wants[1:]:
108             self.proto.write_pkt_line("want %s\n" % want)
109         self.proto.write_pkt_line(None)
110         have = graph_walker.next()
111         while have:
112             self.proto.write_pkt_line("have %s\n" % have)
113             if len(select.select([self.fileno], [], [], 0)[0]) > 0:
114                 pkt = self.proto.read_pkt_line()
115                 parts = pkt.rstrip("\n").split(" ")
116                 if parts[0] == "ACK":
117                     graph_walker.ack(parts[1])
118                     assert parts[2] == "continue"
119             have = graph_walker.next()
120         self.proto.write_pkt_line("done\n")
121         pkt = self.proto.read_pkt_line()
122         while pkt:
123             parts = pkt.rstrip("\n").split(" ")
124             if parts[0] == "ACK":
125                 graph_walker.ack(pkt.split(" ")[1])
126             if len(parts) < 3 or parts[2] != "continue":
127                 break
128             pkt = self.proto.read_pkt_line()
129         for pkt in self.proto.read_pkt_seq():
130             channel = ord(pkt[0])
131             pkt = pkt[1:]
132             if channel == 1:
133                 pack_data(pkt)
134             elif channel == 2:
135                 progress(pkt)
136             else:
137                 raise AssertionError("Invalid sideband channel %d" % channel)
138
139
140 class TCPGitClient(GitClient):
141
142     def __init__(self, host, port=TCP_GIT_PORT):
143         self._socket = socket.socket(type=socket.SOCK_STREAM)
144         self._socket.connect((host, port))
145         self.rfile = self._socket.makefile('rb', -1)
146         self.wfile = self._socket.makefile('wb', 0)
147         self.host = host
148         super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write)
149
150     def send_pack(self, path):
151         self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
152         super(TCPGitClient, self).send_pack(path)
153
154     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
155         self.proto.send_cmd("git-upload-pack", path, "host=%s" % self.host)
156         super(TCPGitClient, self).fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
157
158
159 class SubprocessSocket(object):
160     """A socket-like object that talks to an ssh subprocess via pipes."""
161
162     def __init__(self, proc):
163         self.proc = proc
164
165     def send(self, data):
166         return os.write(self.proc.stdin.fileno(), data)
167
168     def recv(self, count):
169         return os.read(self.proc.stdout.fileno(), count)
170
171     def close(self):
172         self.proc.stdin.close()
173         self.proc.stdout.close()
174         self.proc.wait()
175
176     def get_filelike_channels(self):
177         return (self.proc.stdout, self.proc.stdin)
178
179
180 class SubprocessGitClient(GitClient):
181
182     def __init__(self, host, port=None):
183         self.host = host
184         self.port = port
185
186     def _connect(self, service, *args):
187         argv = [service] + list(args)
188         proc = subprocess.Popen(argv,
189                                 stdin=subprocess.PIPE,
190                                 stdout=subprocess.PIPE)
191         socket = SubprocessSocket(proc)
192         return GitClient(proc.stdin.fileno(), socket.recv, socket.send)
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