-.- --. .-. --..

Excluding folders from git log output

18 Jan 2017

I’m working on a relatively big JavaScipt project at the moment. We’re using React, and AirBnB’s Enzyme library for testing React components. A feature of this library is snapshotting which saves the in-memory rendered HTML of the component inside a file1, which can be checked by the test runner later on for unexpected changes. These snapshot files tend to get really big depending on the number of tests added. One of my PRs had a total of 7000 line count mostly because of the API fixture files and the snapshot files. This makes looking at the output of git diff or git log -p output on the console overwhelming, and pretty much useless. Git’s pathspec feature can help in filtering out these files. I wrote about Git’s pathspec feature before, which can be handy in such scenarios. Using the exclude directive supported by pathspec, you can do something like:

git diff master...head -- . ":(exclude)*/__tests__/*"

Note the . after --. That signifies that the base path starts from the current directory. This is necessary for exclude to run. We use jest as the test runner, and it’s configured to find test files under __tests__ directory in the entire directory tree starting from current path.

This excludes all the folders that are named as __tests__ anywhere in the directory tree, and the test/ folder plus all its children under the git root directory. Any glob pattern can be included after the :(exclude) directive.

1^ More on snapshotting in Jest is available in these articles:

  1. https://facebook.github.io/jest/docs/tutorial-react.html#snapshot-testing
  2. https://facebook.github.io/jest/blog/2016/07/27/jest-14.html

For more information on the entire flow of Jest + Enzyme + React, Hackernoon has a detailed post.