From 75444b933f1f23223576fe0ced682b558393ed21 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Wed, 13 Mar 2024 05:27:44 +0100 Subject: chore: patch shows commit messages --- app/Main.hs | 9 +-------- app/Review.hs | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 0154840..a2fef0b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -592,14 +592,7 @@ main = do `catch` \(_ :: E.ProcessException) -> error "working directory not clean, aborting.." plan <- R.formulatePlan perCommit baseBranch featureBranch - patch <- - A.Patch . concat - <$> mapM - ( \step -> do - R.separateReview step.commit step.changes - =<< R.reviewPatch step.changes - ) - (NE.toList plan.steps) + patch <- A.Patch . concat <$> mapM R.reviewStep (NE.toList plan.steps) T.writeFile "review.patch" (renderAsText patch) -- REVIEW Why is withReviewing in the Status module and not the Review -- module? diff --git a/app/Review.hs b/app/Review.hs index e134a62..ef901ce 100644 --- a/app/Review.hs +++ b/app/Review.hs @@ -2,8 +2,7 @@ module Review ( Plan (..), PlanStep (..), formulatePlan, - reviewPatch, - separateReview, + reviewStep, ) where @@ -38,6 +37,7 @@ type BranchName = T.Text data PlanStep = PlanStep { commit :: Git.CommitHash, + earlierCommit :: Git.CommitHash, changes :: D.FileDeltas } deriving (Show, Generic, B.Binary) @@ -58,31 +58,57 @@ formulatePlan perCommit baseBranch featureBranch = do fileDeltas <- fmap concat . forM commits $ \(commit, earlierCommit) -> - map (earlierCommit,) . (: []) . (.fileDeltas) + map ((commit, earlierCommit),) . (: []) . (.fileDeltas) <$> Git.diffOf earlierCommit commit pure Plan { steps = - NE.fromList (map (uncurry PlanStep) fileDeltas), + NE.fromList + ( map + ( \((commit, earlierCommit), changes) -> + PlanStep {..} + ) + fileDeltas + ), .. } -reviewPatch :: D.FileDeltas -> IO D.FileDeltas -reviewPatch fileDeltas = +reviewStep :: PlanStep -> IO D.FileDeltas +reviewStep step = do + commitMessages <- + T.decodeUtf8 . LB.toStrict + <$> sh + ( proc + "git log %..%" + (Git.toTextUnsafe step.earlierCommit) + (Git.toTextUnsafe step.commit) + ) + separateReview step.earlierCommit step.changes + =<< reviewPatch commitMessages step.changes + +reviewPatch :: T.Text -> D.FileDeltas -> IO D.FileDeltas +reviewPatch commitMessages fileDeltas = withSystemTempDirectory "anissue" $ \tmp -> do let patchFile = tmp "a.patch" patchFile' = tmp "b.patch" - T.writeFile patchFile (renderAsText (A.Patch fileDeltas)) - T.writeFile patchFile' (renderAsText (A.Patch fileDeltas)) + patchContents = renderAsText (A.Patch fileDeltas) + T.writeFile patchFile patchContents + T.writeFile patchFile' (addCommitMessages <> patchContents) sh_ (proc "${EDITOR-vi} %" patchFile') T.writeFile patchFile' . (renderAsText . A.Patch) . addComments . ((.fileDeltas) . A.parse) + . stripCommitMessages =<< T.readFile patchFile' ((.fileDeltas) . A.parse . T.decodeUtf8 . LB.toStrict) <$> sh (proc "rediff % %" patchFile patchFile') + where + addCommitMessages = + T.unlines . map ("# " <>) . T.lines $ commitMessages + stripCommitMessages = + T.unlines . dropWhile ("# " `T.isPrefixOf`) . T.lines addComments :: D.FileDeltas -> D.FileDeltas addComments = -- cgit v1.2.3