1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Main where
-- base
import Control.Exception
( IOException, handle )
import Control.Monad
( forM, forM_, unless, when )
import Data.Maybe
( isJust, mapMaybe )
import System.Environment
( getArgs )
import System.Exit
( ExitCode(..), exitWith )
-- containers
import Data.Map.Strict
( Map )
import qualified Data.Map.Strict as Map
( alter, empty, keys, findWithDefault )
-- mtl
import Control.Monad.Writer
( liftIO, execWriter, tell )
-- process
import System.Process
( readProcess )
-- text
import Data.Text
( Text )
import qualified Data.Text as T
import qualified Data.Text.IO as T
( readFile )
-- linters-common
import Linters.Common
( LintMsg(..), LintLvl(..)
, GitType(..)
, gitCatBlob, gitDiffTree
, z40
)
--------------------------------------------------------------------------------
main :: IO ()
main = do
getArgs >>= \case
("commits":dir:refs) -> mainCommits dir refs
("commits":_) -> fail "usage: lint-whitespace commits <git-repo> [<commit-id>+]"
("files":fs) -> mainFiles fs
("tracked":args) -> mainTracked args
_ -> fail "usage: lint-whitespace <files|commits|tracked> ..."
mainCommits :: FilePath -> [String] -> IO ()
mainCommits dir refs = do
stats <- forM (map T.pack refs) $ \ref -> do
(cid,deltas) <- gitDiffTree dir ref
lintMsgs0 <- forM deltas $ \(origs, (gt, blobId), fname) -> if (gt == GitTypeRegFile && hasSuffix fname)
then do
let blobIds0 = [ b0 | (GitTypeRegFile, b0, _) <- origs, b0 /= z40 ]
blob1 <- gitCatBlob dir blobId
blobs0 <- mapM (gitCatBlob dir) blobIds0
-- blobs0 will be empty in case in case of newly added files as well as renames/copies
-- blobs0 will contain more than one entry for merge-commits
return [ (fname, msg) | msg <- lintBlob blobs0 blob1 ]
else return []
checkLintMsgs ("commit " <> T.unpack cid) lintMsgs0
finalReport stats
finalReport :: [Maybe LintLvl] -> IO ()
finalReport stats = do
unless (null $ filter isJust stats) $
putStrLn "====================================================================================="
let stats1 = maximum (Nothing : stats)
-- unless (stats1 == Nothing) $ do
-- putStrLn "There were commit message linter issues! For more information see"
-- putStrLn ""
unless (stats1 < Just LintLvlErr) $ do
putStrLn "Validation FAILED because at least one commit had linter errors!"
exitWith (ExitFailure 1)
putStrLn "whitespace validation passed!"
checkLintMsgs :: String -> [[(Text, LintMsg)]] -> IO (Maybe LintLvl)
checkLintMsgs herald lintMsgs0 = do
let lintMsgs = concat lintMsgs0
status = maximum (Nothing : [ Just lvl | (_, LintMsg lvl _ _ _) <- lintMsgs ])
ok = status < Just LintLvlErr
unless (null lintMsgs) $ liftIO $ do
putStrLn "====================================================================================="
putStrLn (herald <> " has whitespace linter issues:")
putStrLn ""
forM_ lintMsgs $ \(fn, LintMsg lvl lno l m) -> do
let lvls = case lvl of
LintLvlErr -> "*ERROR*"
LintLvlWarn -> "Warning"
putStrLn (" " <> lvls <> " " <> T.unpack fn <> ":" <> show lno <> ": " <> T.unpack m)
putStrLn (" > " <> show l)
putStrLn ""
return ()
unless ok $ liftIO $
putStrLn ("Validation FAILED for " <> herald)
return status
mainFiles :: [FilePath] -> IO ()
mainFiles fs = do
stats <- forM fs $ \f -> do
lintMsgs0 <- handle (\(_ :: IOException) -> return []) (lintFile <$> T.readFile f)
checkLintMsgs ("file " <> f) [[(T.pack f, err) | err <- lintMsgs0 ]]
finalReport stats
mainTracked :: [String] -> IO ()
mainTracked args = do
(ignoredFiles, ignoredDirs) <- parseTrackedArgs args
allFiles <- lines <$> readProcess "git" ["ls-tree", "--name-only", "-r", "HEAD"] ""
let files = filter (isTracked ignoredFiles ignoredDirs) allFiles
mainFiles files
-- Check a file for trailing whitespace and tabs
lintFile :: Text -> [LintMsg]
lintFile blob1 = execWriter $ do
when (hasTabs blob1) $ do
tell [ LintMsg LintLvlErr lno l "introduces TAB"
| (lno,l) <- zip [1..] lns
, "\t" `T.isInfixOf` l
]
when (hasTrail blob1) $ do
tell [ LintMsg LintLvlErr lno l "introduces trailing whitespace"
| (lno,l) <- zip [1..] lns, hasTrail l ]
where
lns = T.lines blob1
lintBlob :: [Text] -> Text -> [LintMsg]
lintBlob blobs0 blob1 = execWriter $ do
-- Perform simple invariant-preservation checks
when (hasTabs blob1 && not (any hasTabs blobs0)) $ do
tell [ LintMsg LintLvlErr lno l "introduces TAB"
| (lno,l) <- zip [1..] lns
, "\t" `T.isInfixOf` l
]
when (hasTrail blob1 && not (any hasTrail blobs0)) $ do
tell [ LintMsg LintLvlErr lno l "introduces trailing whitespace"
| (lno,l) <- zip [1..] lns, hasTrail l ]
when (missingFinalEOL blob1) $ if not (any missingFinalEOL blobs0)
then tell [LintMsg LintLvlErr llno lln "lacking final EOL"]
else tell [LintMsg LintLvlWarn llno lln "lacking final EOL"]
where
lns = T.lines blob1
llno = length lns
lln = case lns of
[] -> ""
_ -> last lns
hasTabs :: Text -> Bool
hasTabs = T.any (=='\t')
hasTrail :: Text -> Bool
hasTrail t = or [ " \n" `T.isInfixOf` t
, " \r\n" `T.isInfixOf` t
, "\t\r\n" `T.isInfixOf` t
, " " `T.isSuffixOf` t
, "\t" `T.isSuffixOf` t
]
missingFinalEOL :: Text -> Bool
missingFinalEOL = not . T.isSuffixOf "\n"
--------------------------------------------------
data Flag
= IgnoreFiles
| IgnoreDirs
| UnknownFlag Text
deriving stock ( Eq, Ord, Show )
parseTrackedArgs :: [String] -> IO ([Text], [Text])
parseTrackedArgs args = do
unless (null bareArgs) $
fail "usage: lint-whitespace tracked --ignore-files ... --ignore-dirs ..."
unless (null unknownFlags) $
fail $ "lint-whitespace tracked: unknown flags " ++ show unknownFlags ++ "\n\
\supported flags are --ignore-files and --ignore-dirs"
return (ignoredFiles, ignoredDirs)
where
(bareArgs, flagArgs) = splitOn flagMaybe (map T.pack args)
ignoredFiles = Map.findWithDefault [] IgnoreFiles flagArgs
ignoredDirs = Map.findWithDefault [] IgnoreDirs flagArgs
unknownFlags = mapMaybe (\case { UnknownFlag unk -> Just unk ; _ -> Nothing})
$ Map.keys flagArgs
-- Assumes the input string has no whitespace.
flagMaybe :: Text -> Maybe Flag
flagMaybe "-f" = Just IgnoreFiles
flagMaybe "--ignore-files" = Just IgnoreFiles
flagMaybe "-d" = Just IgnoreDirs
flagMaybe "--ignore-dirs" = Just IgnoreDirs
flagMaybe str
| let (hyphens, rest) = T.span ((==) '-') str
, not (T.null hyphens)
= Just (UnknownFlag rest)
| otherwise
= Nothing
splitOn :: forall a b. Ord b => (a -> Maybe b) -> [a] -> ([a], Map b [a])
splitOn f = go Nothing
where
go :: Maybe b -> [a] -> ([a], Map b [a])
go _ [] = ([], Map.empty)
go mb_b (a:as) = case f a of
Nothing ->
case go mb_b as of
(xs, yxs) ->
case mb_b of
Nothing -> (a:xs, yxs)
Just b -> (xs, Map.alter (alter_fn a) b yxs)
Just b -> go (Just b) as
alter_fn :: a -> Maybe [a] -> Maybe [a]
alter_fn a Nothing = Just [a]
alter_fn a (Just as) = Just (a:as)
--------------------------------------------------
-- Predicates used to filter which files we lint.
hasSuffix :: Text -> Bool
hasSuffix fn = any (`T.isSuffixOf` fn) suffixes
where
suffixes = T.words ".hs .hsc .lhs .cabal .c .h .lhs-boot .hs-boot .x .y"
autogenFiles :: [ Text ]
autogenFiles = [ "iconv.c", "Table.hs" ]
ignoredPrefixes :: [Text]
ignoredPrefixes = [ "testsuite/", "libraries/base/tests"
, "utils/hp2ps", "utils/hpc", "utils/unlit"
]
isTracked :: [Text] -> [Text] -> FilePath -> Bool
isTracked ignoredFiles ignoredDirs (T.pack -> fn)
= hasSuffix fn
&& not (fn `elem` ignoredFiles)
&& not (any (`T.isPrefixOf` fn) ignoredDirs)
|