Mercurial > hg
view mercurial/hgweb/server.py @ 31765:264baeef3588
show: new extension for displaying various repository data
Currently, Mercurial has a number of commands to show information. And,
there are features coming down the pipe that will introduce more
commands for showing information.
Currently, when introducing a new class of data or a view that we
wish to expose to the user, the strategy is to introduce a new command
or overload an existing command, sometimes both. For example, there is
a desire to formalize the wip/smartlog/underway/mine functionality that
many have devised. There is also a desire to introduce a "topics"
concept. Others would like views of "the current stack." In the
current model, we'd need a new command for wip/smartlog/etc (that
behaves a lot like a pre-defined alias of `hg log`). For topics,
we'd likely overload `hg topic[s]` to both display and manipulate
topics.
Adding new commands for every pre-defined query doesn't scale well
and pollutes `hg help`. Overloading commands to perform read-only and
write operations is arguably an UX anti-pattern: while having all
functionality for a given concept in one command is nice, having a
single command doing multiple discrete operations is not. Furthermore,
a user may be surprised that a command they thought was read-only
actually changes something.
We discussed this at the Mercurial 4.0 Sprint in Paris and decided that
having a single command where we could hang pre-defined views of
various data would be a good idea. Having such a command would:
* Help prevent an explosion of new query-related commands
* Create a clear separation between read and write operations
(mitigates footguns)
* Avoids overloading the meaning of commands that manipulate data
(bookmark, tag, branch, etc) (while we can't take away the
existing behavior for BC reasons, we now won't introduce this
behavior on new commands)
* Allows users to discover informational views more easily by
aggregating them in a single location
* Lowers the barrier to creating the new views (since the barrier
to creating a top-level command is relatively high)
So, this commit introduces the `hg show` command via the "show"
extension. This command accepts a positional argument of the
"view" to show. New views can be registered with a decorator. To
prove it works, we implement the "bookmarks" view, which shows a
table of bookmarks and their associated nodes.
We introduce a new style to hold everything used by `hg show`.
For our initial bookmarks view, the output varies from `hg bookmarks`:
* Padding is performed in the template itself as opposed to Python
* Revision integers are not shown
* shortest() is used to display a 5 character node by default (as
opposed to static 12 characters)
I chose to implement the "bookmarks" view first because it is simple
and shouldn't invite too much bikeshedding that detracts from the
evaluation of `hg show` itself. But there is an important point
to consider: we now have 2 ways to show a list of bookmarks. I'm not
a fan of introducing multiple ways to do very similar things. So it
might be worth discussing how we wish to tackle this issue for
bookmarks, tags, branches, MQ series, etc.
I also made the choice of explicitly declaring the default show
template not part of the standard BC guarantees. History has shown
that we make mistakes and poor choices with output formatting but
can't fix these mistakes later because random tools are parsing
output and we don't want to break these tools. Optimizing for human
consumption is one of my goals for `hg show`. So, by not covering
the formatting as part of BC, the barrier to future change is much
lower and humans benefit.
There are some improvements that can be made to formatting. For
example, we don't yet use label() in the templates. We obviously
want this for color. But I'm not sure if we should reuse the existing
log.* labels or invent new ones. I figure we can punt that to a
follow-up.
At the aforementioned Sprint, we discussed and discarded various
alternatives to `hg show`.
We considered making `hg log <view>` perform this behavior. The main
reason we can't do this is because a positional argument to `hg log`
can be a file path and if there is a conflict between a path name and
a view name, behavior is ambiguous. We could have introduced
`hg log --view` or similar, but we felt that required too much typing
(we don't want to require a command flag to show a view) and wasn't
very discoverable. Furthermore, `hg log` is optimized for showing
changelog data and there are things that `hg display` could display
that aren't changelog centric.
There were concerns about using "show" as the command name.
Some users already have a "show" alias that is similar to `hg export`.
There were also concerns that Git users adapted to `git show` would
be confused by `hg show`'s different behavior. The main difference
here is `git show` prints an `hg export` like view of the current
commit by default and `hg show` requires an argument. `git show`
can also display any Git object. `git show` does not support
displaying more complex views: just single objects. If we
implemented `hg show <hash>` or `hg show <identifier>`, `hg show`
would be a superset of `git show`. Although, I'm hesitant to do that
at this time because I view `hg show` as a higher-level querying
command and there are namespace collisions between valid identifiers
and registered views.
There is also a prefix collision with `hg showconfig`, which is an
alias of `hg config`.
We also considered `hg view`, but that is already used by the "hgk"
extension.
`hg display` was also proposed at one point. It has a prefix collision
with `hg diff`. General consensus was "show" or "view" are the best
verbs. And since "view" was taken, "show" was chosen.
There are a number of inline TODOs in this patch. Some of these
represent decisions yet to be made. Others represent features
requiring non-trivial complexity. Rather than bloat the patch or
invite additional bikeshedding, I figured I'd document future
enhancements via TODO so we can get a minimal implmentation landed.
Something is better than nothing.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 24 Mar 2017 19:19:00 -0700 |
parents | d524c88511a7 |
children | ac96ff471c9a |
line wrap: on
line source
# hgweb/server.py - The standalone hg web server. # # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. from __future__ import absolute_import import errno import os import socket import sys import traceback from ..i18n import _ from .. import ( error, pycompat, util, ) httpservermod = util.httpserver socketserver = util.socketserver urlerr = util.urlerr urlreq = util.urlreq from . import ( common, ) def _splitURI(uri): """Return path and query that has been split from uri Just like CGI environment, the path is unquoted, the query is not. """ if '?' in uri: path, query = uri.split('?', 1) else: path, query = uri, '' return urlreq.unquote(path), query class _error_logger(object): def __init__(self, handler): self.handler = handler def flush(self): pass def write(self, str): self.writelines(str.split('\n')) def writelines(self, seq): for msg in seq: self.handler.log_error("HG error: %s", msg) class _httprequesthandler(httpservermod.basehttprequesthandler): url_scheme = 'http' @staticmethod def preparehttpserver(httpserver, ui): """Prepare .socket of new HTTPServer instance""" pass def __init__(self, *args, **kargs): self.protocol_version = 'HTTP/1.1' httpservermod.basehttprequesthandler.__init__(self, *args, **kargs) def _log_any(self, fp, format, *args): fp.write("%s - - [%s] %s\n" % (self.client_address[0], self.log_date_time_string(), format % args)) fp.flush() def log_error(self, format, *args): self._log_any(self.server.errorlog, format, *args) def log_message(self, format, *args): self._log_any(self.server.accesslog, format, *args) def log_request(self, code='-', size='-'): xheaders = [] if util.safehasattr(self, 'headers'): xheaders = [h for h in self.headers.items() if h[0].startswith('x-')] self.log_message('"%s" %s %s%s', self.requestline, str(code), str(size), ''.join([' %s:%s' % h for h in sorted(xheaders)])) def do_write(self): try: self.do_hgweb() except socket.error as inst: if inst[0] != errno.EPIPE: raise def do_POST(self): try: self.do_write() except Exception: self._start_response("500 Internal Server Error", []) self._write("Internal Server Error") self._done() tb = "".join(traceback.format_exception(*sys.exc_info())) self.log_error("Exception happened during processing " "request '%s':\n%s", self.path, tb) def do_GET(self): self.do_POST() def do_hgweb(self): path, query = _splitURI(self.path) env = {} env['GATEWAY_INTERFACE'] = 'CGI/1.1' env['REQUEST_METHOD'] = self.command env['SERVER_NAME'] = self.server.server_name env['SERVER_PORT'] = str(self.server.server_port) env['REQUEST_URI'] = self.path env['SCRIPT_NAME'] = self.server.prefix env['PATH_INFO'] = path[len(self.server.prefix):] env['REMOTE_HOST'] = self.client_address[0] env['REMOTE_ADDR'] = self.client_address[0] if query: env['QUERY_STRING'] = query if self.headers.typeheader is None: env['CONTENT_TYPE'] = self.headers.type else: env['CONTENT_TYPE'] = self.headers.typeheader length = self.headers.getheader('content-length') if length: env['CONTENT_LENGTH'] = length for header in [h for h in self.headers.keys() if h not in ('content-type', 'content-length')]: hkey = 'HTTP_' + header.replace('-', '_').upper() hval = self.headers.getheader(header) hval = hval.replace('\n', '').strip() if hval: env[hkey] = hval env['SERVER_PROTOCOL'] = self.request_version env['wsgi.version'] = (1, 0) env['wsgi.url_scheme'] = self.url_scheme if env.get('HTTP_EXPECT', '').lower() == '100-continue': self.rfile = common.continuereader(self.rfile, self.wfile.write) env['wsgi.input'] = self.rfile env['wsgi.errors'] = _error_logger(self) env['wsgi.multithread'] = isinstance(self.server, socketserver.ThreadingMixIn) env['wsgi.multiprocess'] = isinstance(self.server, socketserver.ForkingMixIn) env['wsgi.run_once'] = 0 self.saved_status = None self.saved_headers = [] self.sent_headers = False self.length = None self._chunked = None for chunk in self.server.application(env, self._start_response): self._write(chunk) if not self.sent_headers: self.send_headers() self._done() def send_headers(self): if not self.saved_status: raise AssertionError("Sending headers before " "start_response() called") saved_status = self.saved_status.split(None, 1) saved_status[0] = int(saved_status[0]) self.send_response(*saved_status) self.length = None self._chunked = False for h in self.saved_headers: self.send_header(*h) if h[0].lower() == 'content-length': self.length = int(h[1]) if (self.length is None and saved_status[0] != common.HTTP_NOT_MODIFIED): self._chunked = (not self.close_connection and self.request_version == "HTTP/1.1") if self._chunked: self.send_header('Transfer-Encoding', 'chunked') else: self.send_header('Connection', 'close') self.end_headers() self.sent_headers = True def _start_response(self, http_status, headers, exc_info=None): code, msg = http_status.split(None, 1) code = int(code) self.saved_status = http_status bad_headers = ('connection', 'transfer-encoding') self.saved_headers = [h for h in headers if h[0].lower() not in bad_headers] return self._write def _write(self, data): if not self.saved_status: raise AssertionError("data written before start_response() called") elif not self.sent_headers: self.send_headers() if self.length is not None: if len(data) > self.length: raise AssertionError("Content-length header sent, but more " "bytes than specified are being written.") self.length = self.length - len(data) elif self._chunked and data: data = '%x\r\n%s\r\n' % (len(data), data) self.wfile.write(data) self.wfile.flush() def _done(self): if self._chunked: self.wfile.write('0\r\n\r\n') self.wfile.flush() class _httprequesthandlerssl(_httprequesthandler): """HTTPS handler based on Python's ssl module""" url_scheme = 'https' @staticmethod def preparehttpserver(httpserver, ui): try: from .. import sslutil sslutil.modernssl except ImportError: raise error.Abort(_("SSL support is unavailable")) certfile = ui.config('web', 'certificate') # These config options are currently only meant for testing. Use # at your own risk. cafile = ui.config('devel', 'servercafile') reqcert = ui.configbool('devel', 'serverrequirecert') httpserver.socket = sslutil.wrapserversocket(httpserver.socket, ui, certfile=certfile, cafile=cafile, requireclientcert=reqcert) def setup(self): self.connection = self.request self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) try: import threading threading.activeCount() # silence pyflakes and bypass demandimport _mixin = socketserver.ThreadingMixIn except ImportError: if util.safehasattr(os, "fork"): _mixin = socketserver.ForkingMixIn else: class _mixin(object): pass def openlog(opt, default): if opt and opt != '-': return open(opt, 'a') return default class MercurialHTTPServer(_mixin, httpservermod.httpserver, object): # SO_REUSEADDR has broken semantics on windows if pycompat.osname == 'nt': allow_reuse_address = 0 def __init__(self, ui, app, addr, handler, **kwargs): httpservermod.httpserver.__init__(self, addr, handler, **kwargs) self.daemon_threads = True self.application = app handler.preparehttpserver(self, ui) prefix = ui.config('web', 'prefix', '') if prefix: prefix = '/' + prefix.strip('/') self.prefix = prefix alog = openlog(ui.config('web', 'accesslog', '-'), ui.fout) elog = openlog(ui.config('web', 'errorlog', '-'), ui.ferr) self.accesslog = alog self.errorlog = elog self.addr, self.port = self.socket.getsockname()[0:2] self.fqaddr = socket.getfqdn(addr[0]) class IPv6HTTPServer(MercurialHTTPServer): address_family = getattr(socket, 'AF_INET6', None) def __init__(self, *args, **kwargs): if self.address_family is None: raise error.RepoError(_('IPv6 is not available on this system')) super(IPv6HTTPServer, self).__init__(*args, **kwargs) def create_server(ui, app): if ui.config('web', 'certificate'): handler = _httprequesthandlerssl else: handler = _httprequesthandler if ui.configbool('web', 'ipv6'): cls = IPv6HTTPServer else: cls = MercurialHTTPServer # ugly hack due to python issue5853 (for threaded use) try: import mimetypes mimetypes.init() except UnicodeDecodeError: # Python 2.x's mimetypes module attempts to decode strings # from Windows' ANSI APIs as ascii (fail), then re-encode them # as ascii (clown fail), because the default Python Unicode # codec is hardcoded as ascii. sys.argv # unwrap demand-loader so that reload() works reload(sys) # resurrect sys.setdefaultencoding() oldenc = sys.getdefaultencoding() sys.setdefaultencoding("latin1") # or any full 8-bit encoding mimetypes.init() sys.setdefaultencoding(oldenc) address = ui.config('web', 'address', '') port = util.getport(ui.config('web', 'port', 8000)) try: return cls(ui, app, (address, port), handler) except socket.error as inst: raise error.Abort(_("cannot start server at '%s:%d': %s") % (address, port, inst.args[1]))