0

I want to add pre-commit hook for jshint in svn. As I new to svn therefore, I need some help. I already did some work which is following. Below is my local repo folder structure

svn-repo
    branches
    hooks
        pre-commit.sh
    tags
    trunk
        scripts
            script.js
        index.html
    .jshintrc

Here is my pre-commit hook code

    ROOT_DIR=$(git rev-parse --show-toplevel) # gets the path of this repo
    CONF="--config=${ROOT_DIR}/build/config/jshint.json" # path of your jshint config
    JSHINT=$(which jshint) # jshint path

    if git rev-parse --verify HEAD >/dev/null 2>&1; then
        against=HEAD
    else
        # Initial commit: diff against an empty tree object
        against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    fi


    for file in $(git diff-index --name-only ${against} -- | egrep \.js); do
        if $JSHINT $file ${CONF} 2>&1 | grep 'No errors found' ; then
            echo "jslint passed ${file}"
            exit 0
        else
            $JSHINT $file
            exit 1
        fi
    done

I installed jshint globally and is located on below location

/usr/local/bin/jshint

Now when I commit with incorrect javascript then it still gets committed and does not throw any errors even though js contains errors according to .jshintrc.

How can I make this pre-commit work?

Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • There are some things you should think on before you turn this on. How long does it take jshint to run? How long will it take in a year? What I'm getting at is that you're going to have a bad time if someone checks in while jshint is running. What will the failure message look like for users (you can't assume what client they're using). They will likely think the commit is hung when it doesn't return right away. I think your team would be way happier if you propped up a simple CI loop (perhaps with jenkins) to run jshint and then send out the results. – thekbb Apr 14 '14 at 15:36

1 Answers1

0

Unrelated

  • I can't understand, why you used Git-voodoo here, in and for SVN-transaction

Related (partially)

If you want to monitor hook from client side and see any output, you must redirect it to stderr (and you didn't do it), which hook will return to client

Question

Does this script work as expected in standalone-mode?

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • I want to monitor hooks from client side. How can I redirect it to stderr? I haven't tested if this script works as standalone. – Om3ga Apr 12 '14 at 11:48
  • @2619 - ust append `2>&1` to every informative output, which you want to return to client (and remove from unwanted output). See http://stackoverflow.com/a/1928036/960558 for good-style reject-message – Lazy Badger Apr 12 '14 at 12:00
  • Still no affect on it. – Om3ga Apr 12 '14 at 12:05
  • @2619 - ok I can forgot anything, but `if $JSHINT $file ${CONF} 2>&1 | grep 'No errors found' ; then` seems terrible - you redirect jshint's output from stdout to stderr and pipe output (stdout to stdin) into grep, grep without option just output grep'ed pattern (and I don't recall exit code)... Can jshint have own exit-code? – Lazy Badger Apr 12 '14 at 13:42
  • @2619 - and yes, behore using as hook, get it working as expected in standalone mode – Lazy Badger Apr 12 '14 at 13:44