Git pre-commit hook to find text in files

后端 未结 2 988
暖寄归人
暖寄归人 2021-01-02 23:24

I\'m writing a git pre-commit hook to check if any of the staged files are containing disallowed text and abort if that\'s the case.

Not an expert at this. So far I\

2条回答
  •  -上瘾入骨i
    2021-01-02 23:43

    I am using the same logic as above and even though the stagged file contains the disallowed word, it commits the changes to the branch. Any lead would be greatly appreciated.

    #!/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 $?
    

提交回复
热议问题