Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pre-commit hook to find text in files

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've got this

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

Doesn't seem to work. I'm getting these errors while commiting:

.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'

Any suggestions, ideas and help appreciated. Thanks!

SOLVED: (syntax errors and dysfunctional exit call)

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 $?
like image 347
Saras101 Avatar asked Sep 02 '15 14:09

Saras101


People also ask

How do you run pre-commit on a file?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.

How do you use a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

Where do pre-commit hooks go?

All git hooks are stored in the . git/hooks/ directory under your project root. A pre-commit hook is just an executable file stored in this folder with the magic name pre-commit .

What is git pre hook?

The pre-rebase hook is called before git rebase changes anything, making it a good place to make sure something terrible isn't about to happen. This hook takes 2 parameters: the upstream branch that the series was forked from, and the branch being rebased. The second parameter is empty when rebasing the current branch.


2 Answers

Answer to mark this question as having an answer:

OP ended up with:

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 $?
like image 176
John Weldon Avatar answered Oct 01 '22 08:10

John Weldon


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 $?
like image 29
Opps Avatar answered Oct 01 '22 09:10

Opps