views:

6181

answers:

3

I'm trying to get Mercurial to ignore a configuration file, but I'm failing to get it working.

I have created a repository on my server with hg init and cloned that repository to my computer. I then want to be able to edit the configurationj file but not commit those changes back to the server.

I have tried creating a .hgignore in the root of my clone, but Mercurial flags the file with a ? and whether I commit it or not it still continues to log my configuration changes.

Am I creating the .hgignore file in the wrong place, does this file need to be commited? Does it need to be created before I init the repository on the server?

+8  A: 

Add the .hgignore file itself to the .hgignore file:

syntax: glob

.hgignore

or, just check the .hgignore file in to your local clone...

John Weldon
Per your requirement, to not want to push the .hgignore back to any sources, I would recommend this simple approach. Use a local .hgignore which you do not commit, and add whatever you want to it, placing .hgignore first.If you check .hgignore into your local clone, you might inadvertantly push it back some day.
maxwellb
+28  A: 
  • .hgignore does not need to be created before init
  • It the config file will be used by others, you'd better commit the .hgignore so others dont have to create it, but this is not needed for mercurial to ignore your local files (see example)
  • Yes .hgignore has to be in the root directory

Simple example.

Init the repo:

$ mkdir test 
$ cd test 
$ hg init

Create a file

$ touch foo 
$ hg st 
? foo

Now create a .hgignore, in the root directory of your repo:

$ echo 'foo' > .hgignore

foo is now ignored:

$ hg st 
? .hgignore

Note that .hgignore does not need to be committed for this to work.

A bit more tricky

If both the config file and a .hgignore (ignoring the config file) are committed in the repo, then yes, the config file changes will be tracked (in other words, .hgignore will have no effect)

Creating config and commit it

$ touch config
$ hg ci config -Am 'adding conf'

Ignore it:

$ echo 'config' >> .hgignore

Commit .hgignore:

$ hg ci .hgignore -Am '.hgignore'

Then if you clone the repo:

$ cd ..
$ hg clone test other-user
$ cd other-user

and modify config:

$ echo 'new config param' >> config

then hg will show the changes:

$ hg st
M config

How to deal with this?

You probably want to have a main default configuration file, that is versioned, in the repository, global.cfg

And you will ask users to create a local.cfg where they will put their local settings. And your script will source the local.cfg if present: the local settings override the global ones. Of course, add a line to .hgignore to ignore local.cfg ;)

Alternative: no global config file, only a config.example that users copy and modify locally. The con here is that you will not keep track easily of changes between versions.

NicDumZ
Should the config file be part of the main repo? If it is and I pull it down won't it already be part of the clone, so hgignore will have no effect?
Tom
True, it will have no effect. See my edit for possible solutions :)
NicDumZ
The best way is to make your application load global.cfg first, and then local.cfg if it exists. Then add global.cfg to Mercurial and add "local.cfg" to .hgignore. That will let you version the defaults in global.cfg and the users can still override them as necessary in the local.cfg file. The important thing to remember is that .hgignore can only ignore files *not* tracked by Mercurial. So if you add "*.cfg" to .hgignore, the global.cfg file will still be committed as normal.
Martin Geisler
+7  A: 

Even if you have ignored files, Mercurial will track them once they have been added to the repository.

To remove your config file from the repository, you can use

hg remove -Af file.cfg

This will remove the file from from the repository (once committed) without deleting your local file. See hg help remove However, there is now a delete recorded in the repo, and Mercurial will remove that file when your or anyone else updates across that revision.

You could also rename it and have people copy the master to their local file, but you will need to coordinate updates of the local file.

hg rename file.cfg file.cfg.example
Justin Love