204
217
self._repository = self.createRepository(self._repository_path)
220
class TCPGitServerThread(threading.Thread):
221
"""Thread that runs a TCP Git server."""
223
def __init__(self, backend, address, port=None):
224
super(TCPGitServerThread, self).__init__()
225
self.setName("TCP Git server on %s:%s" % (address, port))
226
self.server = TCPGitServer(backend, address, port)
229
self.server.serve_forever()
231
def get_address(self):
232
return self.server.server_address
235
self.server.shutdown()
207
238
class GitServer(Server):
209
def __init__(self, repo_url):
240
def __init__(self, repository_path, use_server=False):
210
241
super(GitServer, self).__init__()
211
self.repo_url = repo_url
242
self.repository_path = repository_path
243
self._use_server = use_server
246
"""Return a URL to the Git repository."""
248
return 'git://%s:%d/' % self._server.get_address()
250
return local_path_to_url(self.repository_path)
252
def createRepository(self, path):
255
def start_server(self):
256
super(GitServer, self).start_server()
257
self.createRepository(self.repository_path)
259
repo = GitRepo(self.repository_path)
260
self._server = TCPGitServerThread(
261
DictBackend({"/": repo}), "localhost", 0)
264
def stop_server(self):
265
super(GitServer, self).stop_server()
213
269
def makeRepo(self, tree_contents):
216
os.chdir(self.repo_url)
217
repo = GitRepo.init(".")
219
(Blob.from_string(contents), filename) for (filename, contents)
221
repo.object_store.add_objects(blobs)
222
root_id = dulwich.index.commit_tree(repo.object_store, [
223
(filename, b.id, stat.S_IFREG | 0644)
224
for (b, filename) in blobs])
225
repo.do_commit(committer='Joe Foo <joe@foo.com>',
226
message=u'<The commit message>', tree=root_id)
270
repo = GitRepo(self.repository_path)
272
(Blob.from_string(contents), filename) for (filename, contents)
274
repo.object_store.add_objects(blobs)
275
root_id = dulwich.index.commit_tree(repo.object_store, [
276
(filename, b.id, stat.S_IFREG | 0644)
277
for (b, filename) in blobs])
278
repo.do_commit(committer='Joe Foo <joe@foo.com>',
279
message=u'<The commit message>', tree=root_id)
282
class MercurialServerThread(threading.Thread):
283
"""A thread which runs a Mercurial http server."""
285
def __init__(self, path, address, port=0):
286
super(MercurialServerThread, self).__init__()
288
self.ui.setconfig("web", "address", address)
289
self.ui.setconfig("web", "port", port)
290
self.app = hgweb(path, baseui=self.ui)
291
self.httpd = hgweb_server.create_server(self.ui, self.app)
292
# By default the Mercurial server output goes to stdout,
293
# redirect it to prevent a lot of spurious output.
294
self.httpd.errorlog = StringIO()
295
self.httpd.accesslog = StringIO()
297
def get_address(self):
298
return (self.httpd.addr, self.httpd.port)
301
self.httpd.serve_forever()
304
self.httpd.shutdown()
231
307
class MercurialServer(Server):
233
def __init__(self, repo_url):
309
def __init__(self, repository_path, use_server=False):
234
310
super(MercurialServer, self).__init__()
235
self.repo_url = repo_url
311
self.repository_path = repository_path
312
self._use_server = use_server
316
return "http://%s:%d/" % self._hgserver.get_address()
318
return local_path_to_url(self.repository_path)
320
def start_server(self):
321
super(MercurialServer, self).start_server()
322
self.createRepository(self.repository_path)
324
self._hgserver = MercurialServerThread(self.repository_path,
326
self._hgserver.start()
328
def stop_server(self):
329
super(MercurialServer, self).stop_server()
331
self._hgserver.stop()
333
def createRepository(self, path):
334
localrepository(hg_ui(), self.repository_path, create=1)
237
336
def makeRepo(self, tree_contents):
238
from mercurial.ui import ui
239
from mercurial.localrepo import localrepository
240
repo = localrepository(ui(), self.repo_url, create=1)
337
repo = localrepository(hg_ui(), self.repository_path)
241
338
for filename, contents in tree_contents:
242
f = open(os.path.join(self.repo_url, filename), 'w')
339
f = open(os.path.join(self.repository_path, filename), 'w')
244
341
f.write(contents)