Skip to content

Commit

Permalink
squash! DX: Check trailing spaces in project files only
Browse files Browse the repository at this point in the history
DX: Check trailing spaces in project files only

Previously the `check_trailing_spaces.sh` script was checking files that
do not belong to the project, for example:

 - diverse `composer.lock` files
 - local development files like `.idea` or temporary files created

In its' previous design, the list of files to check for trailing spaces
is generated by `find` which is not aware about different files (not) in
the project. To exclude non-project files, it worked with a larger
blacklist of checking if a file is not in diverse paths, for example
and most prominently:

  - -not -path "./.git/*"

which is kind of contradictory as the project *is* managed by git, but
that folder can have a totally different name.

This blacklist is pretty verbose and also needs extra knowledge and care
due to the nature of `find` as it operates on file-system level, not
project level more specifically.

Files in the project are managed by `git` and `git` knows which files
belong into the project and which not. Git on ...

 - ... repository level contains the information which project files are
   ignored. for example the `composer.lock` files.
 - ... developer or system level also knows about which files are
   ignored. for example the `.idea` folder (see "global .gitignore")

Change here is to replace `find` as file-system only utility with `git`
itself, namely the `git-grep` command, for the operation to obtain
the list of files to check.

This includes the exec-call of grep, as gits' grep is a little more
powerful, too [1] *and* is compatible in the output format for the
post-processing with sed in the check-script.

This allows to drop the manually crafted blacklist and to specifically
mark paths/files not to be checked [2] which are (only) the test
fixtures in:

 - tests/Fixtures/

The knowledge of git about the project itself and how the developer
works next to the reduced list of files to check has the additional
benefit of a cache and overall results in a much better performance.

[1]: Fun with "git grep" <https://gitster.livejournal.com/27674.html>
[2]: pathspec - gitglossary <https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec>
  • Loading branch information
ktomk committed May 16, 2020
1 parent c28322d commit dd7c53f
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions check_trailing_spaces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
set -eu

files_with_trailing_spaces=$(
git ls-files -z \
':!tests/Fixtures/' \
| xargs -0 grep -EIHn "\\s$" \
git grep -EIn "\\s$" \
':!tests/Fixtures/*' \
| sort -fh
)

Expand Down

0 comments on commit dd7c53f

Please sign in to comment.