diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-12-07 03:55:45 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-12-07 03:55:47 +0100 |
commit | 3c6e62b75293b6625509ade3c278fc2d4d147c30 (patch) | |
tree | b33f76c2634a771879f9178cff8e5335e43d2f43 /app/History/PartialCommitInfo.hs | |
parent | a5dde0c6e1c1f54a1660f6c2345277927beef30f (diff) |
chore: increase performance by caching everything
Initial cache generation is slower, as we are losing out on parallelism.
Diffstat (limited to 'app/History/PartialCommitInfo.hs')
-rw-r--r-- | app/History/PartialCommitInfo.hs | 138 |
1 files changed, 0 insertions, 138 deletions
diff --git a/app/History/PartialCommitInfo.hs b/app/History/PartialCommitInfo.hs deleted file mode 100644 index 6d93e88..0000000 --- a/app/History/PartialCommitInfo.hs +++ /dev/null @@ -1,138 +0,0 @@ -module History.PartialCommitInfo - ( PartialCommitInfo (..), - getPartialCommitInfos, - ) -where - -import CMark qualified as D -import Control.Exception (catch, handle) -import Data.Binary (Binary) -import Data.ByteString.Lazy.Char8 qualified as LB8 -import Data.Function ((&)) -import Data.List.NonEmpty qualified as N -import Data.Maybe (catMaybes) -import Data.Text qualified as T -import Die (die) -import Exception qualified as E -import GHC.Generics (Generic) -import Git qualified -import History.Cache (cached) -import History.CommitHash (CommitHash (..)) -import Issue (Issue (..)) -import Issue.Parser qualified as I -import Issue.Provenance qualified as I -import Issue.Tag qualified as I -import Issue.Text qualified as I -import Parallel (parMapM) -import Process (proc, sh) -import Render qualified as P -import System.Directory (getCurrentDirectory) -import System.FilePath ((</>)) -import System.IO.Temp (withSystemTempDirectory) -import System.Process.Typed (setWorkingDir) -import TreeGrepper.Comment qualified as G - --- | `PartialCommitInfo` records the complete issues ONLY in files that have --- been changed in the commit. --- TODO Change `PartialCommitInfo` -> `CommitIssuesChanged` -data PartialCommitInfo = PartialCommitInfo - { hash :: CommitHash, - filesChanged :: [FilePath], - issues :: [Issue] - } - deriving (Show, Binary, Generic) - -getPartialCommitInfos :: IO [PartialCommitInfo] -getPartialCommitInfos = do - commitHashes <- N.toList <$> Git.getCommitHashes - parMapM getCommitInfoOf (map Commit commitHashes ++ [WorkingTree]) - -getCommitInfoOf :: CommitHash -> IO PartialCommitInfo -getCommitInfoOf WorkingTree = do - (issues, filesChanged) <- getIssuesAndFilesWorkingTreeChanged [] - pure $ - PartialCommitInfo - { hash = WorkingTree, - .. - } -getCommitInfoOf (Commit hash) = cached (hash <> (T.pack ".changed")) $ \_ -> do - (issues, filesChanged) <- getIssuesAndFilesCommitChanged hash - pure $ - PartialCommitInfo - { hash = Commit hash, - .. - } - --- | Given the hash of a commit, get all issues in the files which have --- been changed by this commit, as well as all changed files. -getIssuesAndFilesCommitChanged :: T.Text -> IO ([Issue], [FilePath]) -getIssuesAndFilesCommitChanged hash = do - withSystemTempDirectory "history" $ \tmp -> do - let cwd = tmp </> T.unpack hash - Git.withWorkingTree cwd hash do - files <- gitShowChanged cwd - issues <- concat <$> catch (mapM (getIssues cwd) files) dieOfInvalidTreeGrepperResult - pure (issues, files) - --- | Get all issues in the given directory and file. -getIssues :: FilePath -> FilePath -> IO [Issue] -getIssues cwd filename = - handle (\(_ :: E.UnknownFileExtension) -> pure []) $ - fmap catMaybes . mapM (fromComment cwd) - =<< G.getComments cwd filename - --- | Note that `provenance` is trivial and needs to be fixed up later. -fromComment :: FilePath -> G.Comment -> IO (Maybe Issue) -fromComment cwd comment = do - commit <- I.commitFromHEAD cwd - let provenance = I.Provenance commit commit - - pure $ - ( \parseResult -> - let (markers, title) = - I.stripIssueMarkers (T.pack (show (P.render parseResult.heading))) - in Issue - { title = title, - description = N.nonEmpty parseResult.paragraphs, - file = comment.file, - provenance = provenance, - start = comment.start, - end = comment.end, - tags = I.extractTags parseResult.tags, - markers = markers, - rawText = rawText, - commentStyle = commentStyle, - comments = N.nonEmpty parseResult.comments, - closed = False - } - ) - <$> I.parse I.issueMarkers (D.commonmarkToNode [] rawText) - where - (commentStyle, rawText) = G.uncomment comment.file_type comment.text - -dieOfInvalidTreeGrepperResult :: E.InvalidTreeGrepperResult -> IO a -dieOfInvalidTreeGrepperResult (E.InvalidTreeGrepperResult e) = - die e - --- | Gets issues in all files which have been changed in your current --- [working --- - tree](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefworkingtreeaworkingtree) -getIssuesAndFilesWorkingTreeChanged :: [FilePath] -> IO ([Issue], [FilePath]) -getIssuesAndFilesWorkingTreeChanged paths = do - cwd <- getCurrentDirectory - files <- gitLsFilesModifiedIn cwd paths - issues <- concat <$> catch (mapM (getIssues cwd) files) dieOfInvalidTreeGrepperResult - pure (issues, files) - -gitShowChanged :: FilePath -> IO [FilePath] -gitShowChanged cwd = - Prelude.lines . LB8.unpack - <$> sh ("git show -p --name-only --format=''" & setWorkingDir cwd) - -gitLsFilesModifiedIn :: FilePath -> [FilePath] -> IO [FilePath] -gitLsFilesModifiedIn cwd paths = - Prelude.lines . LB8.unpack - <$> sh - ( proc "git ls-files --modified %" ("--" : paths) - & setWorkingDir cwd - ) |