1 # server.py -- Implementation of the server side git protocols
2 # Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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
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.
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,
21 from dulwich.protocol import Protocol, TCP_GIT_PORT, extract_capabilities
23 class SimpleFetchGraphWalker(object):
25 def __init__(self, local_heads, get_parents):
26 self.heads = set(local_heads)
27 self.get_parents = get_parents
32 self.heads.remove(ref)
33 if ref in self.parents:
34 for p in self.parents[ref]:
39 ret = self.heads.pop()
40 ps = self.get_parents(ret)
41 self.parents[ret] = ps
47 class GitClient(object):
48 """Git smart server client.
52 def __init__(self, fileno, read, write):
53 self.proto = Protocol(read, write)
56 def capabilities(self):
57 return "multi_ack side-band-64k thin-pack ofs-delta"
60 server_capabilities = None
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):
69 return refs, server_capabilities
71 def send_pack(self, path):
72 refs, server_capabilities = self.read_refs()
73 changed_refs = [] # FIXME
75 self.proto.write_pkt_line(None)
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)
83 def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
84 """Retrieve a pack from a git smart server.
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)
91 (refs, server_capabilities) = self.read_refs()
93 wants = determine_wants(refs)
95 self.proto.write_pkt_line(None)
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()
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()
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":
119 pkt = self.proto.read_pkt_line()
120 for pkt in self.proto.read_pkt_seq():
121 channel = ord(pkt[0])
128 raise AssertionError("Invalid sideband channel %d" % channel)
131 class TCPGitClient(GitClient):
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)
139 super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write)
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)
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)