Joda Maki
Joda Maki

Reputation: 5869

Version Control a file with per machine dependent stuff

I have a config file in my project that includes some info that is per machine dependent (db username, password, path). I understand that in this particular case, I could enforce everybody to use the same username, db path, and password to keep this simple, but there must be another way to deal with this problem.

I use mercurial, if you care, but I am ok with just a theoretical answer if you are unfamiliar with hg specifics.

Upvotes: 0

Views: 106

Answers (5)

You'll want to use the .hgignore file to not include the config file in the repository.

This will allow everyone to have their own version of the config file.

Basically, you just want to add the relative path to the config file and Mercurial commands will ignore it. So the file would look like this:

config/dbconfig.ext

Edit
I just realized you still want to be able to version control the config file (misunderstood the question). So I suggest moving the parts of the config file that are dependent into their own config file and then applying the fix above. That way, you can still have the regular config information under version control and keep part of it separate for each person's machine.

Upvotes: 1

Adam Jaskiewicz
Adam Jaskiewicz

Reputation: 10996

Without knowing exactly what is in your config file, I'm going to assume your file has some stuff that is machine-dependent (e.g., db password, paths) and other stuff that is not (db hostname, maybe some paths relative to a path that is configured on a per-machine basis, etc.)

If that's the case, what you want to do is re-factor your config file so that you have two config files---one for the common stuff, one for the machine-specific stuff. Check the common one in, and add the machine-specific configuration to the ignore file.

Upvotes: 0

ezod
ezod

Reputation: 7421

A common way to handle this is to put a config.example or similar under version control and force the user to copy it and make any necessary changes. That way the user can pull down the overall structure of the file from your repository without overwriting local changes.

Alternatively, you could make your config file provide only defaults, with the option to source a subset of variables from a higher-priority custom config file (in the same format) which the user may or may not provide.

Upvotes: 1

John Giotta
John Giotta

Reputation: 16964

I've never found a solid method for handling this type of configuration files. My final solution was to just maintain a version of each file and use symbolic links. That way each server has the same file path, but different root file.

Upvotes: 0

Jono
Jono

Reputation: 1750

I have per machine databases for my PHP projects. What I do is check the hostname at runtime. If it is one host, I feed it certain credentials. If another, feed it different credentials.

On some systems I create a list of credentials and then just go down the line trying them until one of the connections works. If the list is exhausted, the connection cannot be made.

Upvotes: 0

Related Questions