Debian Codegraph @

util/test-repo-whitespace.BAT

raw
1 @setLocal
2 @echo off
3
4 :: `test-repo-whitespace [DIR]...` v2022.01.01
5 :: style inspector ~ whitespace: find nonconforming files in repository
6
7 :: Copyright (C) 2016-2022 ~ Roy Ivy III <[email protected]>
8 :: License: MIT/Apache-2.0 (see https://opensource.org/licenses/Apache-2.0 , https://opensource.org/licenses/MIT)
9 :: * this software is provided for free, WITHOUT ANY EXPRESS OR IMPLIED WARRANTY (see the license for details)
10
11 :: spell-checker:ignore (shell/cmd) COMSPEC ERRORLEVEL
12 :: spell-checker:ignore () CTYPE POSIX Tval Tvar akefile makefile makefiles multiline testdata
13
14 :config
15 set "_exclude_dirs_rx=(?i)[_.#]build|[.]git|[.]gpg|[.]obj|[.]vs|fixtures|node_modules|target|testdata|test-resources|vendor"
16 set "_exclude_files_rx=(?i)[.](cache|dll|exe|gif|gz|ico|png|zip)$"
17 set "_crlf_files_rx=(?i)[.](bat|cmd|csv)$"
18 set "_tabbed_files_rx=(?i)^^(.*[.]go|go[.](mod|sum)|(GNU)?[Mm]akefile([.].*)?)$"
19 :config_done
20
21 set _dp0=%~dp0.
22 set _nx0=%~nx0
23 set dirs=%*
24 if NOT DEFINED dirs if EXIST "%CD%\repo" ( set dirs="%CD%\repo" )
25 if NOT DEFINED dirs if EXIST "%_dp0%\..\.git" ( set dirs="%_dp0%\.." )
26 if NOT DEFINED dirs ( set "dirs=." )
27
28 set PCREGREP=pcregrep
29 if EXIST "%_dp0%\pcregrep.EXE" ( set "PCREGREP=%_dp0%\pcregrep.EXE" )
30 "%PCREGREP%" --version >NUL 2>NUL || ( echo ERR!: Missing required `pcregrep` [try `scoop install pcregrep`] 1>&2 & goto _undefined_ 2>NUL || "%COMSPEC%" /d/c exit 1 )
31
32 echo [ %dirs% ]
33
34 if /i "%LC_CTYPE%"=="posix" (set "LC_CTYPE=C") &:: `pcregrep` doesn't understand the common "POSIX", replace with the equivalent "C"
35
36 set "ERRORLEVEL="
37 set "ERROR="
38 :: 1. Test for TABs within leading whitespace (except go files makefiles)
39 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --exclude "%_tabbed_files_rx%" --count --files-with-matches --recursive "^\s*\t" %dirs%
40 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: files found with TABs within leading whitespace [file:#lines_matched])
41
42 :: 2. Test for lines with internal TABs
43 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --count --files-with-matches --recursive "^.*[^\t]\t" %dirs%
44 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: files found with lines containing internal TABs [file:#lines_matched])
45
46 :: 3. Test that makefiles have ONLY initial-TAB leading whitespace
47 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --include "(GNU)?[Mm]akefile([.].*)?" --exclude "[.](to|y)ml$" --recursive --line-number --invert-match "^([\t]\s*\S|\S|$)" %dirs%
48 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: Makefiles found with lines having non-TAB leading whitespace [file:line_number])
49
50 :: 4. Test for non-LF line endings
51 set "HAVE_NonLF_ERROR="
52 "%PCREGREP%" --buffer-size=1M -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" -NLF --files-with-matches --multiline --recursive "\r[^\n]" %dirs%
53 if NOT "%ERRORLEVEL%" == "1" ( set HAVE_NonLF_ERROR=1 & echo ## files found with CR line endings)
54 "%PCREGREP%" --buffer-size=1M -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --exclude "%_crlf_files_rx%" -NLF --files-with-matches --multiline --recursive "\r\n" %dirs%
55 if NOT "%ERRORLEVEL%" == "1" ( set HAVE_NonLF_ERROR=1 & echo ## files found with CRLF line endings)
56 if DEFINED HAVE_NonLF_ERROR ( set ERROR=1 & echo ERROR: files found with non-LF line endings)
57
58 :: 5. Test for files without trailing newline
59 :: "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --files-without-match --multiline --recursive "\r?[\r\n]\z" %dirs%
60 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --files-with-matches --multiline --recursive "\z" %dirs%
61 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: files found without trailing newline)
62
63 :: 6. Test for files with lines having trailing whitespace
64 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --recursive --line-number "\s$" %dirs%
65 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: files found with lines having trailing whitespace [file:line_number])
66
67 :: 7. Test for files with BOM
68 "%PCREGREP%" -I --exclude-dir "%_exclude_dirs_rx%" --exclude "%_exclude_files_rx%" --files-with-matches --multiline --recursive "\A[\xEF][\xBB][\xBF]" %dirs%
69 if NOT "%ERRORLEVEL%" == "1" ( set ERROR=1 & echo ERROR: files found with leading BOM)
70
71 :script_done
72 if NOT DEFINED ERROR (
73 echo success: no file errors found
74 )
75 call :#exit__title __exit_title "%COMSPEC%"
76 set "__exit_title=" && title %__exit_title% && set "ERRORLEVEL=" & goto _undefined_ 2>NUL || "%COMSPEC%" /d/c "exit %ERROR%"
77
78 @rem::::
79 @rem:: FUNCTIONS
80 @goto :EOF
81
82 ::
83 :#exit__title ( ref_RETURN PATH )
84 :: RETURN == name of PATH
85 setLocal
86 set "_RETvar=%~1"
87 set "_RETval=%~2"
88 if NOT DEFINED _RETval ( set "_RETval=cmd" & goto :#exit__title_RETURN )
89 set "_RETval=%~n2"
90 :#exit__title_RETURN
91 endLocal & set %_RETvar%^=%_RETval%
92 goto :EOF
93 ::