[PATCH 2 of 3] hgweb: extract factory function of httpservice object

Yuya Nishihara yuya at tcha.org
Tue Nov 24 07:53:07 CST 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1446297316 -32400
#      Sat Oct 31 22:15:16 2015 +0900
# Node ID 5b13304492f867291e0c8e09f6117e9e69b7d6af
# Parent  7a6e49b5ad9203fae2fd23ece832aea9c2d6ea0b
hgweb: extract factory function of httpservice object

The next patch will merge the cmdutil.service() calls of both commandserver
and hgweb. Before doing it, this patch wipes out the code specific to hgweb
from commands.serve().

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5942,33 +5942,7 @@ def serve(ui, repo, **opts):
         service = commandserver.createservice(ui, repo, opts)
         return cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
-    # this way we can check if something was given in the command-line
-    if opts.get('port'):
-        opts['port'] = util.getport(opts.get('port'))
-
-    if repo:
-        baseui = repo.baseui
-    else:
-        baseui = ui
-    optlist = ("name templates style address port prefix ipv6"
-               " accesslog errorlog certificate encoding")
-    for o in optlist.split():
-        val = opts.get(o, '')
-        if val in (None, ''): # should check against default options instead
-            continue
-        baseui.setconfig("web", o, val, 'serve')
-        if repo and repo.ui != baseui:
-            repo.ui.setconfig("web", o, val, 'serve')
-
-    o = opts.get('web_conf') or opts.get('webdir_conf')
-    if not o:
-        if not repo:
-            raise error.RepoError(_("there is no Mercurial repository"
-                                    " here (.hg not found)"))
-        o = repo
-
-    app = hgweb.hgweb(o, baseui=baseui)
-    service = hgweb.httpservice(ui, app, opts)
+    service = hgweb.createservice(ui, repo, opts)
     cmdutil.service(opts, initfn=service.init, runfn=service.run)
 
 @command('^status|st',
diff --git a/mercurial/hgweb/__init__.py b/mercurial/hgweb/__init__.py
--- a/mercurial/hgweb/__init__.py
+++ b/mercurial/hgweb/__init__.py
@@ -13,6 +13,7 @@ import os
 from ..i18n import _
 
 from .. import (
+    error,
     util,
 )
 
@@ -83,3 +84,32 @@ class httpservice(object):
 
     def run(self):
         self.httpd.serve_forever()
+
+def createservice(ui, repo, opts):
+    # this way we can check if something was given in the command-line
+    if opts.get('port'):
+        opts['port'] = util.getport(opts.get('port'))
+
+    if repo:
+        baseui = repo.baseui
+    else:
+        baseui = ui
+    optlist = ("name templates style address port prefix ipv6"
+               " accesslog errorlog certificate encoding")
+    for o in optlist.split():
+        val = opts.get(o, '')
+        if val in (None, ''): # should check against default options instead
+            continue
+        baseui.setconfig("web", o, val, 'serve')
+        if repo and repo.ui != baseui:
+            repo.ui.setconfig("web", o, val, 'serve')
+
+    o = opts.get('web_conf') or opts.get('webdir_conf')
+    if not o:
+        if not repo:
+            raise error.RepoError(_("there is no Mercurial repository"
+                                    " here (.hg not found)"))
+        o = repo
+
+    app = hgweb(o, baseui=baseui)
+    return httpservice(ui, app, opts)


More information about the Mercurial-devel mailing list