简体   繁体   English

使用 git 维护分支特定的配置文件

[英]Branch-specific configuration file maintenance with git

Here is a question keeps me puzzled for a long time as I cannot find nice and more-or-less universal solution to it.这是一个让我困惑了很长时间的问题,因为我找不到好的和或多或少通用的解决方案。

Lets say there are two git branches available, production and dev ;假设有两个 git 分支可用, productiondev each uses it's own configurable parameters for some tasks (ie credentials, build path, test/deployment scripts switches et cetera).每个任务都使用自己的可配置参数来完成某些任务(即凭据、构建路径、测试/部署脚本切换等)。 Same time implementation scripts & code is common for both branches.同时实现脚本和代码对于两个分支都是通用的。

Now, the problem arises is - how to maintain branch-specific configuration within git repository the way required.现在,问题出现了 - 如何以所需的方式在 git 存储库中维护特定于分支的配置。 One of common solutions out there is to use 'template' configuration file within git, symlinking and ignoring the concrete for specific branch, like一种常见的解决方案是在 git 中使用“模板”配置文件,符号链接并忽略特定分支的具体内容,例如

$ cat .gitignore | grep conf
/concrete.conf
$ ls -l *.conf
lrwxrwxrwx 1 1000 100 12 Oct 29 10:23 concrete.conf -> generic.conf
-rw-r--r-- 1 1000 100  0 Oct 29 10:16 generic.conf

breaking and adjusting concrete.conf on development box next.接下来在开发框中打破和调整concrete.conf Yet not a solution I'm in pursuit for.然而,这不是我追求的解决方案。

My requirements (ok, wishes ) are:我的要求(好的,希望)是:

  • support separate configuration file per git branch, and支持每个 git 分支的单独配置文件,以及
  • keep branch-specific configuration file managed by git same time同时保持由 git 管理的特定于分支的配置文件

Is it even possible?甚至可能吗? Could be (actually, preferred to be file sourced by some other one, but it's none related...)可能是(实际上,更喜欢由其他人提供文件,但它不相关......)

The best that has came to my mind so far is to use post-merge hooks to adjust per-branch configuration per se from some other source ignored by git, but it stinks from the very beginning.到目前为止,我想到的最好的方法是使用合并后挂钩从 git 忽略的其他来源调整每个分支配置本身,但它从一开始就很臭。

Any suggestions on solution of problem described?关于所描述的问题的解决方案有什么建议吗?

PS: *nix-specific suggestions (ie using symlinks/hardlinks) suggestions is absolutely fine, I do do have any interest in M$ targets PS:*nix 特定的建议(即使用符号链接/硬链接)建议绝对没问题,我确实对 M$ 目标感兴趣

It is possible, and does not involve symlinking.这是可能的,并且不涉及符号链接。

You do not version my.config , but a template file my.config.tpl , and a value file (with values for each branches)没有版本my.config ,而是一个模板文件my.config.tpl和一个值文件(每个分支都有值)

 prod.conf
 dev.conf

Then, you can use a content filter driver , using .gitattributes declaration .然后,您可以使用内容过滤器驱动程序,使用.gitattributes声明

弄脏 (image from " Customizing Git - Git Attributes ", from " Pro Git book ") (图片来自“ 自定义 Git - Git 属性”,来自“ Pro Git 书”)

The script generate my.config file by replacing placeholder values with the values of the <branch.conf> file, each time you checkout a branch.每次签出分支时,该脚本通过将占位符值替换为<branch.conf>文件的值来生成my.config文件。
You can know about the current branch name with:您可以通过以下方式了解当前分支名称:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

The generated actual my.config remains ignored (by the .gitignore ).生成的实际my.config仍然被忽略(由.gitignore )。
That means your actual working tree does not get "dirty".这意味着您的实际工作树不会“变脏”。

The smudge script selects the correct value file and generates the correct web.config based on the template the smudge script is applied on during a git checkout .涂抹脚本选择正确的值文件并根据在git checkout期间应用涂抹脚本的模板生成正确的web.config

See a complete example at " git smudge/clean filter between branches ".请参阅“ git 涂抹/清洁分支之间的过滤器”中的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM