Skip to content

Commit

Permalink
whitespace check: rewrite in Julia, add checks
Browse files Browse the repository at this point in the history
This now checks for:

- trailing non-ASCII whitespace
- non-breaking spaces anywhere
- non-UNIX line endings
- trailing blank lines
- no trailing newline

Git can't handle this, largely because whether it interprets files as
UTF-8 or Latin-1 depends on how system libraries that it uses for regex
matching are compiled, which is inconsistent and hard to fix. The end of
file checks are also quite awkard and inefficient to implement with Git
and shell scripting. Julia is fast and lets us present results clearly.
  • Loading branch information
StefanKarpinski committed Mar 14, 2022
1 parent 100a741 commit b07b5ba
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ docs-revise:

check-whitespace:
ifneq ($(NO_GIT), 1)
@$(JULIAHOME)/contrib/check-whitespace.sh
@$(JULIAHOME)/contrib/check-whitespace.jl
else
$(warn "Skipping whitespace check because git is unavailable")
endif
Expand Down
2 changes: 1 addition & 1 deletion contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Installation
|[ mac/ ](https://github.com/JuliaLang/julia/blob/master/contrib/mac/) | Mac install files |
|[ windows/ ](https://github.com/JuliaLang/julia/blob/master/contrib/windows/) | Windows install files |
|[ add_license_to_files.jl ](https://github.com/JuliaLang/julia/blob/master/contrib/add_license_to_files.jl ) | Add the Julia license to files in the Julia Project |
|[ check-whitespace.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/check-whitespace.sh) | Check for trailing white space |
|[ check-whitespace.jl ](https://github.com/JuliaLang/julia/blob/master/contrib/check-whitespace.jl) | Check for white space issues |
|[ commit-name.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/commit-name.sh) | Computes a version name for a commit |
|[ fixup-libgfortran.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/fixup-libgfortran.sh) | Include libgfortran and libquadmath for installations |
|[ fixup-libstdc++.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/fixup-libstdc++.sh) | Include libstdc++ for installations |
Expand Down
55 changes: 55 additions & 0 deletions contrib/check-whitespace.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env julia

const patterns = split("""
*.1
*.c
*.cpp
*.h
*.inc
*.jl
*.lsp
*.make
*.md
*.mk
*.rst
*.scm
*.sh
*.yml
*Makefile
""")

const errors = Set{Tuple{String,Int,String}}()

for path in eachline(`git ls-files -- $patterns`)
lineno = 0
non_blank = 0

file_err(msg) = push!(errors, (path, 0, msg))
line_err(msg) = push!(errors, (path, lineno, msg))

for line in eachline(path, keep=true)
lineno += 1
contains(line, '\r') && file_err("non-UNIX line endings")
contains(line, '\ua0') && line_err("non-breaking space")
endswith(line, '\n') || line_err("no trailing newline")
line = chomp(line)
endswith(line, r"\s") && line_err("trailing whitespace")
contains(line, r"\S") && (non_blank = lineno)
end
non_blank < lineno && line_err("trailing blank lines")
end

if isempty(errors)
println(stderr, "Whitespace check found no issues.")
exit(0)
else
println(stderr, "Whitespace check found $(length(errors)) issues:")
for (path, lineno, msg) in sort!(collect(errors))
if lineno == 0
println(stderr, "$path -- $msg")
else
println(stderr, "$path:$lineno -- $msg")
end
end
exit(1)
end
39 changes: 0 additions & 39 deletions contrib/check-whitespace.sh

This file was deleted.

0 comments on commit b07b5ba

Please sign in to comment.