GIT (HUSKY): exit code when a file is being committed?

假装没事ソ 提交于 2020-01-03 05:07:09

问题


is there a way to check if a file is being committed and exit with an error ?

I have a file in git that needs to be there but should never be modified and i was hoping to use husky as a pre-commit - so if anybody tries to modify the file and commit then it would throw an error.

If in future I need to modify the file then i can just disable the pre-commit.

the file is a configuration that i need to edit a lot when developing but the changes should never be committed.

I was hoping to use husky as a check to ensure that i dont.


回答1:


I would rather manage sure a file with a content filter driver, using .gitattributes declaration.

That means you do not version the actual configuration file, but only a template file, and a file with all possible values per environment.

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

The generated actual file remains ignored (by the .gitignore): your actual working tree does not get "dirty".

The smudge script:

  • detect the right environment (not the branch, since only one is needed)
  • selects the correct value file and generates the correct file based on the template the smudge script is applied on during a git checkout.

That way, you modify the config.dev value file as much as you want when developing: the config file (not versioned) will be generated from those values.
But in production, a checkout of that same repo will trigger the generation of a prod config file, using the config.prod (versioned file) values.



来源:https://stackoverflow.com/questions/54140322/git-husky-exit-code-when-a-file-is-being-committed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!