Modify file before commit with pre-commit hook

感情迁移 提交于 2019-12-22 04:57:19

问题


I am trying to write a pre-commit hook that modify a line in my code but I do not know even from where to start.

The problem is:

I have a KEY

public static final String APP_KEY = ""; //DELETE THE KEY BEFORE COMMIT!!!

In order to avoid publishing the KEY to the repository I've think maybe git hooks are the thing we need instead of delete the key manually. I've take a look at Customizing git hooks but I do not know how to write the hook.

Is there a way to before commit the changes, delete the KEy and after the commit write the key again?


回答1:


That would be done with a content filter driver:

  • a clean script which would remove the key on checkin
  • a smudge script which would add it back on checkout.

(image from "Customizing Git Attributes" from the Git Book)

See an example of how those filters are declared in "Can git automatically switch between spaces and tabs?"



来源:https://stackoverflow.com/questions/25217347/modify-file-before-commit-with-pre-commit-hook

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