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 $?
git bash shell unix git-bash
|
show 2 more comments
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 $?
git bash shell unix git-bash
4
You missed a space between${file}
and]]
and you don't actually want[[
or]]
around theegrep
call if you want to use the return status ofegrep
as your test in theif
statement.
– Etan Reisner
Sep 2 '15 at 14:58
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
You are seeing your error output and theexit
is working but the commit isn't failing? It looks like that should be working (and the|| exit $?
probably isn't necessary).
– Etan Reisner
Sep 3 '15 at 12:48
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06
|
show 2 more comments
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 $?
git bash shell unix git-bash
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 $?
git bash shell unix git-bash
git bash shell unix git-bash
edited Sep 3 '15 at 9:27
Saras101
asked Sep 2 '15 at 14:52
Saras101Saras101
484
484
4
You missed a space between${file}
and]]
and you don't actually want[[
or]]
around theegrep
call if you want to use the return status ofegrep
as your test in theif
statement.
– Etan Reisner
Sep 2 '15 at 14:58
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
You are seeing your error output and theexit
is working but the commit isn't failing? It looks like that should be working (and the|| exit $?
probably isn't necessary).
– Etan Reisner
Sep 3 '15 at 12:48
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06
|
show 2 more comments
4
You missed a space between${file}
and]]
and you don't actually want[[
or]]
around theegrep
call if you want to use the return status ofegrep
as your test in theif
statement.
– Etan Reisner
Sep 2 '15 at 14:58
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
You are seeing your error output and theexit
is working but the commit isn't failing? It looks like that should be working (and the|| exit $?
probably isn't necessary).
– Etan Reisner
Sep 3 '15 at 12:48
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06
4
4
You missed a space between
${file}
and ]]
and you don't actually want [[
or ]]
around the egrep
call if you want to use the return status of egrep
as your test in the if
statement.– Etan Reisner
Sep 2 '15 at 14:58
You missed a space between
${file}
and ]]
and you don't actually want [[
or ]]
around the egrep
call if you want to use the return status of egrep
as your test in the if
statement.– Etan Reisner
Sep 2 '15 at 14:58
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
You are seeing your error output and the
exit
is working but the commit isn't failing? It looks like that should be working (and the || exit $?
probably isn't necessary).– Etan Reisner
Sep 3 '15 at 12:48
You are seeing your error output and the
exit
is working but the commit isn't failing? It looks like that should be working (and the || exit $?
probably isn't necessary).– Etan Reisner
Sep 3 '15 at 12:48
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:
printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:
printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06
|
show 2 more comments
2 Answers
2
active
oldest
votes
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 $?
add a comment |
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 $?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32356457%2fgit-pre-commit-hook-to-find-text-in-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 $?
add a comment |
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 $?
add a comment |
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 $?
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 $?
answered Apr 25 '17 at 3:54
community wiki
John Weldon
add a comment |
add a comment |
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 $?
add a comment |
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 $?
add a comment |
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 $?
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 $?
answered Nov 20 '18 at 22:14
OppsOpps
63
63
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32356457%2fgit-pre-commit-hook-to-find-text-in-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
You missed a space between
${file}
and]]
and you don't actually want[[
or]]
around theegrep
call if you want to use the return status ofegrep
as your test in theif
statement.– Etan Reisner
Sep 2 '15 at 14:58
Thank you for noticing. Other issue now is that commit process is not aborted after the error is found.
– Saras101
Sep 3 '15 at 6:25
You are seeing your error output and the
exit
is working but the commit isn't failing? It looks like that should be working (and the|| exit $?
probably isn't necessary).– Etan Reisner
Sep 3 '15 at 12:48
AFAIU, first exit is called from a subshell because its in a loop. Therefore, second exit has to be called to abort with the latest returned value, using $? directive.
– Saras101
Sep 24 '15 at 6:54
The while is running in a sub-shell but it is the right-hand side of the pipe so when it exits there's nothing left running. Try it:
printf %s\n a b c | while IFS= read -r line; do echo "$line"; exit 5; done; echo $?
– Etan Reisner
Sep 24 '15 at 13:06