Branch-specific configuration file maintenance wit

2020-03-06 03:37发布

问题:

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; each uses it's own configurable parameters for some tasks (i.e. 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. One of common solutions out there is to use 'template' configuration file within git, symlinking and ignoring the concrete for specific branch, like

$ 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. Yet not a solution I'm in pursuit for.

My requirements (ok, wishes) are:

  • support separate configuration file per git branch, and
  • keep branch-specific configuration file managed by git same time

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.

Any suggestions on solution of problem described ?

PS: *nix-specific suggestions (i.e. using symlinks/hardlinks) suggestions is absolutely fine, I do do have any interest in M$ targets

回答1:

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)

 prod.conf
 dev.conf

Then, you can use a content filter driver, using .gitattributes declaration.

(image from "Customizing Git - Git Attributes", from "Pro Git book")

The script generate my.config file by replacing placeholder values with the values of the <branch.conf> file, each time you checkout a branch.
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).
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.

See a complete example at "git smudge/clean filter between branches".