@@ -33,6 +33,7 @@ import System.FilePath ((</>))
3333import Data.Orphans ()
3434
3535import qualified Codec.Archive.Tar as Tar
36+ import qualified Codec.Archive.Tar.Entry as Tar
3637import qualified Data.ByteString as B
3738import qualified Data.ByteString.Char8 as B8
3839import qualified Data.ByteString.Lazy as BSL
@@ -56,11 +57,14 @@ import Data.TreeDiff.Instances.Cabal ()
5657import Data.TreeDiff.Pretty (ansiWlEditExprCompact )
5758#endif
5859
60+ import Data.Time.Clock.System
61+ import Data.Time.Format
62+
5963-------------------------------------------------------------------------------
6064-- parseIndex: Index traversal
6165-------------------------------------------------------------------------------
6266
63- parseIndex :: (Monoid a , NFData a ) => (FilePath -> Bool )
67+ parseIndex :: (Monoid a , NFData a ) => (Tar. EpochTime -> FilePath -> Bool )
6468 -> (FilePath -> B. ByteString -> IO a ) -> IO a
6569parseIndex predicate action = do
6670 configPath <- getCabalConfigPath
@@ -99,7 +103,7 @@ parseIndex predicate action = do
99103
100104parseIndex'
101105 :: (Monoid a , NFData a )
102- => (FilePath -> Bool )
106+ => (Tar. EpochTime -> FilePath -> Bool )
103107 -> (FilePath -> B. ByteString -> IO a ) -> FilePath -> IO a
104108parseIndex' predicate action path = do
105109 putStrLn $ " Reading index from: " ++ path
@@ -110,7 +114,7 @@ parseIndex' predicate action path = do
110114
111115 where
112116 cons entry entries
113- | predicate (Tar. entryPath entry) = entry : entries
117+ | predicate (Tar. entryTime entry) ( Tar. entryPath entry) = entry : entries
114118 | otherwise = entries
115119
116120 f entry = case Tar. entryContent entry of
@@ -320,6 +324,13 @@ main = join (O.execParser opts)
320324 , O. progDesc " tests using Hackage's index"
321325 ]
322326
327+ indexP =
328+ fmap cvt <$> O. optional (O. strOption (O. long " index-state" <> O. metavar " YYYY-MM-DD" ))
329+ where
330+ cvt =
331+ systemSeconds . utcToSystemTime .
332+ parseTimeOrError False defaultTimeLocale " %Y-%m-%d"
333+
323334 optsP = subparser
324335 [ command " read-fields" readFieldsP
325336 " Parse outer format (to '[Field]', TODO: apply Quirks)"
@@ -330,20 +341,20 @@ main = join (O.execParser opts)
330341
331342 defaultA = do
332343 putStrLn " Default action: parsec k"
333- parsecA (mkPredicate [" k" ]) False
344+ parsecA [" k" ] False Nothing
334345
335- readFieldsP = readFieldsA <$> prefixP
336- readFieldsA pfx = parseIndex pfx readFieldTest
346+ readFieldsP = readFieldsA <$> prefixP <*> indexP
347+ readFieldsA pfx idx = parseIndex (mkPredicate pfx idx) readFieldTest
337348
338- parsecP = parsecA <$> prefixP <*> keepGoingP
349+ parsecP = parsecA <$> prefixP <*> keepGoingP <*> indexP
339350 keepGoingP =
340351 O. flag' True (O. long " keep-going" ) <|>
341352 O. flag' False (O. long " no-keep-going" ) <|>
342353 pure False
343354
344- parsecA pfx keepGoing = do
355+ parsecA pfx keepGoing idx = do
345356 begin <- Clock. getTime Clock. Monotonic
346- ParsecResult n w f <- parseIndex pfx (parseParsecTest keepGoing)
357+ ParsecResult n w f <- parseIndex (mkPredicate pfx idx) (parseParsecTest keepGoing)
347358 end <- Clock. getTime Clock. Monotonic
348359 let diff = Clock. toNanoSecs $ Clock. diffTimeSpec end begin
349360
@@ -353,14 +364,14 @@ main = join (O.execParser opts)
353364 putStrLn $ showFFloat (Just 6 ) (fromInteger diff / 1e9 :: Double ) " seconds elapsed"
354365 putStrLn $ showFFloat (Just 6 ) (fromInteger diff / 1e6 / fromIntegral n :: Double ) " milliseconds per file"
355366
356- roundtripP = roundtripA <$> prefixP <*> testFieldsP
357- roundtripA pfx testFieldsTransform = do
358- Sum n <- parseIndex pfx (roundtripTest testFieldsTransform)
367+ roundtripP = roundtripA <$> prefixP <*> testFieldsP <*> indexP
368+ roundtripA pfx testFieldsTransform idx = do
369+ Sum n <- parseIndex (mkPredicate pfx idx) (roundtripTest testFieldsTransform)
359370 putStrLn $ show n ++ " files processed"
360371
361- checkP = checkA <$> prefixP
362- checkA pfx = do
363- CheckResult n w x a b c d e <- parseIndex pfx parseCheckTest
372+ checkP = checkA <$> prefixP <*> indexP
373+ checkA pfx idx = do
374+ CheckResult n w x a b c d e <- parseIndex (mkPredicate pfx idx) parseCheckTest
364375 putStrLn $ show n ++ " files processed"
365376 putStrLn $ show w ++ " files have lexer/parser warnings"
366377 putStrLn $ show x ++ " files have check warnings"
@@ -370,7 +381,7 @@ main = join (O.execParser opts)
370381 putStrLn $ show d ++ " build dist suspicious warning"
371382 putStrLn $ show e ++ " build dist inexcusable"
372383
373- prefixP = fmap mkPredicate $ many $ O. strArgument $ mconcat
384+ prefixP = many $ O. strArgument $ mconcat
374385 [ O. metavar " PREFIX"
375386 , O. help " Check only files starting with a prefix"
376387 ]
@@ -380,8 +391,14 @@ main = join (O.execParser opts)
380391 , O. help " Test also 'showFields . fromParsecFields . readFields' transform"
381392 ]
382393
383- mkPredicate [] = const True
384- mkPredicate pfxs = \ n -> any (`isPrefixOf` n) pfxs
394+ indexPredicate :: Maybe Tar. EpochTime -> (k -> Bool ) -> (Tar. EpochTime -> k -> Bool )
395+ indexPredicate Nothing k = const k
396+ indexPredicate (Just indexDate) k =
397+ \ e -> if (e <= indexDate) then k else const False
398+
399+ mkPredicate :: [String ] -> Maybe Tar. EpochTime -> (Tar. EpochTime -> FilePath -> Bool )
400+ mkPredicate [] idx = indexPredicate idx (const True )
401+ mkPredicate pfxs idx = indexPredicate idx (\ n -> any (`isPrefixOf` n) pfxs)
385402
386403 command name p desc = O. command name
387404 (O. info (p <**> O. helper) (O. progDesc desc))
0 commit comments