diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-14 06:52:32 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-14 07:07:45 +0100 |
commit | 1821aca5ef5451d0b1943c8640c5eb1f6fa7bbee (patch) | |
tree | dc3df78464a164b3fb9674d4e03bc56d58a56df1 | |
parent | 2c107e71572888327496d327a36737c02f64d894 (diff) |
chore: resolve TODOs
-rw-r--r-- | app/Exception.hs | 8 | ||||
-rw-r--r-- | app/Git.hs | 18 | ||||
-rw-r--r-- | app/Review.hs | 30 |
3 files changed, 41 insertions, 15 deletions
diff --git a/app/Exception.hs b/app/Exception.hs index db7612b..6ac243b 100644 --- a/app/Exception.hs +++ b/app/Exception.hs @@ -8,6 +8,7 @@ module Exception InvalidIssue (..), CannotReadFile (..), UnsupportedLanguage (..), + NoAncestor (..), ) where @@ -16,6 +17,7 @@ import Control.Exception import Data.ByteString.Lazy.Char8 as LB import Data.Text qualified as T import Data.Void (Void) +import Git.CommitHash qualified as Git import System.Exit (ExitCode) import Text.Megaparsec qualified as P @@ -27,6 +29,7 @@ data AnyException | InvalidDiff' InvalidDiff | InvalidIssue' InvalidIssue | UnsupportedLanguage' UnsupportedLanguage + | NoAncestor' NoAncestor deriving (Show) instance Exception AnyException @@ -74,3 +77,8 @@ data UnsupportedLanguage = UnsupportedLanguage T.Text deriving (Show) instance Exception UnsupportedLanguage + +data NoAncestor = NoAncestor Git.CommitHash Git.CommitHash + deriving (Show) + +instance Exception NoAncestor @@ -31,7 +31,7 @@ import Exception qualified as E import GHC.Generics (Generic) import Git.CommitHash import Patch qualified as A -import Process (proc, sh) +import Process (proc, sh, sh_) import Text.Printf (printf) getCommitHashes :: IO (NonEmpty T.Text) @@ -96,6 +96,7 @@ readTextFileOfText = readTextFileOf LT.readFile LT.decodeUtf8 readTextFileOfBS :: CommitHash -> FilePath -> IO LB.ByteString readTextFileOfBS = readTextFileOf LB.readFile id + -- REVIEW Suggestion: we could use `id` instead of `\x -> x` -- -- REVIEW OK! @@ -118,9 +119,20 @@ resolveRef = . sh . proc "git rev-parse %" --- TODO Throw if `prevHash` is not an ancestor of `hash`. +-- | `getCommitsBetween prevCommit commit` returns the commits from `prevCommit` to `commit`. The result excludes `prevCommit`, but includes `commit`. +-- +-- If `prevCommit` is not an ancestor of `commit`, this functions throws `NoAncestor commit prevCommit`. getCommitsBetween :: CommitHash -> CommitHash -> IO [CommitHash] -getCommitsBetween (Commit prevHash) (Commit hash) = do +getCommitsBetween WorkingTree commit@(Commit _) = + throwIO (E.NoAncestor WorkingTree commit) +getCommitsBetween WorkingTree WorkingTree = pure [WorkingTree] +getCommitsBetween prevCommit WorkingTree = + fmap (++ [WorkingTree]) . getCommitsBetween prevCommit + =<< resolveRef "HEAD" +getCommitsBetween prevCommit@(Commit prevHash) commit@(Commit hash) = do + catch + (sh_ (proc "git merge-base --is-ancestor % %" prevHash hash)) + (\(_ :: E.ProcessException) -> throwIO (E.NoAncestor commit prevCommit)) map (Commit . T.strip) . T.lines . T.decodeUtf8 . LB.toStrict <$> sh (proc "git log --format=%%H %..%" prevHash hash) diff --git a/app/Review.hs b/app/Review.hs index 37d9ee4..721d8e3 100644 --- a/app/Review.hs +++ b/app/Review.hs @@ -7,6 +7,7 @@ module Review ) where +import Comment.Language qualified as L import Control.Monad (ap, forM, forM_, when) import Data.Binary qualified as B import Data.ByteString.Lazy qualified as LB @@ -116,11 +117,18 @@ reviewPatch commitMessages fileDeltas = addComments :: D.FileDeltas -> D.FileDeltas addComments = - map . mapContent . mapHunks . mapLines $ \line@(D.Line {..}) -> - if lineAnnotation == D.Comment - then -- TODO Haskell comment - D.Line D.Added ("-- REVIEW" <> lineContent) - else line + map + ( \fileDelta@(D.FileDelta {D.fileDeltaSourceFile}) -> + ( mapContent . mapHunks . mapLines $ + \line@(D.Line {..}) -> + if lineAnnotation == D.Comment + then + let language = L.fromPath (T.unpack fileDeltaSourceFile) + in D.Line D.Added (L.lineStart language <> " REVIEW" <> lineContent) + else line + ) + fileDelta + ) where mapContent f x = x {D.fileDeltaContent = f x.fileDeltaContent} mapHunks _ D.Binary = D.Binary @@ -155,12 +163,8 @@ separateReview commit fileDeltas fileDeltas' = patchFile = "a.patch" patchFile' = "b.patch" -withTempSourceFiles :: - Git.CommitHash -> - D.FileDeltas -> - (FilePath -> IO a) -> - IO a -withTempSourceFiles (Git.Commit hash) fileDeltas action = do +withTempSourceFiles :: Git.CommitHash -> D.FileDeltas -> (FilePath -> IO a) -> IO a +withTempSourceFiles commit fileDeltas action = do withSystemTempDirectory "anissue" $ \tmp -> do createDirectoryIfMissing False (tmp </> "a") createDirectoryIfMissing False (tmp </> "b") @@ -168,7 +172,9 @@ withTempSourceFiles (Git.Commit hash) fileDeltas action = do let sourceDir = takeDirectory sourceFile fileContents <- if sourceFile /= "/dev/null" - then sh (proc "git show %:%" hash sourceFile) + then case commit of + Git.Commit hash -> sh (proc "git show %:%" hash sourceFile) + Git.WorkingTree -> sh (proc "cat" sourceFile) else pure "" createDirectoryIfMissing True (tmp </> "a" </> sourceDir) LB.writeFile (tmp </> "a" </> sourceFile) fileContents |