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
|
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
module History (getIssues, InvalidTreeGrepperResult (..), UnknownFileExtension (..), listIssues) where
import Control.Exception (Exception, catch, handle, throw)
import Data.Aeson (eitherDecode)
import Data.ByteString.Lazy.Char8 qualified as L8
import Data.List (intercalate)
import Data.Maybe (catMaybes)
import Data.String (fromString)
import Data.Text (Text, lines, unpack)
import Data.Text.Encoding (decodeUtf8)
import Issue (Issue (..), fromMatch)
import Issue.Filter (Filter, applyFilter)
import Process (quote, sh, sh_)
import System.Directory (getCurrentDirectory, setCurrentDirectory)
import System.Exit (ExitCode (ExitFailure), exitWith)
import System.FilePath (takeExtension, (</>))
import System.IO.Temp (withSystemTempDirectory)
import Text.Printf (printf)
import TreeGrepper.Match qualified as G
import TreeGrepper.Result qualified as G
import Prelude hiding (lines)
import Prelude qualified as Prelude
data UnknownFileExtension = UnknownFileExtension
{ extension :: String
}
deriving (Show)
instance Exception UnknownFileExtension
data InvalidTreeGrepperResult = InvalidTreeGrepperResult
{ error :: String
}
deriving (Show)
instance Exception InvalidTreeGrepperResult
listIssues :: [Filter] -> [FilePath] -> IO [Issue]
listIssues filters files = do
commits <- fmap (lines . decodeUtf8 . L8.toStrict) $ sh ("git log --format=%H")
fmap head $ mapM (\commit -> listIssuesOf commit filters files) commits
listIssuesOf :: Text -> [Filter] -> [FilePath] -> IO [Issue]
listIssuesOf commit filters files = do
cwd <- getCurrentDirectory
issue <- withSystemTempDirectory "history" $ \tmp -> do
let worktree = tmp </> unpack commit
sh_ (fromString (printf "git worktree add --detach %s %s" (quote worktree) (quote (unpack commit))))
setCurrentDirectory worktree
filter (applyFilter filters) . concat
<$> catch
( mapM (handle forgetGetIssuesExceptions . getIssues)
=<< getFiles files
)
(\(InvalidTreeGrepperResult e) -> die e)
setCurrentDirectory cwd
pure issue
where
forgetGetIssuesExceptions :: UnknownFileExtension -> IO [a]
forgetGetIssuesExceptions _ = pure []
getFiles :: [String] -> IO [FilePath]
getFiles files =
Prelude.lines . L8.unpack
<$> sh
( fromString
( (printf "git ls-files --cached --exclude-standard --other%s")
( case files of
[] -> ""
_ -> " -- " ++ intercalate " " (map quote files)
)
)
)
getIssues :: FilePath -> IO [Issue]
getIssues filename = do
let extension = takeExtension filename
treeGrepperLanguage =
-- TODO Add support for all tree-grepper supported files
--
-- tree-grepper supported files can be listed through `tree-grepper
-- --languages`.
case extension of
".elm" -> "elm"
".hs" -> "haskell"
".nix" -> "nix"
".sh" -> "sh"
_ -> throw (UnknownFileExtension extension)
treeGrepperQuery =
case extension of
".elm" -> "([(line_comment) (block_comment)])"
".hs" -> "(comment)"
".nix" -> "(comment)"
".sh" -> "(comment)"
_ -> throw (UnknownFileExtension extension)
decode raw =
case eitherDecode raw of
Left e -> throw (InvalidTreeGrepperResult e)
Right treeGrepperResult -> treeGrepperResult
matches <-
concatMap (\result -> map ((,) result) result.matches)
. map fixTreeGrepper
. decode
<$> sh
( fromString
( printf
"tree-grepper --query %s %s --format json %s"
(quote treeGrepperLanguage)
(quote treeGrepperQuery)
(quote filename)
)
)
catMaybes <$> mapM (uncurry fromMatch) matches
fixTreeGrepper :: G.Result -> G.Result
fixTreeGrepper treeGrepperResult =
treeGrepperResult {G.matches = G.merge treeGrepperResult.matches}
die :: String -> IO a
die s = do
printf "error: %s\n" s
exitWith (ExitFailure 1)
|