2

I have a setup as follows.

A private repository at bitbucket where I keep the 'master' repository. A repository on my server which acts as the 'live' website. A repository on my laptop which acts as my working copy.

My process is as follows. I make a change to a file in my local repository. I commit these locally. I push these changes to bitbucket. I then pull these changes from my bitbucket to the webserver.

The problem that I have however is that my local copy utilizes different configuration settings for databases, paths etc, ergo what I want is my 'config.php' file at bitbucket to contain the server settings, and the config.php on my local host to contain local settings.

I believe this can be achieved with .hgignore but i have had no success researching. The problem i encounter is that i make my server settings file, push it to bitbucket, 'forget' the file in my local repository, create a .hgignore, and then recreate the file. However when i 'forget' the file TortoiseHG notices and asks me to commit the change to bitbucket....

Any ideas would be greatly appreciated. Thanks

Additional Points.

Following the advice below I have developed a setup as follows:

I have my local repository on my laptop where i do my edits. I have bitbucket which is essentially the 'main' repository - if any other developers join the team they clone this. I have my live repository on my web host.

On my live repository I have a .hgignore file whichs ignores the respective config files. As such when I do hg pull from my host, it pulls the repository as is with the localhost configuration files, but when i type hg update (to the live working copy), these files are ignored/not updated.

Could someone clarify as to if i have understood this correctly, and as to whether this is a suitable way of achieving what I want?

Thanks

Thomas Clowes
  • 4,089
  • 8
  • 38
  • 69

3 Answers3

5

.hgignore only ignores files if they are not versioned already, so I don’t think your idea in the question will work.

The common approach regarding local configuration is generally a variation on the same theme, like of one of the following:

  1. Do not check in the config.php at all. You can check in a config.example.php with the most common settings, and document in the README that users have to copy it to config.php and then edit it.

  2. Put any shared settings in config.php, and add an include statement to point to an unversioned file with settings specific to the machine, e.g. config.local.php. You can also provide an config.local.example.php-file for this.

  3. Like 2, but the config.php contains all default settings and the local file has the ability to override them.

  4. Check in a config.dev.php and config.server.php-file containing the settings for both environments, and then have an unversioned config.php which includes one of the above files. Advantage is that the configurations themselves are versioned and you can update them.

Which of these variations to pick, or whether you make another variation, depends on your environment.

Laurens Holst
  • 17,485
  • 2
  • 27
  • 33
  • What i did was to use hg remove to get rid of the configuration files in the cloned repository that is my 'live' copy. I then created the .hgignore file before recreating the config file. My understanding being that when i update, now, because of the hgignore it will not update these files? So If i understand correctly, I am essentially achieving the same thing, but do not have the benefits of versioned configurations... Is that correct? Thanks – Thomas Clowes Jan 03 '12 at 14:31
  • Re. the ignore file, sounds about right, the phrasing in your question made it seem like you expected .hgignore to work on a file that was pulled (and thus versioned), but if you remove it first it will get ignored fine. Note though that the file will probably get deleted by the pull, so you gotta restore it after you pull, and from then on it will work. – Laurens Holst Jan 03 '12 at 14:49
  • Re. the options, I think you basically did what option 1 describes, except that I would recommend to check in an example configuration to make it easier for people to configure. Option 1 has unversioned configuration (but an example), option 2 and 3 have partially versioned configurations, and option 4 has fully versioned configurations. Any is fine really, all have their up- and downsides, so it depends on what you want to do. – Laurens Holst Jan 03 '12 at 14:51
1

The basic idea for working with version control and different configuration files is always the same, but I don't know enough PHP to give a detailed answer how you can do this in PHP.
I answered a similar question for .net/Visual Studio a few months ago, so I'll just give you the link to this answer and try to describe the basic idea again, but this time language-agnostic:

For your use case, the basic idea is to have two config files in the repository, one with your local data and one with your server data, for example like this:

config.local.php
config.server.php

The "real" config.php is not in the repository, and it should be in .hgignore, so it never will be in the repository either.

After pulling, you need to run something that copies one of these files (the "correct" one depending on the current environment, local or server) to config.php.

And exactly this last part is the part that I can not answer in detail, because I don't know how to do that in PHP and/or on a web server because I'm a .net/Windows guy.
As far as I know, deploying a PHP site is just copying the files on the web server, so there is no "build/compile" step where the copying/renaming of the config file could be done (where I would do it in .net). Correct me if I'm wrong...


EDIT:

Thomas, I'm not sure if I understood your edits correctly. Your "local" repository on your laptop and your "live" repository on your webserver are basically clones of your "main" repository on Bitbucket, correct?
If yes, are you saying that you have different .hgignore files in the different clones?? That's the part that confuses me.
No matter how you actually do it in the end (there are several possibilities to deal with configuration files, see below), the .hgignore file should be the same in all clones of your repository.

So all your repositories (no matter which clone on which machine) should all contain the same configuration file(s).
Then, you only need to make sure that different configurations are used in different environments. There's already an excellent list of different ways to achieve this in Lauren Holst's answer, so I'll just point you there.
As Laurens Holst already said, we can't tell which of these ways is the best for you - it depends on your environment.

Community
  • 1
  • 1
Christian Specht
  • 33,837
  • 14
  • 123
  • 176
0

You might want to check here. If both the config file and .hgignore are commited, the .hgignore will have no effect. You could also add a domain check conditional:

$domain = $_SERVER['HTTP_HOST'];
if ($domain=="localhost") {
    //local copy config
}
else if ($domain=="yourdomain.com") {
    //webserver config
}
Community
  • 1
  • 1
CodeFuze
  • 371
  • 2
  • 6