Mercurial > hg
annotate mercurial/rcutil.py @ 44031:1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
We will want the ability to cat out these resources, but the same would apply to
templates, so I'm going to wait for the dust to settle on that. Reading the
default config directly from the filesystem is still in place for now.
Differential Revision: https://phab.mercurial-scm.org/D7776
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 29 Dec 2019 21:06:34 -0500 |
parents | 86fe85364811 |
children | 2d4cad94d08a |
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 |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
8 from __future__ import absolute_import |
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 |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
12 from . import ( |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
13 encoding, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
14 pycompat, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
15 util, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
16 ) |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
17 |
43674
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43670
diff
changeset
|
18 from .utils import resourceutil |
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43670
diff
changeset
|
19 |
34645 | 20 if pycompat.iswindows: |
31679
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
21 from . import scmwindows as scmplatform |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
22 else: |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
23 from . import scmposix as scmplatform |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
24 |
32078
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31954
diff
changeset
|
25 fallbackpager = scmplatform.fallbackpager |
31679
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
26 systemrcpath = scmplatform.systemrcpath |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
27 userrcpath = scmplatform.userrcpath |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
28 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
29 |
31681
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
30 def _expandrcpath(path): |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
31 '''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
|
32 p = util.expandpath(path) |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
33 if os.path.isdir(p): |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
34 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
|
35 return sorted( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
36 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
|
37 ) |
31681
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
38 return [p] |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31680
diff
changeset
|
39 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
40 |
31684
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
41 def envrcitems(env=None): |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
42 '''Return [(section, name, value, source)] config items. |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
43 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
44 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
|
45 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
|
46 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
47 If env is not provided, encoding.environ will be used. |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
48 ''' |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
49 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
|
50 env = encoding.environ |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
51 checklist = [ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
52 (b'EDITOR', b'ui', b'editor'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
53 (b'VISUAL', b'ui', b'editor'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
54 (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
|
55 ] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
56 result = [] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
diff
changeset
|
57 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
|
58 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
|
59 continue |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
60 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
|
61 return result |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31683
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 |
31680
448889f9a36c
rcutil: split osrcpath to return default.d paths (API)
Jun Wu <quark@fb.com>
parents:
31679
diff
changeset
|
64 def defaultrcpath(): |
43670
02fe8dedab8c
defaults: rename default.d to defaultrc to make it a valid package name
Martin von Zweigbergk <martinvonz@google.com>
parents:
43077
diff
changeset
|
65 '''return rc paths in defaultrc''' |
43674
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43670
diff
changeset
|
66 defaultpath = os.path.join(resourceutil.datapath, b'defaultrc') |
43918
86fe85364811
rcutil: don't check if defaultrc/ is a directory -- we know it is
Martin von Zweigbergk <martinvonz@google.com>
parents:
43674
diff
changeset
|
67 return _expandrcpath(defaultpath) |
31679
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
68 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
69 |
44031
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
70 def default_rc_resources(): |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
71 """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
|
72 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
|
73 return [ |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
74 (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
|
75 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
|
76 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
|
77 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
|
78 ] |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
79 |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
80 |
31682
07d62fa518a4
rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents:
31681
diff
changeset
|
81 def rccomponents(): |
31683
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31682
diff
changeset
|
82 '''return an ordered [(type, obj)] about where to load configs. |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31682
diff
changeset
|
83 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31682
diff
changeset
|
84 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
|
85 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
|
86 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31682
diff
changeset
|
87 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
|
88 |
44031
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 If type is 'resource', obj is a tuple of (package name, resource name). |
31683
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31682
diff
changeset
|
93 ''' |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
94 envrc = (b'items', envrcitems()) |
31685
d83e51654c8a
rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
31684
diff
changeset
|
95 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
96 if b'HGRCPATH' in encoding.environ: |
31693 | 97 # assume HGRCPATH is all about user configs so environments can be |
98 # overridden. | |
99 _rccomponents = [envrc] | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
100 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep): |
31693 | 101 if not p: |
102 continue | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
103 _rccomponents.extend((b'path', p) for p in _expandrcpath(p)) |
31693 | 104 else: |
44031
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
105 _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
|
106 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
107 normpaths = lambda paths: [ |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
108 (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
|
109 ] |
44031
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43918
diff
changeset
|
110 _rccomponents.extend(normpaths(defaultrcpath() + systemrcpath())) |
31693 | 111 _rccomponents.append(envrc) |
31694
10d88dc7c010
rcutil: extract duplicated logic to a lambda
Jun Wu <quark@fb.com>
parents:
31693
diff
changeset
|
112 _rccomponents.extend(normpaths(userrcpath())) |
31682
07d62fa518a4
rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents:
31681
diff
changeset
|
113 return _rccomponents |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31694
diff
changeset
|
114 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
115 |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31694
diff
changeset
|
116 def defaultpagerenv(): |
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31694
diff
changeset
|
117 '''return a dict of default environment variables and their values, |
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31694
diff
changeset
|
118 intended to be set before starting a pager. |
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31694
diff
changeset
|
119 ''' |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
120 return {b'LESS': b'FRX', b'LV': b'-c'} |