mercurial/configuration/rcutil.py
author Matt Harbison <matt_harbison@yahoo.com>
Sun, 05 Jan 2025 21:03:17 -0500
changeset 52640 24ee91ba9aa8
parent 52427 8c509a70b6fa
permissions -rw-r--r--
pyupgrade: drop usage of py3 aliases for `OSError` These were different classes in py2, but now a handful of error classes are just an alias of `OSError`, like `IOError`, `EnvironmentError`, `WindowsError`, etc. This is the result of running a hacked version of `pyupgrade` 3.19.1[1] $ hg files -0 'relglob:**.py' | xargs -0 \ pyupgrade --py38-plus --keep-percent-format --keep-mock --keep-runtime-typing The hack is because it doesn't have command line switches to disable most changes, so it makes tons of unrelated changes all at once. The hack is to 1) patch `pyupgrade._main._fix_tokens()` to immediately return its content arg 2) change `pyupgrade._data.register_decorator()` to only register the function if it's from the fixer we're interested in: if func.__module__ in ( "pyupgrade._plugins.exceptions", ): FUNCS[tp].append(func) return func [1] https://github.com/asottile/pyupgrade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     1
# rcutil.py - utilities about config paths, special config sections etc.
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     2
#
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     3
#  Copyright Mercurial Contributors
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     4
#
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     7
51859
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 48875
diff changeset
     8
from __future__ import annotations
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
     9
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    10
import os
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    11
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    12
from typing import (
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    13
    Dict,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    14
    List,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    15
    Optional,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    16
)
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    17
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
    18
from .. import (
52425
3e79ca017157 config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52424
diff changeset
    19
    configuration as conf_mod,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    20
    encoding,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
    21
    localrepo,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    22
    pycompat,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
    23
    requirements as requirementsmod,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    24
    util,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
    25
    vfs,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    26
)
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    27
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
    28
from ..utils import resourceutil
43674
5be909dbe385 util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 43670
diff changeset
    29
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 32208
diff changeset
    30
if pycompat.iswindows:
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
    31
    from .. import scmwindows as scmplatform
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    32
else:
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
    33
    from .. import scmposix as scmplatform
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    34
32078
bf5e13e38390 pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents: 31954
diff changeset
    35
fallbackpager = scmplatform.fallbackpager
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    36
systemrcpath = scmplatform.systemrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    37
userrcpath = scmplatform.userrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
    38
52425
3e79ca017157 config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52424
diff changeset
    39
ComponentT = conf_mod.ComponentT
3e79ca017157 config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52424
diff changeset
    40
ConfigItemT = conf_mod.ConfigItemT
3e79ca017157 config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52424
diff changeset
    41
FileRCT = conf_mod.FileRCT
3e79ca017157 config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52424
diff changeset
    42
ResourceIDT = conf_mod.ResourceIDT
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
    43
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    44
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    45
def _expandrcpath(path: bytes) -> List[FileRCT]:
31681
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    46
    '''path could be a file or a directory. return a list of file paths'''
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    47
    p = util.expandpath(path)
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    48
    if os.path.isdir(p):
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    49
        join = os.path.join
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
    50
        return sorted(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    51
            join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc')
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
    52
        )
31681
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    53
    return [p]
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
    54
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
    55
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    56
def envrcitems(env: Optional[Dict[bytes, bytes]] = None) -> List[ConfigItemT]:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
    57
    """Return [(section, name, value, source)] config items.
31684
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    58
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    59
    The config items are extracted from environment variables specified by env,
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    60
    used to override systemrc, but not userrc.
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    61
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    62
    If env is not provided, encoding.environ will be used.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
    63
    """
31684
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    64
    if env is None:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    65
        env = encoding.environ
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    66
    checklist = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    67
        (b'EDITOR', b'ui', b'editor'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    68
        (b'VISUAL', b'ui', b'editor'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    69
        (b'PAGER', b'pager', b'pager'),
31684
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    70
    ]
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    71
    result = []
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    72
    for envname, section, configname in checklist:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    73
        if envname not in env:
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    74
            continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    75
        result.append((section, configname, env[envname], b'$%s' % envname))
31684
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    76
    return result
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
    77
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
    78
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
    79
def default_rc_resources() -> List[ResourceIDT]:
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    80
    """return rc resource IDs in defaultrc"""
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    81
    rsrcs = resourceutil.contents(b'mercurial.defaultrc')
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    82
    return [
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    83
        (b'mercurial.defaultrc', r)
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    84
        for r in sorted(rsrcs)
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    85
        if resourceutil.is_resource(b'mercurial.defaultrc', r)
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    86
        and r.endswith(b'.rc')
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    87
    ]
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    88
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    89
52427
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
    90
def rccomponents(use_hgrcpath=True) -> List[ComponentT]:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
    91
    """return an ordered [(type, obj)] about where to load configs.
31683
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
    92
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
    93
    respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
52427
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
    94
    used. if $HGRCPATH is not set, the platform default will be used. If
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
    95
    `use_hgrcpath` is False, it is never used.
31683
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
    96
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
    97
    if a directory is provided, *.rc files under it will be used.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
    98
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
    99
    type could be either 'path', 'items' or 'resource'. If type is 'path',
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
   100
    obj is a string, and is the config file path. if type is 'items', obj is a
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
   101
    list of (section, name, value, source) that should fill the config directly.
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
   102
    If type is 'resource', obj is a tuple of (package name, resource name).
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
   103
    """
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   104
    envrc = (conf_mod.LEVEL_ENV_OVERWRITE, b'items', envrcitems())
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   105
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   106
    _rccomponents = []
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   107
    comp = _rccomponents.append
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31684
diff changeset
   108
52427
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   109
    if b'HGRCPATH' in encoding.environ and use_hgrcpath:
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
   110
        # assume HGRCPATH is all about user configs so environments can be
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
   111
        # overridden.
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   112
        comp(envrc)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
   113
        for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep):
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
   114
            if not p:
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
   115
                continue
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   116
            for p in _expandrcpath(p):
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   117
                comp((conf_mod.LEVEL_ENV_OVERWRITE, b'path', p))
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
   118
    else:
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   119
        for r in default_rc_resources():
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   120
            comp((conf_mod.LEVEL_BUNDLED_RESOURCE, b'resource', r))
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
   121
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   122
        for p in systemrcpath():
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   123
            comp((conf_mod.LEVEL_GLOBAL, b'path', os.path.normpath(p)))
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   124
        comp(envrc)
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   125
        for p in userrcpath():
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   126
            comp((conf_mod.LEVEL_USER, b'path', os.path.normpath(p)))
31682
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31681
diff changeset
   127
    return _rccomponents
31954
e518192d6bac pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents: 31694
diff changeset
   128
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
   129
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   130
def _shared_source_component(path: bytes) -> List[FileRCT]:
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   131
    """if the current repository is shared one, this tries to read
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   132
    .hg/hgrc of shared source if we are in share-safe mode
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   133
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   134
    This should be called before reading .hg/hgrc or the main repo
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   135
    as that overrides config set in shared source"""
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   136
    try:
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   137
        with open(os.path.join(path, b".hg", b"requires"), "rb") as fp:
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   138
            requirements = set(fp.read().splitlines())
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   139
            if not (
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   140
                requirementsmod.SHARESAFE_REQUIREMENT in requirements
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   141
                and requirementsmod.SHARED_REQUIREMENT in requirements
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   142
            ):
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   143
                return []
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   144
            hgvfs = vfs.vfs(os.path.join(path, b".hg"))
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   145
            sharedvfs = localrepo._getsharedvfs(hgvfs, requirements)
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   146
            return [sharedvfs.join(b"hgrc")]
52640
24ee91ba9aa8 pyupgrade: drop usage of py3 aliases for `OSError`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52427
diff changeset
   147
    except OSError:
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   148
        pass
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   149
    return []
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   150
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   151
52424
e3b45916c375 config: return component from `repo_components`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52423
diff changeset
   152
def repo_components(repo_path: bytes) -> List[ComponentT]:
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   153
    """return the list of config file to read for a repository"""
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   154
    components = []
52426
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   155
    comp = components.append
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   156
    for p in _shared_source_component(repo_path):
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   157
        comp((conf_mod.LEVEL_SHARED, b'path', p))
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   158
    comp(
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   159
        (
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   160
            conf_mod.LEVEL_LOCAL,
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   161
            b'path',
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   162
            os.path.join(repo_path, b".hg", b"hgrc"),
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   163
        )
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   164
    )
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   165
    comp(
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   166
        (
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   167
            conf_mod.LEVEL_NON_SHARED,
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   168
            b'path',
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   169
            os.path.join(repo_path, b".hg", b"hgrc-not-shared"),
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   170
        )
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   171
    )
22129ce9f86d config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52425
diff changeset
   172
    return components
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   173
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
   174
52427
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   175
def all_rc_components(repo_path: Optional[bytes]):
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   176
    components = []
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   177
    components.extend(rccomponents(use_hgrcpath=False))
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   178
    if repo_path is not None:
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   179
        components.extend(repo_components(repo_path))
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   180
    return components
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   181
8c509a70b6fa config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52426
diff changeset
   182
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
   183
def defaultpagerenv() -> Dict[bytes, bytes]:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
   184
    """return a dict of default environment variables and their values,
31954
e518192d6bac pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents: 31694
diff changeset
   185
    intended to be set before starting a pager.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
   186
    """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
   187
    return {b'LESS': b'FRX', b'LV': b'-c'}
44243
238790674d69 config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 44032
diff changeset
   188
238790674d69 config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 44032
diff changeset
   189
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
   190
def use_repo_hgrc() -> bool:
44243
238790674d69 config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 44032
diff changeset
   191
    """True if repositories `.hg/hgrc` config should be read"""
238790674d69 config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 44032
diff changeset
   192
    return b'HGRCSKIPREPO' not in encoding.environ