From e7450765081e31341496a3f8ac91bda119b55f5a Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Wed, 13 Mar 2024 04:36:20 +0100 Subject: chore: drop `--granularity` for `--per-commit` --- app/Main.hs | 24 ++++++++--------- app/Patch.hs | 2 +- app/Review.hs | 82 +++++------------------------------------------------------ app/Status.hs | 6 ++--- 4 files changed, 20 insertions(+), 94 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a28f4a6..634e085 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,5 +1,5 @@ -- TODO Compute history from the top --- _ +-- -- Currently we are computing the history from the bottom (ie. earliest commit -- first). When computing history from the top, it might allow us to interrupt -- the process and present slightly inaccurate information earlier. @@ -343,7 +343,6 @@ import Options.Applicative qualified as O import Process (proc, sh_, textInput) import Render ((<<<)) import Render qualified as P -import Review qualified as R import Settings (Settings (..), readSettings) import Status qualified as S import System.Console.Terminal.Size qualified as Terminal @@ -432,7 +431,7 @@ data Command | Review { baseBranch :: T.Text, featureBranch :: T.Text, - granularity :: R.Granularity + perCommit :: Bool } | Search { pattern :: R.RE, @@ -499,7 +498,7 @@ reviewCmd = Review <$> baseBranchArg <*> featureBranchArg - <*> granularityArg + <*> perCommitArg baseBranchArg :: O.Parser T.Text baseBranchArg = @@ -514,14 +513,11 @@ featureBranchArg :: O.Parser T.Text featureBranchArg = O.strArgument (O.metavar "BRANCH_NAME" <> O.value "HEAD") -granularityArg :: O.Parser R.Granularity -granularityArg = - O.option - O.auto - ( O.long "granularity" - <> O.metavar "GRANULARITY" - <> O.help "Granularity of the review. One of `as-one`, `per-commit`, `per-file` or `per-hunk`. Default: `as-one`." - <> O.value R.AsOne +perCommitArg :: O.Parser Bool +perCommitArg = + O.switch + ( O.long "per-commit" + <> O.help "Review commits individually. (Default: review combined patches)" ) showCmd :: O.Parser Command @@ -599,13 +595,13 @@ main = do Options {colorize, noPager, width, command = Status} -> do status <- S.readStatus ".anissue/status" putDoc colorize noPager width status - Options {colorize, noPager, width, command = Review {baseBranch, featureBranch, granularity}} -> do + Options {colorize, noPager, width, command = Review {baseBranch, featureBranch, perCommit}} -> do sh_ "test -z $(git status --porcelain --untracked-files=no)" `catch` \(_ :: E.ProcessException) -> error "working directory not clean, aborting.." S.withReviewing (putDoc colorize noPager width) - granularity + perCommit baseBranch featureBranch ".anissue/status" diff --git a/app/Patch.hs b/app/Patch.hs index a301382..f170817 100644 --- a/app/Patch.hs +++ b/app/Patch.hs @@ -6,12 +6,12 @@ module Patch ) where -import Prettyprinter (pretty) import Control.Exception (throw) import Data.Binary (Binary (..)) import Data.Text qualified as T import Exception qualified as E import GHC.Generics (Generic) +import Prettyprinter (pretty) import Render ((<<<)) import Render qualified as P import Text.Diff.Extra () diff --git a/app/Review.hs b/app/Review.hs index e296cee..91f4baf 100644 --- a/app/Review.hs +++ b/app/Review.hs @@ -1,6 +1,5 @@ module Review ( Plan (..), - Granularity (..), PlanStep (..), formulatePlan, reviewPatch, @@ -12,8 +11,7 @@ import Control.Exception (SomeException, catch) import Control.Monad (ap, forM, forM_) import Data.Binary qualified as B import Data.ByteString.Lazy qualified as LB -import Data.Function (on, (&)) -import Data.List (groupBy, sortOn) +import Data.Function ((&)) import Data.List.NonEmpty qualified as NE import Data.List.NonEmpty.Zipper qualified as Z import Data.List.NonEmpty.Zipper.Extra () @@ -32,54 +30,16 @@ import System.IO.Temp (withSystemTempDirectory) import System.Process.Typed qualified as P import Text.Diff.Extra () import Text.Diff.Parse.Types qualified as D -import Text.Read (Lexeme (Ident, Symbol), choice, lexP, readPrec) data Plan = Plan { baseBranch :: BranchName, - granularity :: Granularity, + perCommit :: Bool, steps :: Z.Zipper PlanStep } deriving (Show, Generic, B.Binary) type BranchName = T.Text -data Granularity - = AsOne - | PerCommit - | PerFile - | PerHunk - deriving (Eq, Ord, Generic, B.Binary) - -instance Show Granularity where - show AsOne = "as-one" - show PerCommit = "per-commit" - show PerFile = "per-file" - show PerHunk = "per-hunk" - -instance Read Granularity where - readPrec = - choice - [ do - Ident "as" <- lexP - Symbol "-" <- lexP - Ident "one" <- lexP - pure AsOne, - do - Ident "per" <- lexP - Symbol "-" <- lexP - choice - [ do - Ident "commit" <- lexP - pure PerCommit, - do - Ident "file" <- lexP - pure PerFile, - do - Ident "hunk" <- lexP - pure PerHunk - ] - ] - data PlanStep = PlanStep { id :: [Int], commit :: Git.CommitHash, @@ -87,13 +47,13 @@ data PlanStep = PlanStep } deriving (Show, Generic, B.Binary) -formulatePlan :: Granularity -> T.Text -> T.Text -> IO Plan -formulatePlan granularity baseBranch featureBranch = do +formulatePlan :: Bool -> T.Text -> T.Text -> IO Plan +formulatePlan perCommit baseBranch featureBranch = do baseCommit <- Git.resolveRef baseBranch featureCommit <- Git.resolveRef featureBranch commits <- - if granularity >= PerCommit + if perCommit then do commits <- reverse <$> Git.getCommitsBetween baseCommit featureCommit @@ -103,12 +63,7 @@ formulatePlan granularity baseBranch featureBranch = do fileDeltas <- fmap concat . forM commits $ \(commit, earlierCommit) -> - map (earlierCommit,) - . if - | granularity >= PerHunk -> splitPerHunk - | granularity >= PerFile -> splitPerFile - | otherwise -> (: []) - . (.fileDeltas) + map (earlierCommit,) . (: []) . (.fileDeltas) <$> Git.diffOf earlierCommit commit pure @@ -119,31 +74,6 @@ formulatePlan granularity baseBranch featureBranch = do .. } -splitPerFile :: D.FileDeltas -> [D.FileDeltas] -splitPerFile = - groupBy - ((==) `on` (.fileDeltaSourceFile)) - . sortOn (.fileDeltaSourceFile) - -splitPerHunk :: D.FileDeltas -> [D.FileDeltas] -splitPerHunk = - concatMap - ( \fileDeltas -> - [ hunkToFileDeltas fileDelta hunk - | fileDelta <- fileDeltas, - let D.Hunks hunks = fileDelta.fileDeltaContent, - hunk <- hunks - ] - ) - . splitPerFile - -hunkToFileDeltas :: D.FileDelta -> D.Hunk -> D.FileDeltas -hunkToFileDeltas fileDelta hunk = - [ fileDelta - { D.fileDeltaContent = D.Hunks [hunk] - } - ] - reviewPatch :: D.FileDeltas -> IO D.FileDeltas reviewPatch fileDeltas = withSystemTempDirectory "anissue" $ \tmp -> do diff --git a/app/Status.hs b/app/Status.hs index 1a0fd4c..13effa4 100644 --- a/app/Status.hs +++ b/app/Status.hs @@ -76,19 +76,19 @@ type BranchName = T.Text withReviewing :: (forall a. Render a => a -> IO ()) -> - R.Granularity -> + Bool -> BranchName -> BranchName -> FilePath -> ReviewingT IO a -> IO a -withReviewing putDoc granularity baseBranch featureBranch fp action = do +withReviewing putDoc perCommit baseBranch featureBranch fp action = do (plan, changes) <- (Just <$> readStatus fp) `catch` (\(_ :: SomeException) -> pure Nothing) >>= \case Nothing -> do - plan <- R.formulatePlan granularity baseBranch featureBranch + plan <- R.formulatePlan perCommit baseBranch featureBranch let changes = [] writeStatus fp $ Reviewing plan changes pure (plan, changes) -- cgit v1.2.3