[英]git pre-commit hook, add file into index
我正在嘗試編寫一個簡單的預提交鈎子來檢查文件是否被修改,如果是這樣,壓縮它並將其添加到當前索引中,就像這樣
#!/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
而我的第二個問題是,即使我刪除了條件,文件也從未實際添加到提交中,它已被修改,但從未添加。
謝謝,獅子座
你的錯誤是因為你沒有引用$mf
。 將其更改為"$mf"
。 雖然可能有更好的方法而不是貪圖人類可讀命令的輸出......你可以看一下git status --porcelain
例如git status --porcelain
。 甚至git diff --cached <path>
,然后檢查退出代碼,例如:
if ! git diff --quiet --cached <path>; then
# the file was modified; do stuff
fi
我認為Amber可能會誤導您:您應該使用--cached
,因為如果未進行更改,那么就此提交而言,沒有任何更改,因此我想您不想做任何其他事情。
當然,我不知道您的項目,但是我不確定為什么您要執行這樣的操作-通常您不想檢入機器生成的內容,只是想輕松地從中重建內容即可入住。
至於你的上一個問題,正在修改但未添加到提交的文件,我無法用玩具示例重現它。 我將其作為預提交掛鈎:
#!/bin/bash
touch z
git add z
並進行了提交,並按預期方式創建,添加和提交了z。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.