Skip to content

Instantly share code, notes, and snippets.

@seeliang
Last active December 2, 2024 05:08
Show Gist options
  • Save seeliang/0f0de424d1cdc4541c338f4ee93b7e6a to your computer and use it in GitHub Desktop.
Save seeliang/0f0de424d1cdc4541c338f4ee93b7e6a to your computer and use it in GitHub Desktop.
How to lint only changed files?

find out the differences

use git diff to generate file list

git diff --name-only master

limited to certain file types

add ext filter

git diff --name-only master | grep -E "(.js$|.ts$|.tsx$)"

handle the case of file removal

ignore deleted files

git diff --name-only --diff-filter=ACMRTUXB master | grep -E "(.js$|.ts$|.tsx$)"

node scripts

add eslint to package.json

....
"scripts":
  "lint:script": "eslint -c eslintrc.js $(git diff --name-only --diff-filter=ACMRTUXB master | grep  -E \"(.js$|.ts$|.tsx$)\")"

CI script

sample with circle ci here

....
"scripts":
 "lint:script": "eslint -c eslintrc.js $(git diff --name-only --diff-filter=ACMRTUXB origin/master | grep  -E \"(.js$|.ts$|.tsx$)\")"

for stylelint

will need grep fallback for empty result cases

 
 ....
 "scripts":
   "lint:style": "stylelint --config .stylelintrc.js $(git diff --name-only --diff-filter=ACMRTUXB origin/master | grep  -E \"(.js$|.ts$|.tsx$)\" || echo \".stylelintrc.js\")"

NOTE: make sure our local branch is up to date (merge origin/master)

@morriq
Copy link

morriq commented Apr 6, 2021

thank you very much, I used it with --relative on git diff 🚀

@Yegorich555
Copy link

stylelint --config .stylelintrc.js $(git diff --name-only --diff-filter=ACMRTUXB origin/master | grep -E "(.js$|.ts$|.tsx$)" || echo ".stylelintrc.js")

There is an error. Instead " we should use ' - otherwise it doesn't work on git-bush and git-actions (ubuntu)

Script tries to fix '.stylelintrc.js' instead of skipping! Echo doesn't work in expected way on git-bush (windows OS). Any ideas?

@seeliang
Copy link
Author

stylelint --config .stylelintrc.js $(git diff --name-only --diff-filter=ACMRTUXB origin/master | grep -E "(.js$|.ts$|.tsx$)" || echo ".stylelintrc.js")

There is an error. Instead " we should use ' - otherwise it doesn't work on git-bush and git-actions (ubuntu)

Script tries to fix '.stylelintrc.js' instead of skipping! Echo doesn't work in expected way on git-bush (windows OS). Any ideas?

stylelint does not support empty case

@benmosher
Copy link

Why --diff-filter=ACMRTUXB master over --diff-filter=d --cached?

@seeliang
Copy link
Author

@benmosher I think that could work for local. in fact, we have switched to origin/release to make sure it is up to date

https://seeliang.medium.com/how-to-update-lint-without-lint-the-whole-project-841150aa59b0

@AlexJeffcott
Copy link

using --relative is useful if you have a monorepo
git diff --relative --name-only --diff-filter=ACMRTUXB master

@yinonov
Copy link

yinonov commented Jan 1, 2022

will that apply all the same?

# current branch against default (main) branch
git diff origin/main --diff-filter=ACMRTUXB --name-only -- *.js

# staged
git diff --cached --diff-filter=ACMRTUXB --name-only -- *.js 

@Anubarak
Copy link

Could you eventually include windows commands as well? Because I always get the error

grep : The term 'grep' is not recognized as the name of a cmdlet

@seeliang
Copy link
Author

seeliang commented Apr 9, 2022

@yinonov it depends,
origin/main is for large team that pushing to main every hour

@seeliang
Copy link
Author

seeliang commented Apr 9, 2022

@Anubarak not windows expert, I think you can try findstr

@justin808
Copy link

@ko22009
Copy link

ko22009 commented May 17, 2023

I check this solution and it is not better then use cache.

@greghor
Copy link

greghor commented Jun 25, 2023

Just slightly adapted it for sqlfluff, another linter - and it works like a charm! thanks!

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