Show More
@@ -0,0 +1,108 | |||
|
1 | # Gather code related to command dealing with configuration. | |
|
2 | ||
|
3 | from __future__ import annotations | |
|
4 | ||
|
5 | import os | |
|
6 | ||
|
7 | from typing import Any, Dict, Optional | |
|
8 | ||
|
9 | from ..i18n import _ | |
|
10 | ||
|
11 | from .. import ( | |
|
12 | cmdutil, | |
|
13 | error, | |
|
14 | requirements, | |
|
15 | ui as uimod, | |
|
16 | util, | |
|
17 | vfs as vfsmod, | |
|
18 | ) | |
|
19 | ||
|
20 | from . import rcutil | |
|
21 | ||
|
22 | EDIT_FLAG = 'edit' | |
|
23 | ||
|
24 | ||
|
25 | # keep typing simple for now | |
|
26 | ConfigLevelT = str | |
|
27 | LEVEL_USER = 'user' # "user" is the default level and never passed explicitly | |
|
28 | LEVEL_LOCAL = 'local' | |
|
29 | LEVEL_GLOBAL = 'global' | |
|
30 | LEVEL_SHARED = 'shared' | |
|
31 | LEVEL_NON_SHARED = 'non_shared' | |
|
32 | EDIT_LEVELS = ( | |
|
33 | LEVEL_USER, | |
|
34 | LEVEL_LOCAL, | |
|
35 | LEVEL_GLOBAL, | |
|
36 | LEVEL_SHARED, | |
|
37 | LEVEL_NON_SHARED, | |
|
38 | ) | |
|
39 | ||
|
40 | ||
|
41 | def find_edit_level( | |
|
42 | ui: uimod.ui, repo, opts: Dict[str, Any] | |
|
43 | ) -> Optional[ConfigLevelT]: | |
|
44 | """return the level we should edit, if any. | |
|
45 | ||
|
46 | Parse the command option to detect when an edit is requested, and if so the | |
|
47 | configuration level we should edit. | |
|
48 | """ | |
|
49 | if opts.get(EDIT_FLAG) or any(opts.get(o) for o in EDIT_LEVELS): | |
|
50 | cmdutil.check_at_most_one_arg(opts, *EDIT_LEVELS) | |
|
51 | for level in EDIT_LEVELS: | |
|
52 | if opts.get(level): | |
|
53 | return level | |
|
54 | return EDIT_LEVELS[0] | |
|
55 | return None | |
|
56 | ||
|
57 | ||
|
58 | def edit_config(ui: uimod.ui, repo, level: ConfigLevelT) -> None: | |
|
59 | """let the user edit configuration file for the given level""" | |
|
60 | ||
|
61 | if level == LEVEL_USER: | |
|
62 | paths = rcutil.userrcpath() | |
|
63 | elif level == LEVEL_GLOBAL: | |
|
64 | paths = rcutil.systemrcpath() | |
|
65 | elif level == LEVEL_LOCAL: | |
|
66 | if not repo: | |
|
67 | raise error.InputError(_(b"can't use --local outside a repository")) | |
|
68 | paths = [repo.vfs.join(b'hgrc')] | |
|
69 | elif level == LEVEL_NON_SHARED: | |
|
70 | paths = [repo.vfs.join(b'hgrc-not-shared')] | |
|
71 | elif level == LEVEL_SHARED: | |
|
72 | if not repo.shared(): | |
|
73 | raise error.InputError( | |
|
74 | _(b"repository is not shared; can't use --shared") | |
|
75 | ) | |
|
76 | if requirements.SHARESAFE_REQUIREMENT not in repo.requirements: | |
|
77 | raise error.InputError( | |
|
78 | _( | |
|
79 | b"share safe feature not enabled; " | |
|
80 | b"unable to edit shared source repository config" | |
|
81 | ) | |
|
82 | ) | |
|
83 | paths = [vfsmod.vfs(repo.sharedpath).join(b'hgrc')] | |
|
84 | else: | |
|
85 | msg = 'unknown config level: %s' % level | |
|
86 | raise error.ProgrammingError(msg) | |
|
87 | ||
|
88 | for f in paths: | |
|
89 | if os.path.exists(f): | |
|
90 | break | |
|
91 | else: | |
|
92 | if LEVEL_GLOBAL: | |
|
93 | samplehgrc = uimod.samplehgrcs[b'global'] | |
|
94 | elif LEVEL_LOCAL: | |
|
95 | samplehgrc = uimod.samplehgrcs[b'local'] | |
|
96 | else: | |
|
97 | samplehgrc = uimod.samplehgrcs[b'user'] | |
|
98 | ||
|
99 | f = paths[0] | |
|
100 | util.writefile(f, util.tonativeeol(samplehgrc)) | |
|
101 | ||
|
102 | editor = ui.geteditor() | |
|
103 | ui.system( | |
|
104 | b"%s \"%s\"" % (editor, f), | |
|
105 | onerr=error.InputError, | |
|
106 | errprefix=_(b"edit failed"), | |
|
107 | blockedtag=b'config_edit', | |
|
108 | ) |
@@ -53,7 +53,6 from . import ( | |||
|
53 | 53 | phases, |
|
54 | 54 | pycompat, |
|
55 | 55 | registrar, |
|
56 | requirements, | |
|
57 | 56 | revsetlang, |
|
58 | 57 | rewriteutil, |
|
59 | 58 | scmutil, |
@@ -61,15 +60,16 from . import ( | |||
|
61 | 60 | shelve as shelvemod, |
|
62 | 61 | state as statemod, |
|
63 | 62 | tags as tagsmod, |
|
64 | ui as uimod, | |
|
65 | 63 | util, |
|
66 | 64 | verify as verifymod, |
|
67 | vfs as vfsmod, | |
|
68 | 65 | wireprotoserver, |
|
69 | 66 | ) |
|
70 | 67 | |
|
71 | 68 | from .cmd_impls import graft as graft_impl |
|
72 |
from .configuration import |
|
|
69 | from .configuration import ( | |
|
70 | command as config_command, | |
|
71 | rcutil, | |
|
72 | ) | |
|
73 | 73 | from .utils import ( |
|
74 | 74 | dateutil, |
|
75 | 75 | procutil, |
@@ -2367,58 +2367,9 def config(ui, repo, *values, **opts): | |||
|
2367 | 2367 | Returns 0 on success, 1 if NAME does not exist. |
|
2368 | 2368 | |
|
2369 | 2369 | """ |
|
2370 | ||
|
2371 | editopts = ('edit', 'local', 'global', 'shared', 'non_shared') | |
|
2372 | if any(opts.get(o) for o in editopts): | |
|
2373 | cmdutil.check_at_most_one_arg(opts, *editopts[1:]) | |
|
2374 | if opts.get('local'): | |
|
2375 | if not repo: | |
|
2376 | raise error.InputError( | |
|
2377 | _(b"can't use --local outside a repository") | |
|
2378 | ) | |
|
2379 | paths = [repo.vfs.join(b'hgrc')] | |
|
2380 | elif opts.get('global'): | |
|
2381 | paths = rcutil.systemrcpath() | |
|
2382 | elif opts.get('shared'): | |
|
2383 | if not repo.shared(): | |
|
2384 | raise error.InputError( | |
|
2385 | _(b"repository is not shared; can't use --shared") | |
|
2386 | ) | |
|
2387 | if requirements.SHARESAFE_REQUIREMENT not in repo.requirements: | |
|
2388 | raise error.InputError( | |
|
2389 | _( | |
|
2390 | b"share safe feature not enabled; " | |
|
2391 | b"unable to edit shared source repository config" | |
|
2392 | ) | |
|
2393 | ) | |
|
2394 | paths = [vfsmod.vfs(repo.sharedpath).join(b'hgrc')] | |
|
2395 | elif opts.get('non_shared'): | |
|
2396 | paths = [repo.vfs.join(b'hgrc-not-shared')] | |
|
2397 | else: | |
|
2398 | paths = rcutil.userrcpath() | |
|
2399 | ||
|
2400 | for f in paths: | |
|
2401 | if os.path.exists(f): | |
|
2402 | break | |
|
2403 | else: | |
|
2404 | if opts.get('global'): | |
|
2405 | samplehgrc = uimod.samplehgrcs[b'global'] | |
|
2406 | elif opts.get('local'): | |
|
2407 | samplehgrc = uimod.samplehgrcs[b'local'] | |
|
2408 | else: | |
|
2409 | samplehgrc = uimod.samplehgrcs[b'user'] | |
|
2410 | ||
|
2411 | f = paths[0] | |
|
2412 | util.writefile(f, util.tonativeeol(samplehgrc)) | |
|
2413 | ||
|
2414 | editor = ui.geteditor() | |
|
2415 | ui.system( | |
|
2416 | b"%s \"%s\"" % (editor, f), | |
|
2417 | onerr=error.InputError, | |
|
2418 | errprefix=_(b"edit failed"), | |
|
2419 | blockedtag=b'config_edit', | |
|
2420 | ) | |
|
2421 | return | |
|
2370 | edit_level = config_command.find_edit_level(ui, repo, opts) | |
|
2371 | if edit_level is not None: | |
|
2372 | return config_command.edit_config(ui, repo, edit_level) | |
|
2422 | 2373 | ui.pager(b'config') |
|
2423 | 2374 | fm = ui.formatter(b'config', pycompat.byteskwargs(opts)) |
|
2424 | 2375 | for t, f in rcutil.rccomponents(): |
General Comments 0
You need to be logged in to leave comments.
Login now