簡體   English   中英

Git pre-commit hook用於在文件中查找文本

[英]Git pre-commit hook to find text in files

我正在編寫一個git pre-commit鈎子來檢查是否有任何暫存文件包含不允許的文本,如果是這種情況則中止。

不是這方面的專家。 到目前為止我已經有了這個

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        if [[ egrep "DISALLOWED_TEXT" ${file}]]; then
                echo "ERROR: Disallowed text in file: ${file}"
                exit 1
        fi
done

似乎沒有用。 我在提交時遇到這些錯誤:

.git/hooks/pre-commit: line 16: conditional binary operator expected
.git/hooks/pre-commit: line 16: syntax error near `"DISALLOWED_TEXT"'
.git/hooks/pre-commit: line 16: `        if [[ egrep "DISALLOWED_TEXT" ${file}]]; then'

任何建議,想法和幫助表示贊賞。 謝謝!

解決:(語法錯誤和退出調用功能不正常)

disallowed="word1 word2"

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        for word in $disallowed
        do
            if egrep $word $file ; then
                echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                exit 1
            fi
        done
done || exit $?

回答將此問題標記為有答案:

OP結束了:

disallowed="word1 word2"

git diff --cached --name-status | while read x file; do
        if [ "$x" == 'D' ]; then continue; fi
        for word in $disallowed
        do
            if egrep $word $file ; then
                echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                exit 1
            fi
        done
done || exit $?

我使用與上面相同的邏輯,即使交錯的文件包含不允許的單詞,它也會將更改提交給分支。 任何領導都將非常感激。

#!/bin/bash
import os
echo "Running pre-commit hook" 
checks=os.environ["APPSETTING_DEVPASSWORD"],os.environ["APPSETTING_DEVUSER"],os.environ["APPSETTING_DEVPASS_ELMAH"]


git diff --cached --name-status | while read x file; do

          if [ "$x" == 'D' ]; then continue; fi
        for word in $checks
        do
            if egrep $word $file ; then
                echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
                exit 1
            fi
        done
done || exit $?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM