How to handle different instances of index.php in codeigniter with git

后端 未结 1 1713
暗喜
暗喜 2020-12-18 16:40

I have a git repository that is using codeigniter as a framework. The main index.php differs from development to the production as it sets an environment variable (either \"

相关标签:
1条回答
  • 2020-12-18 17:41

    You could just leave the file modified and never commit it, but that is very annoying. And there are complicated methods out there making use of multiple branches and rebasing. But that gets very messy, too.

    The easiest solution I found to this is to use a smudge filter on your production environment. For details see the git book. You can use a smudge filter to replace a certain word with something else on checkout.

    On my setup I use this filter (you can just add this to the .git/config file on your server):

    [filter "htproduction"]
        smudge = sed 's/\\$site-mode\\$/$site-mode:production$/'
        clean = sed 's/\\$site-mode.*\\$/$site-mode$/'
    

    To activate this filter, add this line to .git/info/attributes (create that file if it doesn’t exist):

    index.php filter=htproduction
    

    The effect of this is, that all occurrences of $site-mode$ are replaced with $site-mode:production$ on your server. Now do something like this in your index.php file:

    if (strpos("$site-mode$", "production") !== false)){
        do_only_on_server();
    }
    

    You are strongly recommended to read up on smudge filters, to understand what’s going on and why I’m not doing if ("$site-mode$" == "$site-mode:production$") (which wouldn’t work).

    Note: This answer assumes you are running linux on your server. This method works on windows too, but sed will probably not be available.

    0 讨论(0)
提交回复
热议问题