git钩子 - 重新生成一个文件并将其添加到每个提交? [英] git hooks - regenerate a file and add it to each commit?
问题描述
我想自动生成一个文件并将其添加到提交中(如果它已更改)。是否有可能,如果是这样,我应该使用什么钩子?
上下文:我正在编写一个CSS库。它有几个CSS文件,最后我想生成一个压缩和最小化版本。现在我的工作流程是:
- 修改css文件
x.css
和y.css
-
git add x.css y.css
- 执行
minimize.sh
它解析我的lib中的所有css文件,最小化它们并产生一个min.css
file -
git add min.css
-
git commit -m'修改x和y做foo和bar'
我想通过git钩子自动完成步骤3和4。这可能吗?
我从来没有使用过git钩子。阅读手册页之后,我想我需要使用 pre-提交
钩子。但是,我可以调用 git add min.css
,或者我会打断互联网吗?
以下是我的.git / hooks / pre-commit文件的代码:
#!/ bin / sh
exec minimize.sh
exec git add oocss.min.css
文档没有提到我必须将其设置为可执行文件,否则将无法执行。
如果您对如何最小化感兴趣,我使用榨汁机 - 最小化命令是:
exec juicer merge --force my-uncompressed-file.css
不,它不会破坏任何东西。您可以在 pre-commit
钩子中自由使用 git add
。
I'd like to automatically generate a file and add it to a commit if it has changed. Is it possible, if so, what hooks should I use?
Context: I'm programming a CSS library. It has several CSS files, and at the end I want to produce a compacted and minimized version. Right now my workflow is:
- Modify the css files
x.css
andy.css
git add x.css y.css
- Execute
minimize.sh
which parses all the css files on my lib, minimizes them and produces amin.css
file git add min.css
git commit -m 'modified x and y doing foo and bar'
I would like to have steps 3 and 4 done automatically via a git hook. Is that possible?
I've never used git hooks before. After reading the man page, I think I need to use the pre-commit
hook. But can I invoke git add min.css
, or will I break the internet?
EDIT: It worked! And I didn't create a black hole or anything!
Here's the code of my .git/hooks/pre-commit file:
#!/bin/sh
exec minimize.sh
exec git add oocss.min.css
The documentation didn't mention that I had to make it executable, or it would not work.
In case you are interested in how did I minimize, I used juicer - the minimizing command is:
exec juicer merge --force my-uncompressed-file.css
No, it won’t break anything. You can freely use git add
in a pre-commit
hook.
这篇关于git钩子 - 重新生成一个文件并将其添加到每个提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!