-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiffDump.hs
391 lines (345 loc) · 13.6 KB
/
DiffDump.hs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
import System.Console.CmdArgs
( cmdArgs
, args -- remaining arguments
, opt -- optional
, (&=)
, def
, explicit
, typ
, typFile
, typDir
, argPos
, summary
, help
, helpArg
, name
, Data
, Typeable
)
import System.FilePath
( (</>)
, takeDirectory
, dropTrailingPathSeparator
)
import System.FilePath.Find -- From the 'filemanip' package
( find
, extension
, (==?)
, (&&?)
, (||?)
, FilterPredicate
, filePath
, fileName
)
import System.Process.ByteString
( readProcessWithExitCode
)
import System.Directory
( canonicalizePath
, createDirectoryIfMissing
, createDirectory
, doesFileExist
, doesDirectoryExist
, findExecutable
, removeDirectoryRecursive
, getTemporaryDirectory
, getCurrentDirectory
, setCurrentDirectory
)
import System.Exit
( ExitCode(ExitSuccess)
, exitFailure
)
import System.IO
( stderr
, hPutStrLn
)
import System.IO.Unsafe
( unsafePerformIO
)
import GHC.Conc
( getNumProcessors
)
import Data.List
( stripPrefix
)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Control.Monad
( filterM
, when
, unless
)
import Control.Concurrent.ParallelIO.Local
( parallel_
, withPool
, Pool
)
import Control.Exception
( finally
, bracket
, throwIO
)
import System.IO.Error
( isAlreadyExistsError
)
import System.Posix.IO
( stdOutput
)
import System.Posix.Terminal
( queryTerminal
)
import System.Console.ANSI
( SGR(SetColor, Reset)
, ConsoleLayer(Foreground)
, ColorIntensity(Dull)
, Color(Red, Cyan, Green)
, setSGR
)
import Paths_diffdump
( version
)
import Data.Version
( showVersion
)
import System.IO.Error
( catchIOError -- Use this instead of 'catch' to support GHC 7.6.1 and 7.4.x
)
data DiffDump = DiffDump {
outfile :: FilePath
, outdir :: DirPath
, olddir :: DirPath
, newdir :: DirPath
, func :: FilePath
, arg_list :: [String]
, cmd :: FilePath
, arg :: [String]
, cmd1 :: FilePath
, arg1 :: [String]
, cmd2 :: FilePath
, arg2 :: [String]
, filetype :: String
, fileext :: String
, diff :: FilePath
, diff_arg :: [String]
, force :: Bool
, ignore :: [String]
, recursive :: Bool
, unified :: Bool
, color :: Bool
, no_color :: Bool
, verbose :: Bool
, keep_files :: Bool
, succeed_on_diff :: Bool
} deriving (Show, Data, Typeable)
type DirPath = FilePath
diffDumpOpts :: DirPath -> DiffDump
diffDumpOpts defOutdir = DiffDump {
-----------------------------------------------------------------------
-- name default type description --
-----------------------------------------------------------------------
olddir = def &= argPos 0 &= typ "OLD_DIR" &= opt ("-" :: FilePath)
, newdir = def &= argPos 1 &= typ "NEW_DIR" &= opt ("" :: FilePath)
, func = def &= argPos 2 &= typFile &= opt ("" :: FilePath)
, arg_list = def &= args &= typ "ARG"
, outfile = def &= name "o" &= typFile &= help "Output path."
, outdir = defOutdir &= typDir &= help "Directory for tempory files."
, diff = "diff" &= typFile &= help "Path to diff."
, cmd = def &= typFile &= help "Command to run on both arguments."
, arg = def &= typ "ARG" &= help "Argument to pass to both commands."
, cmd1 = def &= typFile &= help "Command to run on first argument. Overrides --cmd"
, cmd2 = def &= typFile &= help "Command to run on second argument. Overrides --cmd"
, arg1 = def &= typ "ARG" &= help "Argument appended to first command."
, arg2 = def &= typ "ARG" &= help "Argument appended to second command."
, filetype = def &= help "Filter by files that include this string in the output of the 'file' command."
, fileext = def &= help "Filter by files that have this file extension."
, ignore = def &= typDir &= help "Ignore contents of the given directory."
, diff_arg = def &= help "Pass <arg> on to diff."
, force = def &= name "f" &= help "Run despite the output directories already existing."
, recursive = def &= name "r" &= help "Recursively compare any subdirectories found."
, unified = def &= name "u" &= help "Output 3 lines of unified context."
, color = def &= help "Force colorized diff in terminal output."
, no_color = def &= help "Do not colorize diff in terminal output."
, verbose = def &= help "Verbose output."
, keep_files = def &= help "Keep intermediary files."
, succeed_on_diff = def &= help "Unlike diff, return zero exit code when there are differences."
} &= summary ("diffdump " ++ showVersion version)
&= helpArg [explicit, name "h", name "help"]
data DumpCfg = DumpCfg {
dumpPath :: FilePath
, dumpArgs :: [String]
} deriving (Show, Eq)
data DiffCfg = DiffCfg {
diffPath :: FilePath
, diffArgs :: [String]
} deriving (Show, Eq)
main :: IO ()
main = do
tmpDir <- getTemporaryDirectory
defaultOutputDir <- createUniqueDirectory 0 (tmpDir </> ".diffdump")
as <- cmdArgs (diffDumpOpts defaultOutputDir)
n <- getNumProcessors
when (verbose as) $ do
hPutStrLn stderr $ "diffdump: Utilizing " ++ show n ++ " CPU(s)."
finally (withPool n (parallelMain as)) $
removeDirectoryRecursive defaultOutputDir
parallelMain :: DiffDump -> Pool -> IO ()
parallelMain as threadPool = do
let oldRoot = dropTrailingPathSeparator (olddir as)
let newRoot = if null (newdir as) then oldRoot else dropTrailingPathSeparator (newdir as)
let outputDir = dropTrailingPathSeparator (outdir as)
let firstCfg = mkDumpCfg (cmd1 as) (func as ++ cmd as) (arg_list as ++ arg as ++ arg1 as)
let secondCfg = mkDumpCfg (cmd2 as) (func as ++ cmd as) (arg_list as ++ arg as ++ arg2 as)
let recArgs = if recursive as then ["-r"] else []
let uniArgs = if unified as then ["-u"] else []
let diffCfg = DiffCfg (diff as) (recArgs ++ uniArgs ++ diff_arg as)
let aDir = outputDir </> "a"
let bDir = outputDir </> "b"
createTempDirectory (force as) aDir
createTempDirectory (force as) bDir
oldRootIsDir <- doesDirectoryExist oldRoot
newRootIsDir <- doesDirectoryExist newRoot
let diffdump = do
if not oldRootIsDir || not newRootIsDir
then do
when (recursive as) $ do
if not oldRootIsDir
then hPutStrLn stderr $ "diffdump: error: " ++ oldRoot ++ " is not a directory"
else hPutStrLn stderr $ "diffdump: error: " ++ newRoot ++ " is not a directory"
exitFailure
dumpObjFiles aDir bDir (firstCfg, secondCfg) oldRoot newRoot
else do
when (verbose as) $ do
hPutStrLn stderr $ "diffdump: Scanning for files..."
oldPaths <- getRelPaths (recursive as) (filetype as) (fileext as) (ignore as) oldRoot
commonPaths <- filterM (doesFileExist . (newRoot </>)) oldPaths
when (verbose as) $ do
hPutStrLn stderr $ "diffdump: Found " ++ show (length commonPaths) ++ " file pairs to compare."
hPutStrLn stderr $ "diffdump: Generating dump files... "
let dumpOps = map (dumpObj aDir bDir (firstCfg, secondCfg) oldRoot newRoot) commonPaths
parallel_ threadPool dumpOps
when (verbose as) $ do
hPutStrLn stderr $ "diffdump: Comparing files... "
diffPath' <- validateExePath (diffPath diffCfg)
runDiff diffCfg{diffPath=diffPath'} outputDir "a" "b"
diffMaybe <- finally diffdump $
unless (keep_files as) $ do
removeDirectoryRecursive aDir
removeDirectoryRecursive bDir
case diffMaybe of
Nothing -> return ()
Just s -> do
isatty <- queryTerminal stdOutput
let isColor = not (no_color as) && (color as || isatty)
writeDiff isColor (unified as) (outfile as) s
when (verbose as) $ do
hPutStrLn stderr $ "diffdump: Done."
unless (succeed_on_diff as) exitFailure
mkDumpCfg :: String -> String -> [String] -> DumpCfg
mkDumpCfg "" "" = DumpCfg "cat" -- Default command, plus custom options
mkDumpCfg "" x = DumpCfg x -- --cmd as command, plus custom options
mkDumpCfg x _ = DumpCfg x -- Override all defaults
validateExePath :: FilePath -> IO FilePath
validateExePath p = do
exists <- doesFileExist p
if not exists
then do
maybePath <- findExecutable p
case maybePath of
Nothing -> do
hPutStrLn stderr $ "diffdump: Executable not found '" ++ p ++ "'."
exitFailure
Just p' -> return p'
else
return p
createTempDirectory :: Bool -> DirPath -> IO ()
createTempDirectory frce p = do
pExists <- doesDirectoryExist p
when (pExists && not frce) $ do
hPutStrLn stderr $ "error: the temporary directory '" ++ p ++ "' already exists. Use '-f' to proceed anyway."
exitFailure
createDirectoryIfMissing True p
writeDiff :: Bool -> Bool -> FilePath -> B.ByteString -> IO ()
writeDiff True isUni "" = putColorizedDiff isUni
writeDiff False _isUni "" = B.putStr
writeDiff _color _isUni p = B.writeFile p
putColorizedDiff :: Bool -> B.ByteString -> IO ()
putColorizedDiff isUni s = do
mapM_ (putColorizedLine isUni) (B8.lines s)
setSGR [Reset] -- cleanup
putColorizedLine :: Bool -> B.ByteString -> IO ()
putColorizedLine True x = setSGR [getUnifiedColor (safeHead ' ' x)] >> B8.putStrLn x
putColorizedLine False x = setSGR [ getColor (safeHead ' ' x)] >> B8.putStrLn x
safeHead :: Char -> B.ByteString -> Char
safeHead x xs
| B.null xs = x
| otherwise = B8.head xs
getColor :: Char -> SGR
getColor '>' = SetColor Foreground Dull Green
getColor '<' = SetColor Foreground Dull Red
getColor _ = Reset
getUnifiedColor :: Char -> SGR
getUnifiedColor '@' = SetColor Foreground Dull Cyan
getUnifiedColor '+' = SetColor Foreground Dull Green
getUnifiedColor '-' = SetColor Foreground Dull Red
getUnifiedColor _ = Reset
dumpObj :: DirPath -> DirPath -> (DumpCfg, DumpCfg) -> DirPath -> DirPath -> FilePath -> IO ()
dumpObj aDir bDir (cfg1, cfg2) oldRoot newRoot p = do
runDump cfg1 (aDir</>p) (oldRoot</>p) ""
runDump cfg2 (bDir</>p) (newRoot</>p) ""
dumpObjFiles :: DirPath -> DirPath -> (DumpCfg, DumpCfg) -> FilePath -> FilePath -> IO ()
dumpObjFiles aDir bDir (cfg1, cfg2) oldPath newPath = do
input <- if oldPath == "-" || newPath == "-" then B.getContents else return ""
runDump cfg1 (aDir</>"dump.txt") oldPath input
runDump cfg2 (bDir</>"dump.txt") newPath input
runDiff :: DiffCfg -> DirPath -> FilePath -> FilePath -> IO (Maybe B.ByteString)
runDiff cfg outputDir p1 p2 = do
absDiffPath <- canonicalizePath (diffPath cfg)
bracket getCurrentDirectory setCurrentDirectory $ const $ do
setCurrentDirectory outputDir
(exitCode, s, err) <- readProcessWithExitCode absDiffPath (diffArgs cfg ++ [p1, p2]) ""
B.hPutStr stderr err
return $ case exitCode of
ExitSuccess -> Nothing
_ -> Just s
createUniqueDirectory :: Int -> FilePath -> IO FilePath
createUniqueDirectory n p = do
(createDirectory tmp >> return tmp)
`catchIOError` (\e -> if isAlreadyExistsError e
then createUniqueDirectory (succ n) p
else throwIO e)
where
tmp = p ++ show n
runDump :: DumpCfg -> FilePath -> FilePath -> B.ByteString -> IO ()
runDump cfg outPath p input = do
let as = dumpArgs cfg ++ (if p == "-" then [] else [p])
s <- readProcess (dumpPath cfg) as input
createDirectoryIfMissing True (takeDirectory outPath)
B.writeFile outPath s
readProcess :: FilePath -> [String] -> B.ByteString -> IO B.ByteString
readProcess nm as input = do
(exitCode, s, err) <- readProcessWithExitCode nm as input
case exitCode of
ExitSuccess -> return s
_ -> do
hPutStrLn stderr $ "diffdump: error executing: " ++ unwords (nm : as)
B8.hPutStrLn stderr err
exitFailure
getRelPaths :: Bool -> String -> String -> [String] -> DirPath -> IO [FilePath]
getRelPaths isRecursive fileTy fileExt ignoreDirs p = do
ps <- find ((return isRecursive &&? isGoodDir) ||? filePath ==? p) ((return (null fileExt) ||? extension ==? fileExt) &&? expectedFileType fileTy) p
return $ map (stripRoot p) ps
where
isGoodDir = do
nm <- fileName
return $ nm `notElem` ignoreDirs
expectedFileType :: String -> FilterPredicate
expectedFileType "" = return True
expectedFileType s = do
p <- filePath
return $ B8.pack s `B8.isInfixOf` unsafePerformIO (readProcess "file" ["--brief", p] "")
stripRoot :: DirPath -> FilePath -> FilePath
stripRoot pre p = maybe p id (stripPrefix (pre ++ "/") p)