From 5c9c7b360e4578f3db487e663587194746d0386b Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 8 Dec 2023 07:34:55 +0100 Subject: fix: fix `Git.readTextFileOf` --- app/Comment.hs | 2 +- app/Exception.hs | 2 +- app/Git.hs | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) (limited to 'app') diff --git a/app/Comment.hs b/app/Comment.hs index 558778b..5d1c1ef 100644 --- a/app/Comment.hs +++ b/app/Comment.hs @@ -59,7 +59,7 @@ getComments commitHash filePath = . (T.encodeUtf8 . LT.toStrict) =<< catch (Git.readTextFileOf commitHash filePath) - (\(_ :: E.ProcessException) -> pure "") + (\(_ :: E.CannotReadFile) -> pure "") where language = fromExtension (takeExtension filePath) diff --git a/app/Exception.hs b/app/Exception.hs index 41097b3..5428194 100644 --- a/app/Exception.hs +++ b/app/Exception.hs @@ -62,7 +62,7 @@ data InvalidIssue = InvalidIssue (P.ParseErrorBundle [D.Node] Void) instance Exception InvalidIssue -data CannotReadFile = CannotReadFile +data CannotReadFile = CannotReadFile FilePath deriving (Show) instance Exception CannotReadFile diff --git a/app/Git.hs b/app/Git.hs index d308026..b4e36e1 100644 --- a/app/Git.hs +++ b/app/Git.hs @@ -12,7 +12,7 @@ module Git ) where -import Control.Exception (throw, throwIO) +import Control.Exception (IOException, catch, throw, throwIO) import Data.Binary (Binary, Put, get, put) import Data.ByteString.Lazy qualified as LB import Data.Fixed (Pico) @@ -30,6 +30,7 @@ import Exception qualified as E import GHC.Generics (Generic) import Git.CommitHash import Process (proc, sh) +import Text.Printf (printf) import Prelude hiding (lines) getCommitHashes :: IO (NonEmpty T.Text) @@ -105,10 +106,12 @@ getCommitOf commitHash@(Commit hash) = do } _ -> throwIO E.NoCommits --- TODO Fix `readTextFileOf` --- --- Handle file does not exist in `WorkingTree` case. readTextFileOf :: CommitHash -> FilePath -> IO LT.Text -readTextFileOf WorkingTree filePath = LT.readFile filePath +readTextFileOf WorkingTree filePath = + catch + (LT.readFile filePath) + (\(_ :: IOException) -> throwIO (E.CannotReadFile filePath)) readTextFileOf (Commit hash) filePath = - LT.decodeUtf8 <$> sh (proc "git show %:%" hash filePath) + catch + (LT.decodeUtf8 <$> sh (proc "git show %:%" hash filePath)) + (\(_ :: E.ProcessException) -> throwIO (E.CannotReadFile (printf "%s:%s" hash filePath))) -- cgit v1.2.3