---
title: "Run jshint when git commit"
author: "qlqllu"
date: "2014-05-27T17:38:34Z"
tags: []
categories: ["研发管理"]
slug: "run-jshint-when-git-commit"
draft: false
lastmod: "2014-05-27T17:38:34Z"
id: 2072289
url: "https://qlqllu.iteye.com/blog/2072289"
---

#!/bin/sh

\#

\# Run JSHint validation before commit.

 

files=$(git diff --cached --name-only --diff-filter=ACMR -- \*.js)

pass=true

 

if \[ "$files" != "" ]; then

    for file in ${files}; do

        result=$(jshint ${file})

 

        if \[ "$result" != "" ]; then

            echo $result

            pass=false

        fi

    done

fi

 

 

if $pass; then

    exit 0

else

    echo ""

    echo "COMMIT FAILED:"

    echo "Some JavaScript files are invalid. Please fix errors and try committing again."

    exit 1

fi