##// END OF EJS Templates
config: gather the path to edit through rcutil...
marmoute -
r53324:8c509a70 default
parent child Browse files
Show More
@@ -26,6 +26,11 EDIT_LEVELS = (
26 LEVEL_SHARED,
26 LEVEL_SHARED,
27 LEVEL_NON_SHARED,
27 LEVEL_NON_SHARED,
28 )
28 )
29 # levels that can works without a repository
30 NO_REPO_EDIT_LEVELS = (
31 LEVEL_USER,
32 LEVEL_GLOBAL,
33 )
29
34
30 ConfigItemT = Tuple[bytes, bytes, bytes, bytes]
35 ConfigItemT = Tuple[bytes, bytes, bytes, bytes]
31 ResourceIDT = Tuple[bytes, bytes]
36 ResourceIDT = Tuple[bytes, bytes]
@@ -16,17 +16,13 from .. import (
16 requirements,
16 requirements,
17 ui as uimod,
17 ui as uimod,
18 util,
18 util,
19 vfs as vfsmod,
20 )
19 )
21
20
22 from . import (
21 from . import (
23 ConfigLevelT,
22 ConfigLevelT,
24 EDIT_LEVELS,
23 EDIT_LEVELS,
25 LEVEL_GLOBAL,
26 LEVEL_LOCAL,
27 LEVEL_NON_SHARED,
28 LEVEL_SHARED,
24 LEVEL_SHARED,
29 LEVEL_USER,
25 NO_REPO_EDIT_LEVELS,
30 rcutil,
26 rcutil,
31 )
27 )
32
28
@@ -55,21 +51,14 def find_edit_level(
55 def edit_config(ui: uimod.ui, repo, level: ConfigLevelT) -> None:
51 def edit_config(ui: uimod.ui, repo, level: ConfigLevelT) -> None:
56 """let the user edit configuration file for the given level"""
52 """let the user edit configuration file for the given level"""
57
53
58 if level == LEVEL_USER:
54 # validate input
59 paths = rcutil.userrcpath()
55 if repo is None and level not in NO_REPO_EDIT_LEVELS:
60 elif level == LEVEL_GLOBAL:
56 msg = b"can't use --%s outside a repository" % pycompat.bytestr(level)
61 paths = rcutil.systemrcpath()
57 raise error.InputError(_(msg))
62 elif level == LEVEL_LOCAL:
58 if level == LEVEL_SHARED:
63 if not repo:
64 raise error.InputError(_(b"can't use --local outside a repository"))
65 paths = [repo.vfs.join(b'hgrc')]
66 elif level == LEVEL_NON_SHARED:
67 paths = [repo.vfs.join(b'hgrc-not-shared')]
68 elif level == LEVEL_SHARED:
69 if not repo.shared():
59 if not repo.shared():
70 raise error.InputError(
60 msg = _(b"repository is not shared; can't use --shared")
71 _(b"repository is not shared; can't use --shared")
61 raise error.InputError(msg)
72 )
73 if requirements.SHARESAFE_REQUIREMENT not in repo.requirements:
62 if requirements.SHARESAFE_REQUIREMENT not in repo.requirements:
74 raise error.InputError(
63 raise error.InputError(
75 _(
64 _(
@@ -77,24 +66,32 def edit_config(ui: uimod.ui, repo, leve
77 b"unable to edit shared source repository config"
66 b"unable to edit shared source repository config"
78 )
67 )
79 )
68 )
80 paths = [vfsmod.vfs(repo.sharedpath).join(b'hgrc')]
69
81 else:
70 # find rc files paths
71 repo_path = None
72 if repo is not None:
73 repo_path = repo.root
74 all_rcs = rcutil.all_rc_components(repo_path)
75 rc_by_level = {}
76 for lvl, rc_type, values in all_rcs:
77 if rc_type != b'path':
78 continue
79 rc_by_level.setdefault(lvl, []).append(values)
80
81 if level not in rc_by_level:
82 msg = 'unknown config level: %s' % level
82 msg = 'unknown config level: %s' % level
83 raise error.ProgrammingError(msg)
83 raise error.ProgrammingError(msg)
84
84
85 paths = rc_by_level[level]
85 for f in paths:
86 for f in paths:
86 if os.path.exists(f):
87 if os.path.exists(f):
87 break
88 break
88 else:
89 else:
89 if LEVEL_GLOBAL:
90 samplehgrc = uimod.samplehgrcs.get(level)
90 samplehgrc = uimod.samplehgrcs[b'global']
91 elif LEVEL_LOCAL:
92 samplehgrc = uimod.samplehgrcs[b'local']
93 else:
94 samplehgrc = uimod.samplehgrcs[b'user']
95
91
96 f = paths[0]
92 f = paths[0]
97 util.writefile(f, util.tonativeeol(samplehgrc))
93 if samplehgrc is not None:
94 util.writefile(f, util.tonativeeol(samplehgrc))
98
95
99 editor = ui.geteditor()
96 editor = ui.geteditor()
100 ui.system(
97 ui.system(
@@ -87,11 +87,12 def default_rc_resources() -> List[Resou
87 ]
87 ]
88
88
89
89
90 def rccomponents() -> List[ComponentT]:
90 def rccomponents(use_hgrcpath=True) -> List[ComponentT]:
91 """return an ordered [(type, obj)] about where to load configs.
91 """return an ordered [(type, obj)] about where to load configs.
92
92
93 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
93 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is
94 used. if $HGRCPATH is not set, the platform default will be used.
94 used. if $HGRCPATH is not set, the platform default will be used. If
95 `use_hgrcpath` is False, it is never used.
95
96
96 if a directory is provided, *.rc files under it will be used.
97 if a directory is provided, *.rc files under it will be used.
97
98
@@ -105,7 +106,7 def rccomponents() -> List[ComponentT]:
105 _rccomponents = []
106 _rccomponents = []
106 comp = _rccomponents.append
107 comp = _rccomponents.append
107
108
108 if b'HGRCPATH' in encoding.environ:
109 if b'HGRCPATH' in encoding.environ and use_hgrcpath:
109 # assume HGRCPATH is all about user configs so environments can be
110 # assume HGRCPATH is all about user configs so environments can be
110 # overridden.
111 # overridden.
111 comp(envrc)
112 comp(envrc)
@@ -171,6 +172,14 def repo_components(repo_path: bytes) ->
171 return components
172 return components
172
173
173
174
175 def all_rc_components(repo_path: Optional[bytes]):
176 components = []
177 components.extend(rccomponents(use_hgrcpath=False))
178 if repo_path is not None:
179 components.extend(repo_components(repo_path))
180 return components
181
182
174 def defaultpagerenv() -> Dict[bytes, bytes]:
183 def defaultpagerenv() -> Dict[bytes, bytes]:
175 """return a dict of default environment variables and their values,
184 """return a dict of default environment variables and their values,
176 intended to be set before starting a pager.
185 intended to be set before starting a pager.
General Comments 0
You need to be logged in to leave comments. Login now