'Running TSLint only on changed files with Git Diff
I want to run tslint
only on files that were modified in a pull request, instead of the entire project.
git diff --name-only develop -- *.ts
successfully prints out the filenames that have changed between the feature branch and develop.
I'm trying to pipe that command into my lint command. Lint can take in a list of files to run against instead of running against everything by placing the filenames after:
git diff --name-only develop -- *.ts | npm run lint
This works on our Jenkins box, but on my local Windows machine no text gets appended to the end of the lint command.
C:\code>git diff --name-only develop-- *.ts | npm run lint
tslint -c .\tslint.json -p .\tsconfig.json
Is there a simple one line method that will only run lint on changed files in a branch?
Solution 1:[1]
I got a list of modified files by running
git diff --name-only --diff-filter=ACMR origin/dev-123...develop -- *.ts
Where origin/dev-123
is the source branch and develop
is the destination.
I put the output of that command into an array, and ran the linter against the array of files.
Solution 2:[2]
You can use Git Bash to run the below:
npm run lint $(git diff --name-only --diff-filter=ACMR origin/dev-123...develop -- *.ts)
Solution 3:[3]
On Windows, the commands above may not work because of unix based bash features. So, I made a cmd batch script that works on CMD, IDEs like VsCode, and even on git-bash (that has unix bash features).
Here it goes for those who, like me, are forced to work on Windows (kidding):
- Create a
lint-git-changed.bat
script and save it on project's root:
@echo off
setlocal enableDelayedExpansion
set fileCount=0
set fileList=
for /f "eol=: delims=" %%F in ('git diff --name-only HEAD --diff-filter=ACMRTUXB -- *.ts') do (
set /a fileCount+=1
if .!fileList!==. (
set fileList=%%F
) else (
set fileList=!fileList! %%F
)
)
if %fileCount% GTR 0 (
echo Linting %fileCount% files...
npx tslint "%fileList%"
) else (
echo Lint: Nenhum arquivo para verificar.
exit 0
)
- Add a script command on package.json
"scripts": {
...
"lint:changed": "lint-git-changed.bat",
...
}
- Run
npm run lint:changed
and it's done. Alternative you can add it also on pre-commit hooks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | sax |
Solution 3 | vanderdill |