Git pre-commit hook to check for a string and exit code 1 if the string exists
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/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 $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
bash git pre-commit-hook
add a comment |
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/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 $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
bash git pre-commit-hook
add a comment |
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/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 $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
bash git pre-commit-hook
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/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 $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
bash git pre-commit-hook
bash git pre-commit-hook
edited Nov 21 '18 at 21:16
phd
20.9k52442
20.9k52442
asked Nov 21 '18 at 16:49
Opps
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression "${word}" in file: ${file}" >&2
exit 1
fi
done
done
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force apre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push.pre-receive
orupdate
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to usegit grep
or extract files from commit objects and trees to disk.
– phd
Nov 26 '18 at 20:20
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%2f53416905%2fgit-pre-commit-hook-to-check-for-a-string-and-exit-code-1-if-the-string-exists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression "${word}" in file: ${file}" >&2
exit 1
fi
done
done
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force apre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push.pre-receive
orupdate
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to usegit grep
or extract files from commit objects and trees to disk.
– phd
Nov 26 '18 at 20:20
add a comment |
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression "${word}" in file: ${file}" >&2
exit 1
fi
done
done
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force apre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push.pre-receive
orupdate
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to usegit grep
or extract files from commit objects and trees to disk.
– phd
Nov 26 '18 at 20:20
add a comment |
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression "${word}" in file: ${file}" >&2
exit 1
fi
done
done
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression "${word}" in file: ${file}" >&2
exit 1
fi
done
done
answered Nov 21 '18 at 17:22
phd
20.9k52442
20.9k52442
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force apre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push.pre-receive
orupdate
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to usegit grep
or extract files from commit objects and trees to disk.
– phd
Nov 26 '18 at 20:20
add a comment |
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force apre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push.pre-receive
orupdate
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to usegit grep
or extract files from commit objects and trees to disk.
– phd
Nov 26 '18 at 20:20
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
works great, thank you so much
– Opps
Nov 21 '18 at 20:41
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
can I add the same hook on the server side to enforce to all the developer machines?
– Opps
Nov 26 '18 at 18:30
You cannot force a
pre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push. pre-receive
or update
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to use git grep
or extract files from commit objects and trees to disk.– phd
Nov 26 '18 at 20:20
You cannot force a
pre-commit
hook to the developer hosts, but you can set up a server-side hook to stop push. pre-receive
or update
hooks, I think. Please be careful — every hook has a different set of arguments and stdin data and server-side hooks usually don't have access to files (bare repositories) so you will need to use git grep
or extract files from commit objects and trees to disk.– phd
Nov 26 '18 at 20:20
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53416905%2fgit-pre-commit-hook-to-check-for-a-string-and-exit-code-1-if-the-string-exists%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