Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Last active April 15, 2021 03:36
Show Gist options
  • Save kentcdodds/9768d9a8d0bfbf6797cd to your computer and use it in GitHub Desktop.
Save kentcdodds/9768d9a8d0bfbf6797cd to your computer and use it in GitHub Desktop.
Shell script to lint only changed files to be used as a githook (specific to my project)
#!/usr/bin/env bash
set -e
# allow being run from somewhere other than the git rootdir
gitroot=$(git rev-parse --show-cdup)
# default gitroot to . if we're already at the rootdir
gitroot=${gitroot:-.};
nm_bin=$gitroot/node_modules/.bin
echo "Linting changed files"
SRC_FILES=$(git diff --staged --diff-filter=ACMTUXB --name-only -- '*.js' | grep -v '\.test\|mock\|e2e\.js$') && x=1
TEST_FILES=$(git diff --staged --diff-filter=ACMTUXB --name-only -- '*.js' | grep '\.test\|mock\|e2e\.js$') && x=1
function lint() {
if [ "$2" ]; then
echo "Linting changed $1 files"
$nm_bin/eslint $2 -c other/${1}.eslintrc
else
echo "No $1 files changed"
fi
}
lint "app" $SRC_FILES;
lint "test" $TEST_FILES;
echo "⚡️ changed files passed linting! ⚡️"
@aubford
Copy link

aubford commented Dec 5, 2019

This is amazing. Here's my simplified version for linting only changed js and jsx files. This lints all changed files, not just staged ones.

eslint $(git diff head --name-only | grep '^.*[.js,.jsx]$')

@earonesty
Copy link

earonesty commented Jan 3, 2020

See also: https://pypi.org/project/lint_diffs: works with many linters (including eslint) and many vcs. It can run on all changed files, and show linter errors for the whole file, or for only changed lines. Good for gradually improving linter errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment