git pre-commit hook,将文件添加到索引中
我正在尝试编写一个简单的预提交挂钩来检查文件是否被修改,如果是,则压缩它并将其添加到当前索引中,像这样
#!/bin/sh
# was the file modified?
mf='git status | grep jquery.detectBrowser.js'
# is the non-compressed file in the staging area?
if [ $mf != "" ]
then
# alert the user
echo "Javascript file modified, YUI Compressor will begin now."
# go to rhino
cd $HOME/apps/rhino/yuicompressor-2.4.7/build
# compress my file
java -jar yuicompressor-2.4.7.jar ~/www/jquery.detectBrowser.js/jquery.detectBrowser.js -o ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
# comeback to the initial directory
cd -
# add the new file into the index
git add ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
fi
我有2个问题,1我的条件失败,每次,我一定有错字或类似的东西,但我不明白它是什么?这是我得到的错误:
[: 23: git: unexpected operator
我的第二个问题是,即使我删除条件,文件也从未真正添加到提交中,它被修改了,但从未添加。
谢谢, 狮子座
I'm trying to write a simple pre-commit hook to check if a file was modified, if so, compress it and add it into the current index, something like this
#!/bin/sh
# was the file modified?
mf='git status | grep jquery.detectBrowser.js'
# is the non-compressed file in the staging area?
if [ $mf != "" ]
then
# alert the user
echo "Javascript file modified, YUI Compressor will begin now."
# go to rhino
cd $HOME/apps/rhino/yuicompressor-2.4.7/build
# compress my file
java -jar yuicompressor-2.4.7.jar ~/www/jquery.detectBrowser.js/jquery.detectBrowser.js -o ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
# comeback to the initial directory
cd -
# add the new file into the index
git add ~/www/jquery.detectBrowser.js/jquery.detectBrowser.min.js
fi
I have 2 issues, 1 my condition is failing, every time, I must have a typo or something like that, but I can't figure out what it is? This is the error I get back:
[: 23: git: unexpected operator
And my second problem is that even if I remove the condition the file is never actually ADDED into the commit, it's modified, but never ADDED.
Thanks,
Leo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
您的错误是因为您没有引用,只需检查退出代码,例如:
$mf
。将其更改为“$mf”
。尽管也许有比 grep 人类可读命令的输出更好的方法......例如,您可以查看 git status --porcelain 。或者甚至是 git diff --cached我认为 Amber 可能误导了您:您应该使用
- -cached
,因为如果更改没有暂存,那么就本次提交而言,没有任何更改,所以我假设您不想做任何其他事情。当然,我不知道你的项目,但我不确定你为什么要做这样的事情 - 通常你不想签入机器生成的内容,只是让你可以轻松地从现有内容重建签入。
至于您的最后一个问题,文件被修改但未添加到提交中,我无法用玩具示例重现它。我将其作为预提交挂钩:
并进行了提交,并且 z 按预期创建、添加和提交。
Your error is because you're not quoting
$mf
. Change it to"$mf"
. Though there are perhaps better ways than grepping the output of a human-readable command... you could have a look atgit status --porcelain
for example. Or evengit diff --cached <path>
, and just examine the exit code, e.g.:I think Amber may have misled you: you should use
--cached
, because if the changes are not staged, then as far as this commit is concerned, there are no changes, so I assume you don't want to do anything else.And of course, I don't know your project, but I'm not sure why you're doing something like this - usually you don't want to check in machine-generated content, just make it easy to rebuild from what is checked in.
As for your last problem, the file being modified but not added to the commit, I can't reproduce it with a toy example. I made this as a pre-commit hook:
and made a commit, and z was as expected created, added, and committed.