Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/configuration/rcutil.py @ 52458:8c509a70b6fa
config: gather the path to edit through rcutil
Using the common logic helps to reduce potential error when it changes
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Wed, 23 Oct 2024 02:05:03 +0200 |
parents | 22129ce9f86d |
children | 24ee91ba9aa8 |
rev | line source |
---|---|
31684
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 |
51901
f4733654f144
typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents:
48966
diff
changeset
|
8 from __future__ import annotations |
31684
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 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
12 from typing import ( |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
13 Dict, |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
14 List, |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
15 Optional, |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
16 ) |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
17 |
52447
0a81f3ef054c
config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
51901
diff
changeset
|
18 from .. import ( |
52456
3e79ca017157
config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52455
diff
changeset
|
19 configuration as conf_mod, |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
20 encoding, |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
21 localrepo, |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
22 pycompat, |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
23 requirements as requirementsmod, |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
24 util, |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
25 vfs, |
31684
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 |
52447
0a81f3ef054c
config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
51901
diff
changeset
|
28 from ..utils import resourceutil |
43715
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43711
diff
changeset
|
29 |
34645 | 30 if pycompat.iswindows: |
52447
0a81f3ef054c
config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
51901
diff
changeset
|
31 from .. import scmwindows as scmplatform |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
32 else: |
52447
0a81f3ef054c
config: move `rcutil` module under a new `mercurial.configuration` module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
51901
diff
changeset
|
33 from .. import scmposix as scmplatform |
31684
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 |
31684
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 |
52456
3e79ca017157
config: gather constant and type into the `__init__.py`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52455
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:
52455
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:
52455
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:
52455
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 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
44 |
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
45 def _expandrcpath(path: bytes) -> List[FileRCT]: |
31686
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
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:
31685
diff
changeset
|
47 p = util.expandpath(path) |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
48 if os.path.isdir(p): |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
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 ) |
31686
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
53 return [p] |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
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 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
56 def envrcitems(env: Optional[Dict[bytes, bytes]] = None) -> List[ConfigItemT]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
57 """Return [(section, name, value, source)] config items. |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
58 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
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:
31688
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:
31688
diff
changeset
|
61 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
62 If env is not provided, encoding.environ will be used. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
63 """ |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
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:
31688
diff
changeset
|
65 env = encoding.environ |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
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'), |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
70 ] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
71 result = [] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
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:
31688
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:
31688
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)) |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
76 return result |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
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 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
79 def default_rc_resources() -> List[ResourceIDT]: |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
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:
43924
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:
43924
diff
changeset
|
82 return [ |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
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:
43924
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:
43924
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:
43924
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:
43924
diff
changeset
|
87 ] |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
88 |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
89 |
52458
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
diff
changeset
|
90 def rccomponents(use_hgrcpath=True) -> List[ComponentT]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
91 """return an ordered [(type, obj)] about where to load configs. |
31688
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
92 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
93 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is |
52458
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
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:
52457
diff
changeset
|
95 `use_hgrcpath` is False, it is never used. |
31688
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
96 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
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:
31687
diff
changeset
|
98 |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
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:
43924
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:
43924
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:
43924
diff
changeset
|
102 If type is 'resource', obj is a tuple of (package name, resource name). |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
103 """ |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
105 |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
106 _rccomponents = [] |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
107 comp = _rccomponents.append |
31690
d83e51654c8a
rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
31689
diff
changeset
|
108 |
52458
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
diff
changeset
|
109 if b'HGRCPATH' in encoding.environ and use_hgrcpath: |
31698 | 110 # assume HGRCPATH is all about user configs so environments can be |
111 # overridden. | |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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): |
31698 | 114 if not p: |
115 continue | |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
117 comp((conf_mod.LEVEL_ENV_OVERWRITE, b'path', p)) |
31698 | 118 else: |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
120 comp((conf_mod.LEVEL_BUNDLED_RESOURCE, b'resource', r)) |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
121 |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
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:
52456
diff
changeset
|
124 comp(envrc) |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
126 comp((conf_mod.LEVEL_USER, b'path', os.path.normpath(p))) |
31687
07d62fa518a4
rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents:
31686
diff
changeset
|
127 return _rccomponents |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31699
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 |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
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:
52451
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:
52451
diff
changeset
|
133 |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
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:
52451
diff
changeset
|
136 try: |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
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:
52451
diff
changeset
|
139 if not ( |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
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:
52451
diff
changeset
|
142 ): |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
143 return [] |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
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:
52451
diff
changeset
|
146 return [sharedvfs.join(b"hgrc")] |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
147 except IOError: |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
148 pass |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
149 return [] |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
150 |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
151 |
52455
e3b45916c375
config: return component from `repo_components`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52454
diff
changeset
|
152 def repo_components(repo_path: bytes) -> List[ComponentT]: |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
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:
52451
diff
changeset
|
154 components = [] |
52457
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
155 comp = components.append |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
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:
52456
diff
changeset
|
158 comp( |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
159 ( |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
161 b'path', |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
163 ) |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
164 ) |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
165 comp( |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
166 ( |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
168 b'path', |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
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:
52456
diff
changeset
|
170 ) |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
171 ) |
22129ce9f86d
config: include the component level when returning them
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52456
diff
changeset
|
172 return components |
52454
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
173 |
0b791c90280a
repo-config: move rc component of repository inside `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52451
diff
changeset
|
174 |
52458
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
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:
52457
diff
changeset
|
176 components = [] |
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
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:
52457
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:
52457
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:
52457
diff
changeset
|
180 return components |
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
diff
changeset
|
181 |
8c509a70b6fa
config: gather the path to edit through rcutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52457
diff
changeset
|
182 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
183 def defaultpagerenv() -> Dict[bytes, bytes]: |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
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:
31699
diff
changeset
|
185 intended to be set before starting a pager. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
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'} |
44160
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
188 |
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
189 |
52451
91f22b89ea05
config: type `rcutil`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
52447
diff
changeset
|
190 def use_repo_hgrc() -> bool: |
44160
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
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:
44034
diff
changeset
|
192 return b'HGRCSKIPREPO' not in encoding.environ |