我想寫一個簡單的預先提交鉤子來檢查文件是否被修改,如果是這樣,壓縮它並將其添加到當前索引,像這樣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