git pre-commit script












0















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?





EDIT



@Jesse_b pointed out that I should be using $() around my function calls so that the numerical comparison operators aren't sent as arguments to the function. The following example still doesn't give the desired result:



if [[ $(file_staged "package.json") -eq 1 ]]; then
echo "got here."
fi









share|improve this question









New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Possibly related: Check via shell-script if git repository’s master branch is behind origin.

    – G-Man
    8 hours ago
















0















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?





EDIT



@Jesse_b pointed out that I should be using $() around my function calls so that the numerical comparison operators aren't sent as arguments to the function. The following example still doesn't give the desired result:



if [[ $(file_staged "package.json") -eq 1 ]]; then
echo "got here."
fi









share|improve this question









New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Possibly related: Check via shell-script if git repository’s master branch is behind origin.

    – G-Man
    8 hours ago














0












0








0








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?





EDIT



@Jesse_b pointed out that I should be using $() around my function calls so that the numerical comparison operators aren't sent as arguments to the function. The following example still doesn't give the desired result:



if [[ $(file_staged "package.json") -eq 1 ]]; then
echo "got here."
fi









share|improve this question









New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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?





EDIT



@Jesse_b pointed out that I should be using $() around my function calls so that the numerical comparison operators aren't sent as arguments to the function. The following example still doesn't give the desired result:



if [[ $(file_staged "package.json") -eq 1 ]]; then
echo "got here."
fi






bash shell-script git bash-functions






share|improve this question









New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 17 mins ago









Rui F Ribeiro

40.7k1479137




40.7k1479137






New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 8 hours ago









RichardRichard

1013




1013




New contributor




Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Richard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Possibly related: Check via shell-script if git repository’s master branch is behind origin.

    – G-Man
    8 hours ago



















  • Possibly related: Check via shell-script if git repository’s master branch is behind origin.

    – G-Man
    8 hours ago

















Possibly related: Check via shell-script if git repository’s master branch is behind origin.

– G-Man
8 hours ago





Possibly related: Check via shell-script if git repository’s master branch is behind origin.

– G-Man
8 hours ago










1 Answer
1






active

oldest

votes


















1














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





share|improve this answer
























  • Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

    – Richard
    2 hours ago











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.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503889%2fgit-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









1














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





share|improve this answer
























  • Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

    – Richard
    2 hours ago
















1














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





share|improve this answer
























  • Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

    – Richard
    2 hours ago














1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









Jesse_bJesse_b

13.1k23369




13.1k23369













  • Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

    – Richard
    2 hours ago



















  • Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

    – Richard
    2 hours ago

















Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

– Richard
2 hours ago





Ah, I see. You're right. By enclosing the function call in $(), I'm preventing the -eq from being sent as arguments to the function. However, I'm encountering another problem. The file_staged() function isn't working as expected. When I run the command from the command line, I get the expected results, but not in my script. I'll update my question accordingly.

– Richard
2 hours ago










Richard is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503889%2fgit-pre-commit-script%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Loup dans la culture

How to solve the problem of ntp “Unable to contact time server” from KDE?

ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux