1

Due to gJSLint spewing out errors because of missing member documentation. We updated our pre-commit hook tasked with linting JavaScript. The edit included gJSLint rule 220 (ignore missing member documentation). However when performing a commit GIT still complains that the JavaScript is invalid. Running the gJSLint on its own produces no errors.

gjslint --strict --disable=5,6,110,220 app/pits/modules/api.js

We have removed the staged files and then added them back to staging with no luck.

Does GIT cache pre-commit hooks?

Pre-Commit Hook

#!/bin/sh

files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$") if [ "$files" = "" ]; then 
    exit 0  fi

pass=true

echo "\nValidating JavaScript:\n"

for file in ${files}; do
    result=$(gjslint --strict --disable=5,6,110,220 ${file} | grep "${file} is OK")
    if [ "$result" != "" ]; then
        echo "\t\033[32mJSLint Passed: ${file}\033[0m"
    else
        echo "\t\033[31mJSLint Failed: ${file}\033[0m"
        pass=false
    fi done

echo "\nJavaScript validation complete\n"

if ! $pass; then
    echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.\n          try: gjslint --strict --disable=5,6,110,220 file.js\n"
    exit 1 else
    echo "\033[42mCOMMIT SUCCEEDED\033[0m\n" fi

Result of git commit -m"...."

Validating JavaScript:

JSLint Failed: app/pits/modules/api.js

JSLint Failed: app/pits/modules/State.js

JSLint Failed: app/pits/modules/table.js

JavaScript validation complete

COMMIT FAILED: Your commit contains files that should pass JSLint but

              do not. Please fix the JSLint errors and try again.

              try: gjslint --strict --disable=5,6,110,220 file.js

Environment

git version 1.8.1.2

gjslint version 2.3.13

lubuntu version 13.04

Ben Crowhurst
  • 8,204
  • 6
  • 48
  • 78
  • 1
    The way you check for `gjslint` errors seems hacky. Can't you just check the return value instead of grepping the output? You might want to print the output as well to do some debugging. – pmr Jan 24 '14 at 14:36
  • 1
    Agreed, this would be a valuable improvement. – Ben Crowhurst Jan 24 '14 at 14:41
  • 1
    The answer to the subject-question is "no"; but your pre-commit hook contains various minor sins, aside from the [issue noted by pmr](http://stackoverflow.com/questions/21335220/does-git-cache-pre-commit-hooks#comment32162779_21335220). See [check if my commit has 'import pdb' in emacs/git?](http://stackoverflow.com/q/21298044/1256452) for details. – torek Jan 24 '14 at 14:42

1 Answers1

1

This issue had nothing to do with GIT and was caused by a poorly written pre-commit hook.

#!/bin/sh

files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
    exit 0
fi

pass=true

echo "\nValidating JavaScript:\n"

for file in ${files}; do
    eval "gjslint --strict --disable=5,6,110,220 ${file}"

    if [ $? -eq 0 ]; then
        echo "\t\033[32mJSLint Passed: ${file}\033[0m"
    else
        echo "\t\033[31mJSLint Failed: ${file}\033[0m"
        pass=false
    fi
done

echo "\nJavaScript validation complete\n"

if ! $pass; then
    echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.\n          try: gjslint --strict --disable=5,6,110,220 file.js\n"
    exit 1
else
    echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
Ben Crowhurst
  • 8,204
  • 6
  • 48
  • 78