Missed one
[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 select
20 import socket
21 from dulwich.protocol import Protocol
22
23 def extract_capabilities(text):
24     if not "\0" in text:
25         return text
26     capabilities = text.split("\0")
27     return (capabilities[0], capabilities[1:])
28
29
30 class SimpleFetchGraphWalker(object):
31
32     def __init__(self, local_heads, get_parents):
33         self.heads = set(local_heads)
34         self.get_parents = get_parents
35         self.parents = {}
36
37     def ack(self, ref):
38         if ref in self.heads:
39             self.heads.remove(ref)
40         if not ref in self.parents:
41             return
42         for p in self.parents[ref]:
43             self.ack(p)
44
45     def next(self):
46         if self.heads:
47             ret = self.heads.pop()
48             ps = self.get_parents(ret)
49             self.parents[ret] = ps
50             self.heads.update(ps)
51             return ret
52         return None
53
54
55 class GitClient(object):
56     """Git smart server client.
57
58     """
59
60     def __init__(self, fileno, read, write, host):
61         self.proto = Protocol(read, write)
62         self.fileno = fileno
63         self.host = host
64
65     def send_cmd(self, name, *args):
66         self.proto.write_pkt_line("%s %s" % (name, "".join(["%s\0" % a for a in args])))
67
68     def capabilities(self):
69         return "multi_ack side-band-64k thin-pack ofs-delta"
70
71     def read_refs(self):
72         server_capabilities = None
73         refs = {}
74         # Receive refs from server
75         for pkt in self.proto.read_pkt_seq():
76             (sha, ref) = pkt.rstrip("\n").split(" ", 1)
77             if server_capabilities is None:
78                 (ref, server_capabilities) = extract_capabilities(ref)
79             if not (ref == "capabilities^{}" and sha == "0" * 40):
80                 refs[ref] = sha
81         return refs, server_capabilities
82
83     def send_pack(self, path):
84         self.send_cmd("git-receive-pack", path, "host=%s" % self.host)
85         refs, server_capabilities = self.read_refs()
86         changed_refs = [] # FIXME
87         if not changed_refs:
88             self.proto.write_pkt_line(None)
89             return
90         self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
91         for changed_ref in changed_refs[:]:
92             self.proto.write_pkt_line("%s %s %s" % changed_refs)
93         self.proto.write_pkt_line(None)
94         # FIXME: Send pack
95
96     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
97         """Retrieve a pack from a git smart server.
98
99         :param determine_wants: Callback that returns list of commits to fetch
100         :param graph_walker: Object with next() and ack().
101         :param pack_data: Callback called for each bit of data in the pack
102         :param progress: Callback for progress reports (strings)
103         """
104         self.send_cmd("git-upload-pack", path, "host=%s" % self.host)
105
106         (refs, server_capabilities) = self.read_refs()
107        
108         wants = determine_wants(refs)
109         if not wants:
110             self.proto.write_pkt_line(None)
111             return
112         self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
113         for want in wants[1:]:
114             self.proto.write_pkt_line("want %s\n" % want)
115         self.proto.write_pkt_line(None)
116         have = graph_walker.next()
117         while have:
118             self.proto.write_pkt_line("have %s\n" % have)
119             if len(select.select([self.fileno], [], [], 0)[0]) > 0:
120                 pkt = self.proto.read_pkt_line()
121                 parts = pkt.rstrip("\n").split(" ")
122                 if parts[0] == "ACK":
123                     graph_walker.ack(parts[1])
124                     assert parts[2] == "continue"
125             have = graph_walker.next()
126         self.proto.write_pkt_line("done\n")
127         pkt = self.proto.read_pkt_line()
128         while pkt:
129             parts = pkt.rstrip("\n").split(" ")
130             if parts[0] == "ACK":
131                 graph_walker.ack(pkt.split(" ")[1])
132             if len(parts) < 3 or parts[2] != "continue":
133                 break
134             pkt = self.proto.read_pkt_line()
135         for pkt in self.proto.read_pkt_seq():
136             channel = ord(pkt[0])
137             pkt = pkt[1:]
138             if channel == 1:
139                 pack_data(pkt)
140             elif channel == 2:
141                 progress(pkt)
142             else:
143                 raise AssertionError("Invalid sideband channel %d" % channel)
144
145
146 class TCPGitClient(GitClient):
147
148     def __init__(self, host, port=9418):
149         self._socket = socket.socket()
150         self._socket.connect((host, port))
151         self.rfile = self._socket.makefile('rb', -1)
152         self.wfile = self._socket.makefile('wb', 0)
153         super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write, host)