Skip to content

Instantly share code, notes, and snippets.

@jredh
Forked from cvogt/pre-commit-hook-install.sh
Last active November 6, 2018 21:38
Show Gist options
  • Save jredh/e1af2f274889d9ec780dc86603150e6e to your computer and use it in GitHub Desktop.
Save jredh/e1af2f274889d9ec780dc86603150e6e to your computer and use it in GitHub Desktop.

Revisions

  1. jredh revised this gist Nov 6, 2018. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions pre-push-hook.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    #!/bin/sh

    # checks if locally staged changes pass tests.
    echo "starting pre-push hook"

    _DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' )

    echo ""
    echo "Running pre-commit hook ... (you can omit this with --no-verify, but don't)"
    git diff --quiet
    hadNoNonStagedChanges=$?
    @@ -16,8 +15,19 @@ then
    git stash --keep-index -u > /dev/null
    fi


    [ ! -f $DIR/build.sbt ]
    sbtProject=$?

    if [ ! sbtProject -eq 0 ]
    then
    echo "* Not an SBT Project."
    exit 0
    fi

    echo "* This is an SBT project."
    echo "* Running Tests."
    $DIR/sbt ";clean;test" > /dev/null
    sbt ";clean;test" > /dev/null
    passes=$?

    echo "* Compiles?"
  2. Jared Hooper revised this gist Oct 29, 2018. No changes.
  3. Jared Hooper revised this gist Oct 29, 2018. 3 changed files with 5 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions pre-commit-hook-install.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    #!/bin/sh
    cd "$(dirname "$0")"
    touch .git/hooks/pre-commit
    rm .git/hooks/pre-commit
    ln -s ../../pre-commit-hook.sh .git/hooks/pre-commit
    5 changes: 5 additions & 0 deletions pre-push-hook-install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #!/bin/sh
    cd "$(dirname "$0")"
    touch .git/hooks/pre-push
    rm .git/hooks/pre-push
    ln -s ../../pre-push-hook.sh .git/hooks/pre-push
    File renamed without changes.
  4. Jared Hooper revised this gist Oct 29, 2018. 1 changed file with 7 additions and 34 deletions.
    41 changes: 7 additions & 34 deletions pre-commit-hook.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    #!/bin/sh

    # checks if locally staged changes are
    # formatted properly. Ignores non-staged
    # changes.
    # Intended as git pre-commit hook
    # checks if locally staged changes pass tests.

    _DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' )
    @@ -19,54 +16,30 @@ then
    git stash --keep-index -u > /dev/null
    fi

    echo "* Compiling/formatting staged changes"
    $DIR/sbt ";test:compile;format" > /dev/null
    compiles=$?
    echo "* Running Tests."
    $DIR/sbt ";clean;test" > /dev/null
    passes=$?

    echo "* Compiles?"

    if [ $compiles -eq 0 ]
    if [ $passes -eq 0 ]
    then
    echo "* Yes!"

    echo "* Properly formatted?"
    git diff --quiet
    formatted=$?

    if [ $formatted -eq 0 ]
    then
    echo "* Yes!"
    else
    echo "* No!"
    echo "The following files need formatting (in stage or commited):"
    git diff --name-only
    echo ""
    fi
    else
    echo "* No!"
    fi

    echo "* Undoing formatting"
    git stash --keep-index > /dev/null
    git stash drop > /dev/null

    if ! [ $hadNoNonStagedChanges -eq 0 ]
    then
    echo "* Scheduling stash pop of previously stashed non-staged changes for 1 second after commit"
    sleep 1 && git stash pop --index > /dev/null & # sleep and & otherwise commit fails when this leads to a merge conflict
    fi

    if [ $compiles -eq 0 ] && [ $formatted -eq 0 ]
    if [ $passes -eq 0 ]
    then
    echo "... done. Proceeding with commit."
    exit 0
    elif [ $compiles -eq 0 ]
    then
    echo "... done."
    echo "CANCELLING commit due to NON-FORMATTED CODE."
    exit 1
    else
    echo "... done."
    echo "CANCELLING commit due to COMPILE ERROR."
    echo "CANCELLING commit due to COMPILE or TEST ERROR."
    exit 2
    fi
  5. @cvogt cvogt created this gist Jun 16, 2015.
    5 changes: 5 additions & 0 deletions pre-commit-hook-install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #!/bin/sh
    cd "$(dirname "$0")"
    touch .git/hooks/pre-commit
    rm .git/hooks/pre-commit
    ln -s ../../pre-commit-hook.sh .git/hooks/pre-commit
    72 changes: 72 additions & 0 deletions pre-commit-hook.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/bin/sh

    # checks if locally staged changes are
    # formatted properly. Ignores non-staged
    # changes.
    # Intended as git pre-commit hook

    _DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
    DIR=$( echo $_DIR | sed 's/\/.git\/hooks$//' )

    echo ""
    echo "Running pre-commit hook ... (you can omit this with --no-verify, but don't)"
    git diff --quiet
    hadNoNonStagedChanges=$?

    if ! [ $hadNoNonStagedChanges -eq 0 ]
    then
    echo "* Stashing non-staged changes"
    git stash --keep-index -u > /dev/null
    fi

    echo "* Compiling/formatting staged changes"
    $DIR/sbt ";test:compile;format" > /dev/null
    compiles=$?

    echo "* Compiles?"

    if [ $compiles -eq 0 ]
    then
    echo "* Yes!"

    echo "* Properly formatted?"
    git diff --quiet
    formatted=$?

    if [ $formatted -eq 0 ]
    then
    echo "* Yes!"
    else
    echo "* No!"
    echo "The following files need formatting (in stage or commited):"
    git diff --name-only
    echo ""
    fi
    else
    echo "* No!"
    fi

    echo "* Undoing formatting"
    git stash --keep-index > /dev/null
    git stash drop > /dev/null

    if ! [ $hadNoNonStagedChanges -eq 0 ]
    then
    echo "* Scheduling stash pop of previously stashed non-staged changes for 1 second after commit"
    sleep 1 && git stash pop --index > /dev/null & # sleep and & otherwise commit fails when this leads to a merge conflict
    fi

    if [ $compiles -eq 0 ] && [ $formatted -eq 0 ]
    then
    echo "... done. Proceeding with commit."
    exit 0
    elif [ $compiles -eq 0 ]
    then
    echo "... done."
    echo "CANCELLING commit due to NON-FORMATTED CODE."
    exit 1
    else
    echo "... done."
    echo "CANCELLING commit due to COMPILE ERROR."
    exit 2
    fi