我想写一个简单的预先提交钩子来检查文件是否被修改,如果是这样,压缩它并将其添加到当前索引,像这样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
我的第二个问题是,即使我删除的条件的文件从来没有真正加入到承诺,它的修改,但没有被加入进来。
感谢, 利奥
'git diff --cached'显示已经在索引中的变化。也许你只需要定期使用'git diff'呢?另外,如果您有任何其他未添加的文件,您的条件将失败。另外,它应该是'=='而不是'='。你可能真的想为你正在寻找的文件“grep”,而不是使用'=='。 – Amber
谢谢琥珀,我已更新帖子以反映您的反馈。 – leopic
@Amber:我认为OP *只会在这个文件发生变化时才会这样做。虽然有点模糊。 – Cascabel