1

So basically when a branch is pushed I want to run a diff between the master branch and a target branch and then feed those files into eslint.

The command is:

git diff --name-only master..$TRAVIS_BRANCH | grep -E '\.js$' | xargs npx eslint

It works when I use it locally and replace $TRAVIS_BRANCH with my current working branch.

However, when I use this on travis ci, the build process only checks out the branch its testing against and not master. So it comes out with the error

$ git diff --name-only master..$TRAVIS_BRANCH | grep -E '\.js$' | xargs npx eslint
fatal: ambiguous argument 'master..test-fail': unknown revision or path not in the working tree.

How can I get travis CI to diff the files changed in a branch and run eslint only on those files?

.travis.yml

language: node_js
node_js:
  - "lts/*"
  - "node"
  - "8"
jobs:
  include:
    - stage: lint
      script: git diff --name-only master..$TRAVIS_BRANCH | grep -E '\.js$' | xargs npx eslint
nanomosfet
  • 132
  • 10
  • 1
    Why don't you just lint the whole thing in CI? – jonrsharpe Nov 21 '18 at 10:07
  • 1
    Not my question but the reason is because the test would fail. I basically want to enforce that new changes meet the standards but if there are problems already don't fail the build – nanomosfet Nov 21 '18 at 10:11
  • Did you look at https://docs.travis-ci.com/user/customizing-the-build? – jonrsharpe Nov 21 '18 at 10:14
  • Possible duplicate of [How can I customize / override the "git clone" step in Travis CI?](https://stackoverflow.com/questions/32580821/how-can-i-customize-override-the-git-clone-step-in-travis-ci) – jonrsharpe Nov 21 '18 at 10:19
  • yeah but I still don't know how to complete what I want. Maybe I can use the git sparse checkout thing. not sure – nanomosfet Nov 21 '18 at 10:19

1 Answers1

0

To throw another answer into the mix that doesn't require python I came across Fastlint which seems to do exactly this.

For example, run eslint on all js files in the src or tests directory changed compared to the origin/master branch.

fastlint --status --print0 --glob '{src,tests}/**/*.{js,jsx}' origin/master HEAD | xargs -0 eslint
csilk
  • 2,403
  • 2
  • 22
  • 39