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