Mercurial > public > mercurial-scm > hg
comparison mercurial/configuration/command.py @ 52427: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 |
comparison
equal
deleted
inserted
replaced
52426:22129ce9f86d | 52427:8c509a70b6fa |
---|---|
14 formatter, | 14 formatter, |
15 pycompat, | 15 pycompat, |
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 |
33 EDIT_FLAG = 'edit' | 29 EDIT_FLAG = 'edit' |
34 | 30 |
53 | 49 |
54 | 50 |
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 _( |
76 b"share safe feature not enabled; " | 65 b"share safe feature not enabled; " |
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( |
101 b"%s \"%s\"" % (editor, f), | 98 b"%s \"%s\"" % (editor, f), |
102 onerr=error.InputError, | 99 onerr=error.InputError, |