[英]Git pre-commit hook not working as expected
我已將我的存儲庫配置為使用hooks
目錄而不是.git/hooks
目錄,以便可以從該存儲庫中對其進行管理
我想運行sed
在提交發生之前修改密碼。 我在我的hooks/pre-commit
腳本中使用了此代碼,該腳本也已使其可執行。
#!/bin/bash
FNAME=smbclient.conf
sed -i -e 's/password=.*/password=See_Thycotic/g' ${FNAME}
grep -c See_Thycotic ${FNAME}
if [ "$?" -ne "0" ] ; then
echo Failed to redact password in ${FNAME}
exit 1
fi
echo Password was redacted in ${FNAME} before commit
當我運行此命令時:
git commit smbclient.conf -m "changed something"
我看到此消息(按預期):
1
Password was redacted in smbclient.conf before commit
問題在於,在通過pre-commit
腳本更改內容之前已提交文件。 如果然后運行git status
,它會告訴我已modified: smbclient.conf
。
1)如何在提交之前更改此文件,然后又提交呢?
2)僅提交smbclient.conf文件而不提交其他文件時,是否可以運行預提交腳本?
1)如果$FNAME
文件由sed
更新,則應讓pre-commit
掛鈎執行git add $FNAME
。
2)否。無法定義僅對特定文件執行的預提交掛鈎。
做到這一點的正確方法可能是讓腳本在每次提交時都運行,但是讓它從以下方面開始:
if [[ "$(git diff --name-only --staged -- $FNAME)" == "" ]] #If $FNAME file is not updated in this commit
then
exit 0 #Stop execution of this hook, and consider hook execution successful
fi
#Rest of pre-commit hook script here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.