Last Updated: February 25, 2016
·
4.915K
· rane

Lint code before git pushing

Linting before pushing can be useful. Of course, sometimes seeing the problems as you write code is even better, but potentially distracting.

This script runs gulp jshint and/or gulp scss-lint only if the changes are being pushed to files relevant to the lint tasks. For example, gulp jshint will only run if javascript files have changed.

Usage

Place the file in .git/hooks/pre-push and make sure it's executable. Customize to your needs.

bonus #protip

icefox/git-hooks is a great tool for managing git hooks.

#!/bin/bash

# lint
#   - if pushing changes to .js files: run gulp jshint
#   - if pushing changes to .scss files: run gulp scss-lint

set -e

remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
  if [ "$local_sha" = $z40 ]; then
    # branch deleted
    :
  else
    if [ "$remote_sha" = $z40 ]; then
      jshint=1
      scsslint=1
    else
      range="$remote_sha..$local_sha"
      files=`git diff --name-only $range`

      [ -n "`echo $files | grep \.js$`" ] && jshint=1
      [ -n "`echo $files | grep \.scss$`" ] && scsslint=1
    fi

    if [[ $jshint -eq 1 ]]; then
      gulp jshint --target=production
    fi

    if [[ $scsslint -eq 1 ]]; then
      gulp scss-lint --target=production
    fi
  fi
done

exit 0