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,
23 from dulwich.protocol import Protocol, TCP_GIT_PORT, extract_capabilities
25 class SimpleFetchGraphWalker(object):
27 def __init__(self, local_heads, get_parents):
28 self.heads = set(local_heads)
29 self.get_parents = get_parents
34 self.heads.remove(ref)
35 if ref in self.parents:
36 for p in self.parents[ref]:
41 ret = self.heads.pop()
42 ps = self.get_parents(ret)
43 self.parents[ret] = ps
49 class GitClient(object):
50 """Git smart server client.
54 def __init__(self, fileno, read, write):
55 self.proto = Protocol(read, write)
58 def capabilities(self):
59 return "multi_ack side-band-64k thin-pack ofs-delta"
62 server_capabilities = None
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):
71 return refs, server_capabilities
73 def send_pack(self, path):
74 refs, server_capabilities = self.read_refs()
75 changed_refs = [] # FIXME
77 self.proto.write_pkt_line(None)
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()))
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))
92 def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
93 """Retrieve a pack from a git smart server.
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)
100 (refs, server_capabilities) = self.read_refs()
102 wants = determine_wants(refs)
104 self.proto.write_pkt_line(None)
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()
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()
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":
128 pkt = self.proto.read_pkt_line()
129 for pkt in self.proto.read_pkt_seq():
130 channel = ord(pkt[0])
137 raise AssertionError("Invalid sideband channel %d" % channel)
140 class TCPGitClient(GitClient):
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)
148 super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write)
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)
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)
159 class SubprocessSocket(object):
160 """A socket-like object that talks to an ssh subprocess via pipes."""
162 def __init__(self, proc):
165 def send(self, data):
166 return os.write(self.proc.stdin.fileno(), data)
168 def recv(self, count):
169 return os.read(self.proc.stdout.fileno(), count)
172 self.proc.stdin.close()
173 self.proc.stdout.close()
176 def get_filelike_channels(self):
177 return (self.proc.stdout, self.proc.stdin)
180 class SubprocessGitClient(GitClient):
182 def __init__(self, host, port=None):
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)
194 def send_pack(self, path):
195 client = self._connect("git-receive-pack", path)
196 client.send_pack(path)
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)