annotate mercurial/configuration/rcutil.py @ 52424:e3b45916c375

config: return component from `repo_components` We used to returns just paths. However using the same return types as `rccomponents` will be quite useful later in this series.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 23 Oct 2024 01:12:52 +0200
parents 0b791c90280a
children 3e79ca017157
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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 Tuple,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
17 Union,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
18 )
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
19
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
20 from .. import (
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
21 encoding,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
22 localrepo,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
23 pycompat,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
24 requirements as requirementsmod,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
25 util,
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
26 vfs,
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
27 )
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
28
52416
0a81f3ef054c config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 51859
diff changeset
29 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
30
34645
75979c8d4572 codemod: use pycompat.iswindows
Jun Wu <quark@fb.com>
parents: 32208
diff changeset
31 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
32 from .. import scmwindows as scmplatform
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
33 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
34 from .. import scmposix as scmplatform
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
35
32078
bf5e13e38390 pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents: 31954
diff changeset
36 fallbackpager = scmplatform.fallbackpager
31679
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
37 systemrcpath = scmplatform.systemrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
38 userrcpath = scmplatform.userrcpath
0f8ba0bc1154 rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff changeset
39
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
40 ConfigItemT = Tuple[bytes, bytes, bytes, bytes]
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
41 ResourceIDT = Tuple[bytes, bytes]
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
42 FileRCT = bytes
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
43 ComponentT = Tuple[
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
44 bytes,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
45 Union[
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
46 List[ConfigItemT],
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
47 FileRCT,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
48 ResourceIDT,
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
49 ],
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
50 ]
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
51
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
52
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
53 def _expandrcpath(path: bytes) -> List[FileRCT]:
31681
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
54 '''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
55 p = util.expandpath(path)
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
56 if os.path.isdir(p):
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
57 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
58 return sorted(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
59 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
60 )
31681
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
61 return [p]
294728f2a908 rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents: 31680
diff changeset
62
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
63
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
64 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
65 """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
66
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
67 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
68 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
69
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
70 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
71 """
31684
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
72 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
73 env = encoding.environ
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
74 checklist = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
75 (b'EDITOR', b'ui', b'editor'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
76 (b'VISUAL', b'ui', b'editor'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
77 (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
78 ]
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
79 result = []
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
80 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
81 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
82 continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
83 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
84 return result
0be96ac9199a rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents: 31683
diff changeset
85
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
86
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
87 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
88 """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
89 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
90 return [
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
91 (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
92 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
93 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
94 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
95 ]
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
96
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
97
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
98 def rccomponents() -> List[ComponentT]:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
99 """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
100
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
101 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
102 used. if $HGRCPATH is not set, the platform default will be used.
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
103
00e569a2da97 rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents: 31682
diff changeset
104 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
105
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
106 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
107 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
108 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
109 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
110 """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
111 envrc = (b'items', envrcitems())
31685
d83e51654c8a rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents: 31684
diff changeset
112
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
113 if b'HGRCPATH' in encoding.environ:
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
114 # 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
115 # overridden.
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
116 _rccomponents = [envrc]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
117 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
118 if not p:
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
119 continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
120 _rccomponents.extend((b'path', p) for p in _expandrcpath(p))
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
121 else:
44031
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
122 _rccomponents = [(b'resource', r) for r in default_rc_resources()]
1864efbe90d9 ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents: 43918
diff changeset
123
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
124 normpaths = lambda paths: [
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
125 (b'path', os.path.normpath(p)) for p in paths
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
126 ]
44032
2d4cad94d08a rcutil: drop the `defaultrcpath()` method (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 44031
diff changeset
127 _rccomponents.extend(normpaths(systemrcpath()))
31693
67f0377bd24b rcutil: unindent a block
Jun Wu <quark@fb.com>
parents: 31692
diff changeset
128 _rccomponents.append(envrc)
31694
10d88dc7c010 rcutil: extract duplicated logic to a lambda
Jun Wu <quark@fb.com>
parents: 31693
diff changeset
129 _rccomponents.extend(normpaths(userrcpath()))
31682
07d62fa518a4 rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents: 31681
diff changeset
130 return _rccomponents
31954
e518192d6bac pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents: 31694
diff changeset
131
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 42093
diff changeset
132
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
133 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
134 """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
135 .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
136
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
137 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
138 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
139 try:
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
140 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
141 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
142 if not (
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
143 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
144 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
145 ):
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
146 return []
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
147 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
148 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
149 return [sharedvfs.join(b"hgrc")]
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
150 except IOError:
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
151 pass
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
152 return []
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
153
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
154
52424
e3b45916c375 config: return component from `repo_components`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52423
diff changeset
155 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
156 """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
157 components = []
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
158 components.extend(_shared_source_component(repo_path))
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
159 components.append(os.path.join(repo_path, b".hg", b"hgrc"))
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
160 components.append(os.path.join(repo_path, b".hg", b"hgrc-not-shared"))
52424
e3b45916c375 config: return component from `repo_components`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52423
diff changeset
161 return [(b'path', c) for c in components]
52423
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
162
0b791c90280a repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52420
diff changeset
163
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
164 def defaultpagerenv() -> Dict[bytes, bytes]:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 44243
diff changeset
165 """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
166 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
167 """
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
168 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
169
238790674d69 config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 44032
diff changeset
170
52420
91f22b89ea05 config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52416
diff changeset
171 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
172 """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
173 return b'HGRCSKIPREPO' not in encoding.environ