Help with bash, 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?










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
    2 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?










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
    2 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?










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?







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






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 2 hours ago









RichardRichard

1012




1012




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





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

– G-Man
2 hours ago










1 Answer
1






active

oldest

votes


















0














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























    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%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









    0














    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




























      0














      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


























        0












        0








        0







        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 2 hours ago









        Jesse_bJesse_b

        13.1k23369




        13.1k23369






















            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%2fhelp-with-bash-git-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 rewrite equation of hyperbola in standard form

            No ethernet ip address in my vocore2