6

Searching for the best approach to keep my config files separate, yet not introduce extra steps for new developers setting up their environments.

I am guessing a submodule would suffice to do the job, but then how would I switch configs seamlessly depending on the task at hand, aka pull in DEV config regularly, pull PROD branch of config repo during build?

Needs to be:

  • Easy and painless for new devs.
  • PROD config files should only be accessible to select users + build user.

Thank you in advance.

Dmitri Farkov
  • 9,133
  • 1
  • 29
  • 45
  • I just edited the answer to add a way to detect the branch in which a script is executing itself. – VonC Dec 29 '13 at 07:18

1 Answers1

11

That is called content filter driver, and it allows you to declare, in a .gitattributes file (and only for your config files type) a smudge script which will automatically on checkout:

  • combine a config file template file (config.tpl)
  • with the right config file value (config.dev, config.prod, ...)
  • in order to produced a non-versioned config file (private file)

Smudge filter

See "Customizing Git - Git Attributes":

echo '*.cfg.tpl filter=config' >> .gitattributes
git config --global filter.config.smudge yourScript

With that approach, you don't need submodules, but you can generate as many config file you need depending on your environment, like for instance your branch:
A bit like in "Find Git branch name in post-update hook", your smudge script can find out in which branch it is currently executing with:

#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you. Although I ended up going the route of storing configs separately and puppetizing my setup, this is still great info and I never knew about this feature before. – Dmitri Farkov Jan 14 '14 at 16:49
  • Is there no need for the argument name `filter=` at `echo '*.cfg.tpl config'`, i.e., making it `echo '*.cfg.tpl filter=config' >> .gitattributes`. Or is `filter` the default argument Git looks for in config if no argument name is provided in `.giattributes`? – ToJo May 05 '20 at 12:10
  • 1
    @ToJo Good point, thank you. I must have missed it 6+ years ago. I have edited the answer accordingly. – VonC May 05 '20 at 15:05