git pre-commit script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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() 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
























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

    – G-Man
    Mar 1 at 23:32















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() 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
























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

    – G-Man
    Mar 1 at 23:32













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() 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
















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() 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















share|improve this question













share|improve this question




share|improve this question








edited Mar 2 at 7:27









Rui F Ribeiro

41.9k1483142




41.9k1483142










asked Mar 1 at 23:13









RichardRichard

1013




1013












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

    – G-Man
    Mar 1 at 23:32

















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

    – G-Man
    Mar 1 at 23:32
















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

– G-Man
Mar 1 at 23:32





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

– G-Man
Mar 1 at 23:32










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
    Mar 2 at 5:12











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
);



);













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
    Mar 2 at 5:12















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
    Mar 2 at 5:12













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 Mar 1 at 23:34









Jesse_bJesse_b

14.1k23572




14.1k23572












  • 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
    Mar 2 at 5:12

















  • 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
    Mar 2 at 5:12
















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
Mar 2 at 5:12





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
Mar 2 at 5:12

















draft saved

draft discarded
















































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

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?