6

On any non-trivial hg installation, the hgrc's tend to contain significant stuff.

Is there a way to completely ignore/bypass ALL configurations, from system, user, to repo-level?

The use case is to use some hg core functionalities in some automation scripts. Currently, if anything is misconfigured (and I mess with my ~/.hgrc a lot), the scripts will abort for something it doesn't use at all.

It'd be perfect is I can just hg <whatever> --config:none.

Geoffrey Zheng
  • 6,562
  • 2
  • 38
  • 47

2 Answers2

8

You can do it by setting your HGRCPATH environment variable to something with no configuration in it.

ry4an@hail [~/hg/crew] % hg showconfig | grep Ry4an
ui.username=Ry4an Brase <ry4an@msi.umn.edu>
ry4an@hail [~/hg/crew] % HGRCPATH=/dev/null hg showconfig | grep Ry4an
ry4an@hail [~/hg/crew] % 

Also if you're invoking from a script consider HGPLAIN too.

Both found here: https://www.mercurial-scm.org/repo/hg/file/e3b87fb34d00/mercurial/help/environment.txt

Which says:

    41 HGRCPATH
    42     A list of files or directories to search for configuration
    43     files. Item separator is ":" on Unix, ";" on Windows. If HGRCPATH
    44     is not set, platform default search path is used. If empty, only
    45     the .hg/hgrc from the current repository is read.
    46 
    47     For each element in HGRCPATH:
    48 
    49     - if it's a directory, all files ending with .rc are added
    50     - otherwise, the file itself will be added
    51 
    52 HGPLAIN
    53     When set, this disables any configuration settings that might
    54     change Mercurial's default output. This includes encoding,
    55     defaults, verbose mode, debug mode, quiet mode, tracebacks, and
    56     localization. This can be useful when scripting against Mercurial
    57     in the face of existing user configuration.
    58 
    59     Equivalent options set via command line flags or environment
    60     variables are not overridden.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • Which hg version do you use? I tried 1.5.4 and 1.7.2, for both `HGRCPATH=/dev/null hg showconfig` still shows repo config. I suspect that your username is set in user config (which is usually the case), and `hg showconfig --debug|grep Ry4an` would confirm that. – Geoffrey Zheng Dec 24 '10 at 03:16
  • My eyes--the doc says right there that `If empty, only the .hg/hgrc from the current repository is read`. Is there a way to bypass that as well? – Geoffrey Zheng Jan 21 '11 at 01:18
  • Yeah, I took that to be empty value for the environment variable not empty contents of the file pointed to by the environment variable, but I'm getting the contents of .hg/hgrc included always too. I guess to avoid that you move it out of the way, clone so you have a repo w/o it, or chown it over to a user that's not trusted. – Ry4an Brase Jan 22 '11 at 03:50
2

There doesn't seem to have any option to perform want you want. But since the documentation states that

(Unix, Windows) /.hg/hgrc

Per-repository configuration options that only apply in a particular repository. This file is not version-controlled, and will not get transferred during a "clone" operation. Options in this file override options in all other configuration files. On Unix, most of this file will be ignored if it doesn't belong to a trusted user or to a trusted group. See the documentation for the trusted section below for more details.

The following script will read the stdin and convert the output of hg -showconfig to an override config that could be written at <repo>/.hg/hgrc. Effectively it overrides all the current config found by hg. The script might have to be tweaked but it seems to work so far.

# File override.py

import sys

config = dict()
for l in sys.stdin.readlines():
    section, sep, value = l.partition('.')
    if not section in config:
        config[section] = []
    config[section].append(value.split("=")[0])

for k in iter(config):
    print "[{0}]".format(k)
    for v in config[k]:
        print v + "="

It can then be used as such:

> rm -f .hg/hgrc
> hg -showconfig | python override.py > .hg/hgrc
Rod
  • 52,748
  • 3
  • 38
  • 55
  • Thanks, but that's not really a solution, since I can add a new config in my ~/.hgrc and forget to override that in the repo hgrc. – Geoffrey Zheng Dec 23 '10 at 20:01
  • @Geoffrey Zheng True. You could potentially automate the override by the script I have added. – Rod Dec 23 '10 at 20:41
  • Did you really just edit your answer 9 minutes after mine to append the much better solution I suggested? – Ry4an Brase Dec 23 '10 at 21:31
  • @Ry4an. I did to point out the that you could set it to nothing. I should have given you the credit as I did with my latest edit. – Rod Dec 23 '10 at 21:40
  • Setting it to nothing still uses the repository's hgrc, and he wants to ignore even that. – Ry4an Brase Dec 23 '10 at 21:49
  • Which could be easily removed by a script. Will changing HGRCPATH to somewhere where there is no config prevent from reading the repository hgrc ? – Rod Dec 23 '10 at 21:59
  • Yeah, setting it to /dev/null like I did precludes reading any hgrc file. – Ry4an Brase Dec 23 '10 at 22:09
  • Too bad that the script doesn't work for bundled extension, for example `hgext.mq=` means enabling it. – Geoffrey Zheng Dec 24 '10 at 03:33