62

GitHub - codeocelot/git-diff-lint: Lint only the files your branch touches

 6 years ago
source link: https://github.com/codeocelot/git-diff-lint
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

git-diff-lint

Unopinionated lint runner that only lints files that your feature branch has modified.

Many codebases don't start off with good linting standards and adopting a linting standard in a large codebase is rarely non-trivial or easy. git-diff-lint is a tool that allows contributors to enjoy the benefits of linting and automated status checks on their feature branches without requiring the entire project to conform. git-diff-lint only lints files that have been modified since the current branch diverges from it's parent.

git-diff-lint is configurable with many popular linters and faithfully preserves the linter exit code meaning it works great as a status check or git pre-push hook.

Installation

Just download and add command to your system path:

curl -O https://raw.githubusercontent.com/codeocelot/git-diff-lint/master/git-diff-lint
export PATH=$PATH/$(pwd)
# add to zshrc to use in future sessions
echo "export PATH=\$PATH:$(pwd)" >> ~/.zshrc
# or add to bashrc
echo "export PATH=\$PATH:$(pwd)" >> ~/.bashrc

Usage

$ git-diff-lint -x eslint -e .js
$ git-diff-lint -x "pycodestyle --statistics" -e .py -b master

Executes linter command with the list of modified files compared to parent git branch. Returns status from linter command and prints any linting errors to stderr.

Options:

flag env var purpose default
-x LINT lint command, e.g. eslint pycodestyle
-b BRANCH parent git branch stage
-e EXT file regex expression \.py$
-h <none> Show help message and exit

Recipes

ESLint

  • Lint .js & .jsx files with ESlint:
git-diff-lint -x "npx eslint" -e ".js$|.jsx" -b stage
# alternatively, using env vars:
EXT=".js$|.jsx$" LINT="npx eslint" BRANCH=stage git-diff-lint

Node package.json

Escaping strings is annoying in package.json, but doable.

{
  "name": "your-project",
  "scripts": {
    "lint": "EXT=\".js$|.jsx$|.ts$|.tsx$\" LINT=\"npx eslint --ext .js,.jsx,.ts,.tsx\" ./tools/git-diff-lint/git-diff-lint"
  }
}

Or create a script file and call it.

# lint.sh
git-diff-lint -x "npx eslint" -e ".js$|.jsx" -b stage
{
  "name": "your-project",
  "scripts": {
    "lint": "./lint.sh"
  }
}

Contributing

Have any ideas on how to make this tool more useful? PRs are greatly appreciated. Since this project doesn't have a test suite, please make sure your contribution doesn't break existing functionality and within your PR, please document how to test your proposed feature.

I prefer if you use commitizen to format your commits, but any reasonable committing strategy is fine.

If you have ideas on how to write tests for this project, please share!

License

MIT Licensed. Go forth and create, but please provide attribution back to this project.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK