Skip to content

Commit b07b5ba

Browse files
whitespace check: rewrite in Julia, add checks
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.
1 parent 100a741 commit b07b5ba

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ docs-revise:
9999

100100
check-whitespace:
101101
ifneq ($(NO_GIT), 1)
102-
@$(JULIAHOME)/contrib/check-whitespace.sh
102+
@$(JULIAHOME)/contrib/check-whitespace.jl
103103
else
104104
$(warn "Skipping whitespace check because git is unavailable")
105105
endif

Diff for: contrib/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Installation
66
|[ mac/ ](https://github.com/JuliaLang/julia/blob/master/contrib/mac/) | Mac install files |
77
|[ windows/ ](https://github.com/JuliaLang/julia/blob/master/contrib/windows/) | Windows install files |
88
|[ 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 |
9-
|[ check-whitespace.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/check-whitespace.sh) | Check for trailing white space |
9+
|[ check-whitespace.jl ](https://github.com/JuliaLang/julia/blob/master/contrib/check-whitespace.jl) | Check for white space issues |
1010
|[ commit-name.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/commit-name.sh) | Computes a version name for a commit |
1111
|[ fixup-libgfortran.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/fixup-libgfortran.sh) | Include libgfortran and libquadmath for installations |
1212
|[ fixup-libstdc++.sh ](https://github.com/JuliaLang/julia/blob/master/contrib/fixup-libstdc++.sh) | Include libstdc++ for installations |

Diff for: contrib/check-whitespace.jl

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env julia
2+
3+
const patterns = split("""
4+
*.1
5+
*.c
6+
*.cpp
7+
*.h
8+
*.inc
9+
*.jl
10+
*.lsp
11+
*.make
12+
*.md
13+
*.mk
14+
*.rst
15+
*.scm
16+
*.sh
17+
*.yml
18+
*Makefile
19+
""")
20+
21+
const errors = Set{Tuple{String,Int,String}}()
22+
23+
for path in eachline(`git ls-files -- $patterns`)
24+
lineno = 0
25+
non_blank = 0
26+
27+
file_err(msg) = push!(errors, (path, 0, msg))
28+
line_err(msg) = push!(errors, (path, lineno, msg))
29+
30+
for line in eachline(path, keep=true)
31+
lineno += 1
32+
contains(line, '\r') && file_err("non-UNIX line endings")
33+
contains(line, '\ua0') && line_err("non-breaking space")
34+
endswith(line, '\n') || line_err("no trailing newline")
35+
line = chomp(line)
36+
endswith(line, r"\s") && line_err("trailing whitespace")
37+
contains(line, r"\S") && (non_blank = lineno)
38+
end
39+
non_blank < lineno && line_err("trailing blank lines")
40+
end
41+
42+
if isempty(errors)
43+
println(stderr, "Whitespace check found no issues.")
44+
exit(0)
45+
else
46+
println(stderr, "Whitespace check found $(length(errors)) issues:")
47+
for (path, lineno, msg) in sort!(collect(errors))
48+
if lineno == 0
49+
println(stderr, "$path -- $msg")
50+
else
51+
println(stderr, "$path:$lineno -- $msg")
52+
end
53+
end
54+
exit(1)
55+
end

Diff for: contrib/check-whitespace.sh

-39
This file was deleted.

0 commit comments

Comments
 (0)