aboutsummaryrefslogtreecommitdiffstats
path: root/app/TreeGrepper/Comment.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/TreeGrepper/Comment.hs')
-rw-r--r--app/TreeGrepper/Comment.hs44
1 files changed, 28 insertions, 16 deletions
diff --git a/app/TreeGrepper/Comment.hs b/app/TreeGrepper/Comment.hs
index 0ca9543..7c2ca90 100644
--- a/app/TreeGrepper/Comment.hs
+++ b/app/TreeGrepper/Comment.hs
@@ -7,7 +7,7 @@ module TreeGrepper.Comment
)
where
-import Control.Exception (throw)
+import Control.Exception (catch, throw)
import Data.Aeson qualified as A
import Data.Binary (Binary)
import Data.ByteString.Lazy.Char8 qualified as B
@@ -15,11 +15,15 @@ import Data.Function ((&))
import Data.List (find)
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
+import Data.Text.Lazy.IO qualified as LT
import Exception qualified as E
import GHC.Generics (Generic)
+import Git qualified
import Process (proc, sh)
-import System.FilePath (takeExtension)
-import System.Process.Typed (setWorkingDir)
+import System.Directory (createDirectoryIfMissing)
+import System.FilePath (takeDirectory, takeExtension, takeFileName, (</>))
+import System.IO.Temp (withSystemTempDirectory)
+import System.Process.Typed qualified as P
import TreeGrepper.FileType (FileType (..))
import TreeGrepper.FileType qualified as G
import TreeGrepper.Match (Match (..), Position (..))
@@ -90,20 +94,28 @@ stripBlockComment blockStart blockEnd text =
fromMatch :: Result -> Match -> Comment
fromMatch Result {..} Match {..} = Comment {..}
-getComments :: FilePath -> FilePath -> IO [Comment]
-getComments cwd fn = do
+getComments :: Git.CommitHash -> FilePath -> IO [Comment]
+getComments commitHash fn = do
let ext = takeExtension fn
- concatMap (\result -> map (fromMatch result) result.matches)
- . map fixTreeGrepper
- . decode
- <$> sh
- ( proc
- "tree-grepper --query % % --format json %"
- (treeGrepperLanguage ext)
- (treeGrepperQuery ext)
- fn
- & setWorkingDir cwd
- )
+ s <-
+ catch
+ (Git.readTextFileOf commitHash fn)
+ (\(_ :: E.ProcessException) -> pure "")
+ withSystemTempDirectory (takeFileName fn) $ \cwd -> do
+ createDirectoryIfMissing True (cwd </> takeDirectory fn)
+ LT.writeFile (cwd </> fn) s
+ concatMap (\result -> map (fromMatch result) result.matches)
+ . map fixTreeGrepper
+ . decode
+ <$> sh
+ ( ( proc
+ "tree-grepper % --query % % --format json"
+ fn
+ (treeGrepperLanguage ext)
+ (treeGrepperQuery ext)
+ )
+ & P.setWorkingDir cwd
+ )
decode :: B.ByteString -> [Result]
decode = either (throw . E.InvalidTreeGrepperResult) id . A.eitherDecode