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