changeset 38211:aea29e81753a

serve: add option print-url It can be used to open hgweb in a web browser. Example : xdg-open "`hg serve --print-url`". Differential Revision: https://phab.mercurial-scm.org/D3649
author Nicola Spanti <nicola.spanti@logilab.fr>
date Thu, 24 May 2018 09:56:28 +0000
parents 7fa3408f83ef
children c019db5ccfa1
files mercurial/commands.py mercurial/hgweb/__init__.py tests/test-completion.t
diffstat 3 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Sat May 26 11:33:02 2018 +0900
+++ b/mercurial/commands.py	Thu May 24 09:56:28 2018 +0000
@@ -4786,7 +4786,8 @@
     ('t', 'templates', '', _('web templates to use'), _('TEMPLATE')),
     ('', 'style', '', _('template style to use'), _('STYLE')),
     ('6', 'ipv6', None, _('use IPv6 in addition to IPv4')),
-    ('', 'certificate', '', _('SSL certificate file'), _('FILE'))]
+    ('', 'certificate', '', _('SSL certificate file'), _('FILE')),
+    ('', 'print-url', None, _('start and print only the URL'))]
      + subrepoopts,
     _('[OPTION]...'),
     optionalrepo=True)
@@ -4818,6 +4819,10 @@
     opts = pycompat.byteskwargs(opts)
     if opts["stdio"] and opts["cmdserver"]:
         raise error.Abort(_("cannot use --stdio with --cmdserver"))
+    if opts["print_url"] and ui.verbose:
+        raise error.Abort(_("cannot use --print-url with --verbose"))
+    if opts["print_url"]:
+        opts['daemon'] = True
 
     if opts["stdio"]:
         if repo is None:
--- a/mercurial/hgweb/__init__.py	Sat May 26 11:33:02 2018 +0900
+++ b/mercurial/hgweb/__init__.py	Thu May 24 09:56:28 2018 +0000
@@ -57,7 +57,9 @@
         procutil.setsignalhandler()
         self.httpd = server.create_server(self.ui, self.app)
 
-        if self.opts['port'] and not self.ui.verbose:
+        if (self.opts['port'] and
+            not self.ui.verbose and
+            not self.opts['print_url']):
             return
 
         if self.httpd.prefix:
@@ -78,13 +80,18 @@
         fqaddr = self.httpd.fqaddr
         if r':' in fqaddr:
             fqaddr = r'[%s]' % fqaddr
-        if self.opts['port']:
-            write = self.ui.status
+
+        url = 'http://%s%s/%s' % (
+            pycompat.sysbytes(fqaddr), pycompat.sysbytes(port), prefix)
+        if self.opts['print_url']:
+            self.ui.write('%s\n' % url)
         else:
-            write = self.ui.write
-        write(_('listening at http://%s%s/%s (bound to %s:%d)\n') %
-              (pycompat.sysbytes(fqaddr), pycompat.sysbytes(port),
-               prefix, pycompat.sysbytes(bindaddr), self.httpd.port))
+            if self.opts['port']:
+                write = self.ui.status
+            else:
+                write = self.ui.write
+            write(_('listening at %s (bound to %s:%d)\n') %
+                  (url, pycompat.sysbytes(bindaddr), self.httpd.port))
         self.ui.flush()  # avoid buffering of status message
 
     def run(self):
--- a/tests/test-completion.t	Sat May 26 11:33:02 2018 +0900
+++ b/tests/test-completion.t	Thu May 24 09:56:28 2018 +0000
@@ -191,6 +191,7 @@
   --pid-file
   --port
   --prefix
+  --print-url
   --profile
   --quiet
   --repository
@@ -239,7 +240,7 @@
   pull: update, force, rev, bookmark, branch, ssh, remotecmd, insecure
   push: force, rev, bookmark, branch, new-branch, pushvars, ssh, remotecmd, insecure
   remove: after, force, subrepos, include, exclude, dry-run
-  serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, subrepos
+  serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, print-url, subrepos
   status: all, modified, added, removed, deleted, clean, unknown, ignored, no-status, terse, copies, print0, rev, change, include, exclude, subrepos, template
   summary: remote
   update: clean, check, merge, date, rev, tool