From 5842e730152a2ae11fc8772a505baa3ba81b1e9c Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 8 Dec 2023 03:04:43 +0100 Subject: chore: drop `Git.withWorkingTree` --- app/TreeGrepper/Comment.hs | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'app/TreeGrepper') 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 -- cgit v1.2.3