Add ignored config file to git repo as sample

后端 未结 4 1448
-上瘾入骨i
-上瘾入骨i 2021-02-03 11:56

I have an repository for an app that I\'m working on that includes a configuration file. Currently, I distribute with a .dist extension, and have the user rename th

相关标签:
4条回答
  • 2021-02-03 12:27

    As others have pointed out, there is no magical solution for this in Git. And as far as I know, it's the same with other version control systems.

    Hooks are also of no solution here, if they would work this way, that would be a serious security threat - just by cloning a repo, an evil code could execute in your machine.

    But to use your app, the user has to do at least one more thing besides cloning it with git: running your app.

    • If modifying the config file is not required for running the application, then if your program does if [ ! -e config ] ; then cp config.dist config ; fi on startup, then that should be IMHO quite enough. Additionally you should have warning inside config.dist which says you shouldn't modify this file, but make a copy of it instead.

    • If the modification of config file is required, then starting the program without config file could start up some installation mode, that asks the user to enter the needed configuration options and saves them.

    • A third possibility is that your app is unable to modify the config file when it starts up (maybe it's just some JavaScript + HTML files). In that case I see no other option than to have some separate build script or let the user just manually do it.

    0 讨论(0)
  • 2021-02-03 12:37

    This is a good question, but AFAIK there is no git solution for this.

    Few workarounds:

    • configure your app to read config.dist when config is not present (e.g. PHPUnit uses this technique)
    • use build script to do rename for you (e.g. ant or phing)
    • use git hooks to rename the configs
    0 讨论(0)
  • 2021-02-03 12:41

    You can also version and distribute:

    • a .gitattributes files declaring a filter driver (for only 'config.dist')
    • a smudge script which will detect that content and generate the final (and private, as in "non-versioned" config file)

    Now that is bending a bit what a filter driver is about (it is about modifying the content of a file, regardless of its "state", i.e its full path and name). But it can work.
    The .gitattributes would contain:

    config.dist  filter=generateConfig
    

    alt text

    And you can register your smudge script (applied during the checkout step when creating/updating the working directory) like:

    git config --global filter.generateConfig.smudge /path/to/generateConfig
    
    0 讨论(0)
  • 2021-02-03 12:44

    I'd probably include an 'install' script of some sort that copied 'config.dist' to 'config'.

    I don't think there's a way to have a file both not-ignored and ignored.

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