mercurial/hgweb/__init__.py
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 18 Sep 2024 21:00:20 -0400
changeset 51873 0afd58c72175
parent 51863 f4733654f144
permissions -rw-r--r--
tests: enable pytype checking on `mercurial/bundlerepo.py`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2391
d351a3be3371 Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents: 2356
diff changeset
     1
# hgweb/__init__.py - web interface to a mercurial repository
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     2
#
238
3b92f8fe47ae hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents: 222
diff changeset
     3
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
     4
# Copyright 2005 Olivia Mackall <olivia@selenic.com>
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 3877
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 8225
diff changeset
     7
# GNU General Public License version 2 or any later version.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
51863
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 49389
diff changeset
     9
from __future__ import annotations
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    10
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    11
import os
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    12
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    13
from ..i18n import _
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    14
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    15
from .. import (
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
    16
    error,
34704
c51380879054 hgweb: rewrite most obviously-native-strings to be native strings
Augie Fackler <augie@google.com>
parents: 30509
diff changeset
    17
    pycompat,
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    18
)
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    20
from ..utils import procutil
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    21
49389
093e5c274f54 typing: suppress a few pyi-errors with more recent pytype
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    22
# pytype: disable=pyi-error
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    23
from . import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    24
    hgweb_mod,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    25
    hgwebdir_mod,
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    26
    server,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 10996
diff changeset
    27
)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    28
49389
093e5c274f54 typing: suppress a few pyi-errors with more recent pytype
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    29
# pytype: enable=pyi-error
093e5c274f54 typing: suppress a few pyi-errors with more recent pytype
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
    30
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    31
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    32
def hgweb(config, name=None, baseui=None):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45820
diff changeset
    33
    """create an hgweb wsgi object
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    34
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    35
    config can be one of:
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    36
    - repo object (single repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    37
    - path to repo (single repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    38
    - path to config file (multi-repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    39
    - dict of virtual:real pairs (multi-repo view)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    40
    - list of virtual:real tuples (multi-repo view)
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 45820
diff changeset
    41
    """
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    42
48934
06de08b36c82 py3: use str instead of pycompat.unicode
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    43
    if isinstance(config, str):
42208
91104f10ff65 py3: properly reject non-encoded strings given to hgweb
Ludovic Chabant <ludovic@chabant.com>
parents: 38199
diff changeset
    44
        raise error.ProgrammingError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
            b'Mercurial only supports encoded strings: %r' % config
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    46
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    47
    if (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    48
        (isinstance(config, bytes) and not os.path.isdir(config))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    49
        or isinstance(config, dict)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    50
        or isinstance(config, list)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    51
    ):
10996
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    52
        # create a multi-dir interface
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    53
        return hgwebdir_mod.hgwebdir(config, baseui=baseui)
f6d41bfc189e hgweb: make hgweb.hgweb a unified interface to hgweb/hgwebdir
Matt Mackall <mpm@selenic.com>
parents: 10994
diff changeset
    54
    return hgweb_mod.hgweb(config, name=name, baseui=baseui)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    55
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    56
10992
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    57
def hgwebdir(config, baseui=None):
a9b8c8c8ce80 hgweb: make top-level prototypes mirror their callees
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    58
    return hgwebdir_mod.hgwebdir(config, baseui=baseui)
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 2391
diff changeset
    59
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    60
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48934
diff changeset
    61
class httpservice:
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    62
    def __init__(self, ui, app, opts):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    63
        self.ui = ui
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    64
        self.app = app
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    65
        self.opts = opts
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    66
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    67
    def init(self):
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34704
diff changeset
    68
        procutil.setsignalhandler()
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    69
        self.httpd = server.create_server(self.ui, self.app)
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    70
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    71
        if (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
            self.opts[b'port']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    73
            and not self.ui.verbose
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
            and not self.opts[b'print_url']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    75
        ):
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    76
            return
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    77
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    78
        if self.httpd.prefix:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
            prefix = self.httpd.prefix.strip(b'/') + b'/'
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    80
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
            prefix = b''
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    82
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    83
        port = ':%d' % self.httpd.port
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    84
        if port == ':80':
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    85
            port = ''
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    86
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    87
        bindaddr = self.httpd.addr
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    88
        if bindaddr == '0.0.0.0':
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    89
            bindaddr = '*'
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    90
        elif ':' in bindaddr:  # IPv6
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    91
            bindaddr = '[%s]' % bindaddr
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    92
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
    93
        fqaddr = self.httpd.fqaddr
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    94
        if ':' in fqaddr:
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43117
diff changeset
    95
            fqaddr = '[%s]' % fqaddr
38199
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
    96
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    97
        url = b'http://%s%s/%s' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    98
            pycompat.sysbytes(fqaddr),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
    99
            pycompat.sysbytes(port),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   100
            prefix,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   101
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
        if self.opts[b'print_url']:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
            self.ui.write(b'%s\n' % url)
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   104
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   105
            if self.opts[b'port']:
38199
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
   106
                write = self.ui.status
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
   107
            else:
aea29e81753a serve: add option print-url
Nicola Spanti <nicola.spanti@logilab.fr>
parents: 37745
diff changeset
   108
                write = self.ui.write
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   109
            write(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   110
                _(b'listening at %s (bound to %s:%d)\n')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   111
                % (url, pycompat.sysbytes(bindaddr), self.httpd.port)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   112
            )
27138
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   113
        self.ui.flush()  # avoid buffering of status message
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   114
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   115
    def run(self):
ea8e27e6098d hgweb: move httpservice object from commands module
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   116
        self.httpd.serve_forever()
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   117
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   118
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   119
def createapp(baseui, repo, webconf):
27181
a9cecc7b68d3 hgweb: eliminate duck-typing to select hgweb or hgwebdir by command option
Yuya Nishihara <yuya@tcha.org>
parents: 27139
diff changeset
   120
    if webconf:
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   121
        return hgwebdir_mod.hgwebdir(webconf, baseui=baseui)
27181
a9cecc7b68d3 hgweb: eliminate duck-typing to select hgweb or hgwebdir by command option
Yuya Nishihara <yuya@tcha.org>
parents: 27139
diff changeset
   122
    else:
27139
d73f23344dc7 hgweb: extract factory function of httpservice object
Yuya Nishihara <yuya@tcha.org>
parents: 27138
diff changeset
   123
        if not repo:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   124
            raise error.RepoError(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43077
diff changeset
   125
                _(b"there is no Mercurial repository here (.hg not found)")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42208
diff changeset
   126
            )
30508
9195bc4cb816 hgweb: extract app factory
Yuya Nishihara <yuya@tcha.org>
parents: 27184
diff changeset
   127
        return hgweb_mod.hgweb(repo, baseui=baseui)