Help with bash, git pre-commit script
The goal is to exit from the script with a non-zero exit code when committing package-lock.json
with no associated changes to package.json
being committed.
#!/bin/bash
# exits with 1 if there were differences and 0 means no differences
file_changed() {
git diff --quiet --exit-code "$1"
}
# exits with 1 if no lines were selected, 0 if one or more lines selected, > 1 if error
file_staged() {
git diff --name-only --cached | grep -q "$1"
}
# package-lock.json has changed and
# package-lock.json in staged files and
# package.json not in staged files?
if file_changed "package-lock.json" -eq 1 &&
file_staged "package-lock.json" -eq 0 &&
file_staged "package.json" -eq 1
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
I'm fairly certain the problem lies in my files_staged
function. When testing file_staged "package-lock.json" -eq 0
, I get the expected results. When testing file_staged "package.json" -eq 1
, it always fails.
Simplifying the problem, I can never get this condition to trigger when package.json
is not in the list of files returned by git diff --name-only --cached
:
if file_staged "package.json" -eq 1; then
echo "got here."
fi
Where am I going wrong?
bash shell-script git bash-functions
New contributor
add a comment |
The goal is to exit from the script with a non-zero exit code when committing package-lock.json
with no associated changes to package.json
being committed.
#!/bin/bash
# exits with 1 if there were differences and 0 means no differences
file_changed() {
git diff --quiet --exit-code "$1"
}
# exits with 1 if no lines were selected, 0 if one or more lines selected, > 1 if error
file_staged() {
git diff --name-only --cached | grep -q "$1"
}
# package-lock.json has changed and
# package-lock.json in staged files and
# package.json not in staged files?
if file_changed "package-lock.json" -eq 1 &&
file_staged "package-lock.json" -eq 0 &&
file_staged "package.json" -eq 1
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
I'm fairly certain the problem lies in my files_staged
function. When testing file_staged "package-lock.json" -eq 0
, I get the expected results. When testing file_staged "package.json" -eq 1
, it always fails.
Simplifying the problem, I can never get this condition to trigger when package.json
is not in the list of files returned by git diff --name-only --cached
:
if file_staged "package.json" -eq 1; then
echo "got here."
fi
Where am I going wrong?
bash shell-script git bash-functions
New contributor
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago
add a comment |
The goal is to exit from the script with a non-zero exit code when committing package-lock.json
with no associated changes to package.json
being committed.
#!/bin/bash
# exits with 1 if there were differences and 0 means no differences
file_changed() {
git diff --quiet --exit-code "$1"
}
# exits with 1 if no lines were selected, 0 if one or more lines selected, > 1 if error
file_staged() {
git diff --name-only --cached | grep -q "$1"
}
# package-lock.json has changed and
# package-lock.json in staged files and
# package.json not in staged files?
if file_changed "package-lock.json" -eq 1 &&
file_staged "package-lock.json" -eq 0 &&
file_staged "package.json" -eq 1
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
I'm fairly certain the problem lies in my files_staged
function. When testing file_staged "package-lock.json" -eq 0
, I get the expected results. When testing file_staged "package.json" -eq 1
, it always fails.
Simplifying the problem, I can never get this condition to trigger when package.json
is not in the list of files returned by git diff --name-only --cached
:
if file_staged "package.json" -eq 1; then
echo "got here."
fi
Where am I going wrong?
bash shell-script git bash-functions
New contributor
The goal is to exit from the script with a non-zero exit code when committing package-lock.json
with no associated changes to package.json
being committed.
#!/bin/bash
# exits with 1 if there were differences and 0 means no differences
file_changed() {
git diff --quiet --exit-code "$1"
}
# exits with 1 if no lines were selected, 0 if one or more lines selected, > 1 if error
file_staged() {
git diff --name-only --cached | grep -q "$1"
}
# package-lock.json has changed and
# package-lock.json in staged files and
# package.json not in staged files?
if file_changed "package-lock.json" -eq 1 &&
file_staged "package-lock.json" -eq 0 &&
file_staged "package.json" -eq 1
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
I'm fairly certain the problem lies in my files_staged
function. When testing file_staged "package-lock.json" -eq 0
, I get the expected results. When testing file_staged "package.json" -eq 1
, it always fails.
Simplifying the problem, I can never get this condition to trigger when package.json
is not in the list of files returned by git diff --name-only --cached
:
if file_staged "package.json" -eq 1; then
echo "got here."
fi
Where am I going wrong?
bash shell-script git bash-functions
bash shell-script git bash-functions
New contributor
New contributor
New contributor
asked 2 hours ago
RichardRichard
1012
1012
New contributor
New contributor
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago
add a comment |
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
None of the conditions in your if
construct work. Since you aren't using a test command (test
, [
, [[
) you are simply testing the return status of your function.
Example:
$ test () { echo 0; }
$ if test -eq 1; then echo yes; fi
0
yes
$ if test -eq 10; then echo yes; fi
0
yes
$ if test -eq 100000000000; then echo yes; fi
0
yes
The -eq ...
is being treated as an option to the test
function, and that function is returning 0 so it's being treated as a success.
You want to use a test command:
if [[ $(file_changed "package-lock.json") -eq 1 &&
$(file_staged "package-lock.json") -eq 0 &&
$(file_staged "package.json") -eq 1 ]]
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Richard is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f503889%2fhelp-with-bash-git-pre-commit-script%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
None of the conditions in your if
construct work. Since you aren't using a test command (test
, [
, [[
) you are simply testing the return status of your function.
Example:
$ test () { echo 0; }
$ if test -eq 1; then echo yes; fi
0
yes
$ if test -eq 10; then echo yes; fi
0
yes
$ if test -eq 100000000000; then echo yes; fi
0
yes
The -eq ...
is being treated as an option to the test
function, and that function is returning 0 so it's being treated as a success.
You want to use a test command:
if [[ $(file_changed "package-lock.json") -eq 1 &&
$(file_staged "package-lock.json") -eq 0 &&
$(file_staged "package.json") -eq 1 ]]
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
add a comment |
None of the conditions in your if
construct work. Since you aren't using a test command (test
, [
, [[
) you are simply testing the return status of your function.
Example:
$ test () { echo 0; }
$ if test -eq 1; then echo yes; fi
0
yes
$ if test -eq 10; then echo yes; fi
0
yes
$ if test -eq 100000000000; then echo yes; fi
0
yes
The -eq ...
is being treated as an option to the test
function, and that function is returning 0 so it's being treated as a success.
You want to use a test command:
if [[ $(file_changed "package-lock.json") -eq 1 &&
$(file_staged "package-lock.json") -eq 0 &&
$(file_staged "package.json") -eq 1 ]]
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
add a comment |
None of the conditions in your if
construct work. Since you aren't using a test command (test
, [
, [[
) you are simply testing the return status of your function.
Example:
$ test () { echo 0; }
$ if test -eq 1; then echo yes; fi
0
yes
$ if test -eq 10; then echo yes; fi
0
yes
$ if test -eq 100000000000; then echo yes; fi
0
yes
The -eq ...
is being treated as an option to the test
function, and that function is returning 0 so it's being treated as a success.
You want to use a test command:
if [[ $(file_changed "package-lock.json") -eq 1 &&
$(file_staged "package-lock.json") -eq 0 &&
$(file_staged "package.json") -eq 1 ]]
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
None of the conditions in your if
construct work. Since you aren't using a test command (test
, [
, [[
) you are simply testing the return status of your function.
Example:
$ test () { echo 0; }
$ if test -eq 1; then echo yes; fi
0
yes
$ if test -eq 10; then echo yes; fi
0
yes
$ if test -eq 100000000000; then echo yes; fi
0
yes
The -eq ...
is being treated as an option to the test
function, and that function is returning 0 so it's being treated as a success.
You want to use a test command:
if [[ $(file_changed "package-lock.json") -eq 1 &&
$(file_staged "package-lock.json") -eq 0 &&
$(file_staged "package.json") -eq 1 ]]
then
echo "attempted commit of package-lock.json without changes to package.json!"
exit 1
fi
answered 2 hours ago
Jesse_bJesse_b
13.1k23369
13.1k23369
add a comment |
add a comment |
Richard is a new contributor. Be nice, and check out our Code of Conduct.
Richard is a new contributor. Be nice, and check out our Code of Conduct.
Richard is a new contributor. Be nice, and check out our Code of Conduct.
Richard is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f503889%2fhelp-with-bash-git-pre-commit-script%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
Possibly related: Check via shell-script if git repository’s master branch is behind origin.
– G-Man
2 hours ago