17

I'm looking for a way to set .hgrc configuration items without actually editing the text file. I'm trying to standardize the setup of the hgrc across multiple developers and I would like a command like

hg --config ui.username=foo

but which also saves that config change into the hgrc file.

It seems like this should be something that should be supported directly in the vanilla hg command, but I can't find it anywhere.

Cœur
  • 37,241
  • 25
  • 195
  • 267
johnkpaul
  • 417
  • 3
  • 10

3 Answers3

10

Someone -- either you or Mercurial -- will have to edit the configuration file if you want the config change to be saved :-)

And if you can call Mercurial with

hg --config ui.username=foo

then you should also be able to do

echo '[ui]' >> ~/.hgrc
echo 'username = foo' >> ~/.hgrc

which will save the config change, not matter how the ~/.hgrc file happens to look like (it is okay to have multiple [ui] sections).

Mercurial 3.0 and later has the hg config --edit command that opens an editor with the user config file. Still not quite what you're asking for, but at least this makes it easier to edit the file interactively.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • 3
    And if the process under which hg is running has no '~' or HOME dir, or it's running as a system/daemon user with a '~' dir you can't write to: :~( – Garen May 02 '12 at 16:33
  • 1
    @Garen: You can always set `HGRCPATH` to override where Mercurial looks for the configuration files. That should be handy for your homeless daemon user. – Martin Geisler May 03 '12 at 09:18
  • 2
    Certain popular CI systems (e.g. TeamCity) offer no way to do this. The documentation is also plain wrong--for instance hg doesn't check the documented system-wide locations like C:\Mercurial, and I've never been able to get it to read the advertised registry location. On Linux it can get tough when your build occurs inside a chroot where you might have limited ability to effect environment settings, have no homedir, etc. I could go on and on, the point being that it would be SO easy to get around them IFF Mercurial would just let us specify a config-file path on the command line. :) – Garen May 04 '12 at 17:26
  • 1
    @Garen: I was surprised to find a reference to `C:\Mercurial\Mercurial.ini` in `hg help config` — that file has not been read since version 1.8. The documentation has now been updated for the upcoming Mercurial 2.2.2 release. – Martin Geisler May 06 '12 at 13:52
  • 5
    @Garen: This seems like a poor replacement for git's --config facility. Echoing your config to the end of the file hurts continuity and structure and at best overrides existing config elements (instead of replacing or appending them 'in place'). – jeckhart Feb 19 '13 at 15:04
  • 1
    I'm not advocating that. What I would like is an option to specify a path to a config file, to ensure that the right settings are always found and used. This is important for automation purposes (specifically, CI). – Garen Feb 22 '13 at 20:40
  • Use a wrapper script (or bat file) that sets `HGRCPATH` for you if you cannot convince your CI tool to set the environment variable. – Martin Geisler Feb 24 '13 at 14:21
  • This has been a request ever since the existence of mercurial AFAIK. Wonder why the stubbornness on not building this one. – Pykler Nov 04 '15 at 17:52
1

There is an extension that helps with this, https://bitbucket.org/alu/hgconfig/wiki/Home

After installing that hgext, you can do things like this.

% hg showconfig paths
paths.default=ssh://hg@bitbucket.org/alu/hgconfig
% hg config paths.upstream $(hg showconfig paths.default)
% hg config paths.default $(hg showconfig paths.default | sed 's|/alu/|/nassrat/|')
% hg showconfig paths
paths.default=ssh://hg@bitbucket.org/nassrat/hgconfig
paths.upstream=ssh://hg@bitbucket.org/alu/hgconfig

The only gotcha is this overrides the builtin config command, you can either tweak the code to change the command name, or live with it. Fortunately, it probably would not matter if your use case is simply to set and get specific configs.

Pykler
  • 14,565
  • 9
  • 41
  • 50
1

This form:

hg --config ui.username=foo

Doesn't save anything. It sets the value for just the one run.

Also you can use /etc/mercurial/hgrc for system wide settings if that helps anything.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169