{-# LANGUAGE OverloadedStrings #-}

-- Copyright (C) 2010 Petr Rockai
--
-- Permission is hereby granted, free of charge, to any person
-- obtaining a copy of this software and associated documentation
-- files (the "Software"), to deal in the Software without
-- restriction, including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

-- |
-- Module      : Darcs.Patch.Annotate
-- Copyright   : 2010 Petr Rockai
-- License     : MIT
-- Maintainer  : darcs-devel@darcs.net
-- Stability   : experimental
-- Portability : portable

module Darcs.Patch.Annotate
    (
      annotateFile
    , annotateDirectory
    , format
    , machineFormat
    , AnnotateResult
    , Annotate(..)
    , AnnotateRP
    ) where

import Darcs.Prelude

import Control.Monad.State ( modify, modify', when, gets, State, execState )

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.Map as M
import qualified Data.Vector as V

import Data.Function ( on )
import Data.List( nub, groupBy )
import Data.Maybe( isJust, mapMaybe )

import qualified Darcs.Patch.Prim.FileUUID as FileUUID

import Darcs.Patch.Effect ( Effect(..) )
import Darcs.Patch.FromPrim ( PrimOf(..) )
import Darcs.Patch.Info ( PatchInfo(..), displayPatchInfo, piAuthor, makePatchname )
import Darcs.Patch.Invert ( Invert, invert )
import Darcs.Patch.Named ( patchcontents )
import Darcs.Patch.PatchInfoAnd( info, PatchInfoAnd, hopefully )
import Darcs.Patch.Prim.V1.Core ( Prim(..), DirPatchType(..), FilePatchType(..) )
import Darcs.Patch.TokenReplace ( annotateReplace )
import Darcs.Patch.Witnesses.Ordered

import Darcs.Util.Path ( AnchoredPath, movedirfilename, flatten )
import Darcs.Util.Printer( renderString )
import Darcs.Util.ByteString ( linesPS, decodeLocale )

data FileOrDirectory = File
                     | Directory
                       deriving (Int -> FileOrDirectory -> ShowS
[FileOrDirectory] -> ShowS
FileOrDirectory -> String
(Int -> FileOrDirectory -> ShowS)
-> (FileOrDirectory -> String)
-> ([FileOrDirectory] -> ShowS)
-> Show FileOrDirectory
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileOrDirectory] -> ShowS
$cshowList :: [FileOrDirectory] -> ShowS
show :: FileOrDirectory -> String
$cshow :: FileOrDirectory -> String
showsPrec :: Int -> FileOrDirectory -> ShowS
$cshowsPrec :: Int -> FileOrDirectory -> ShowS
Show, FileOrDirectory -> FileOrDirectory -> Bool
(FileOrDirectory -> FileOrDirectory -> Bool)
-> (FileOrDirectory -> FileOrDirectory -> Bool)
-> Eq FileOrDirectory
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileOrDirectory -> FileOrDirectory -> Bool
$c/= :: FileOrDirectory -> FileOrDirectory -> Bool
== :: FileOrDirectory -> FileOrDirectory -> Bool
$c== :: FileOrDirectory -> FileOrDirectory -> Bool
Eq)

type AnnotateResult = V.Vector (Maybe PatchInfo, B.ByteString)

data Content2 f g
  = FileContent (f (g B.ByteString))
  | DirContent (f (g AnchoredPath))

data Annotated2 f g = Annotated2
    { Annotated2 f g -> AnnotateResult
annotated     :: !AnnotateResult
    , Annotated2 f g -> Content2 f g
current       :: !(Content2 f g)
    , Annotated2 f g -> Maybe AnchoredPath
currentPath   :: (Maybe AnchoredPath)
    , Annotated2 f g -> PatchInfo
currentInfo   :: PatchInfo
    }

type Content = Content2 [] ((,) Int)
type Annotated = Annotated2 [] ((,) Int)

deriving instance Eq Content
deriving instance Show Content

deriving instance Eq Annotated
deriving instance Show Annotated

type AnnotatedM = State Annotated

class Annotate p where
  annotate :: p wX wY -> AnnotatedM ()

-- |This constraint expresses what is needed for a repo patch to
-- support the high-level interface to annotation
-- (currently annotateFile and annotateDirectory)
type AnnotateRP p = (Annotate (PrimOf p), Invert (PrimOf p), Effect p)

instance Annotate Prim where
  annotate :: Prim wX wY -> AnnotatedM ()
annotate (FP AnchoredPath
fn FilePatchType wX wY
fp) = case FilePatchType wX wY
fp of
    FilePatchType wX wY
RmFile -> do
      AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn (AnnotatedM () -> AnnotatedM ()) -> AnnotatedM () -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (\Annotated
s -> Annotated
s { currentPath :: Maybe AnchoredPath
currentPath = Maybe AnchoredPath
forall a. Maybe a
Nothing })
      ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
withDirectory (([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ())
-> ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ AnchoredPath -> [(Int, AnchoredPath)] -> AnnotatedM ()
updateDirectory AnchoredPath
fn
    FilePatchType wX wY
AddFile -> () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Hunk Int
off [ByteString]
o [ByteString]
n -> AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn (AnnotatedM () -> AnnotatedM ()) -> AnnotatedM () -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ ([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
withFile (([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ())
-> ([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \[(Int, ByteString)]
c -> do
      let remove :: Int
remove = [ByteString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
o
      let add :: Int
add = [ByteString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
n
      PatchInfo
i <- (Annotated -> PatchInfo) -> StateT Annotated Identity PatchInfo
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> PatchInfo
forall (f :: * -> *) (g :: * -> *). Annotated2 f g -> PatchInfo
currentInfo
      AnnotateResult
a <- (Annotated -> AnnotateResult)
-> StateT Annotated Identity AnnotateResult
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated
      -- NOTE patches are inverted and in inverse order
      (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' ((Annotated -> Annotated) -> AnnotatedM ())
-> (Annotated -> Annotated) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \Annotated
s ->
        -- NOTE subtract one from offset because darcs counts from one,
        -- whereas vectors and lists count from zero.
        let ([(Int, ByteString)]
to,[(Int, ByteString)]
from) = Int
-> [(Int, ByteString)]
-> ([(Int, ByteString)], [(Int, ByteString)])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int
offInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [(Int, ByteString)]
c
        in  Annotated
s { current :: Content
current = [(Int, ByteString)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g ByteString) -> Content2 f g
FileContent ([(Int, ByteString)] -> Content) -> [(Int, ByteString)] -> Content
forall a b. (a -> b) -> a -> b
$ ((Int, ByteString) -> (Int, ByteString))
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> (Int, ByteString)
forall a. (Int, a) -> (Int, a)
eval ([(Int, ByteString)] -> [(Int, ByteString)])
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> a -> b
$ [(Int, ByteString)]
to [(Int, ByteString)] -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. [a] -> [a] -> [a]
++ Int -> (Int, ByteString) -> [(Int, ByteString)]
forall a. Int -> a -> [a]
replicate Int
add (-Int
1, ByteString
B.empty) [(Int, ByteString)] -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. [a] -> [a] -> [a]
++ Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
drop Int
remove [(Int, ByteString)]
from
              , annotated :: AnnotateResult
annotated = PatchInfo
-> AnnotateResult -> [(Int, ByteString)] -> AnnotateResult
forall a t.
a
-> Vector (Maybe a, ByteString)
-> [(Int, t)]
-> Vector (Maybe a, ByteString)
merge PatchInfo
i AnnotateResult
a ([(Int, ByteString)] -> AnnotateResult)
-> [(Int, ByteString)] -> AnnotateResult
forall a b. (a -> b) -> a -> b
$ ((Int, ByteString) -> (Int, ByteString))
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> (Int, ByteString)
forall a. (Int, a) -> (Int, a)
eval ([(Int, ByteString)] -> [(Int, ByteString)])
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> a -> b
$ Int -> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. Int -> [a] -> [a]
take Int
remove ([(Int, ByteString)] -> [(Int, ByteString)])
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> a -> b
$ [(Int, ByteString)]
from
              }
    TokReplace String
t String
o String
n -> AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn (AnnotatedM () -> AnnotatedM ()) -> AnnotatedM () -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ ([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
withFile (([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ())
-> ([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \[(Int, ByteString)]
c -> do
      let test :: ByteString -> Bool
test = String -> ByteString -> ByteString -> ByteString -> Bool
annotateReplace String
t (String -> ByteString
BC.pack String
o) (String -> ByteString
BC.pack String
n)
      PatchInfo
i <- (Annotated -> PatchInfo) -> StateT Annotated Identity PatchInfo
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> PatchInfo
forall (f :: * -> *) (g :: * -> *). Annotated2 f g -> PatchInfo
currentInfo
      AnnotateResult
a <- (Annotated -> AnnotateResult)
-> StateT Annotated Identity AnnotateResult
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated
      (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' ((Annotated -> Annotated) -> AnnotatedM ())
-> (Annotated -> Annotated) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \Annotated
s -> Annotated
s
        { current :: Content
current = [(Int, ByteString)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g ByteString) -> Content2 f g
FileContent ([(Int, ByteString)] -> Content) -> [(Int, ByteString)] -> Content
forall a b. (a -> b) -> a -> b
$ ((Int, ByteString) -> (Int, ByteString))
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Int
ix,ByteString
b)->if ByteString -> Bool
test ByteString
b then (-Int
1,ByteString
B.empty) else (Int
ix,ByteString
b)) [(Int, ByteString)]
c
        , annotated :: AnnotateResult
annotated = PatchInfo
-> AnnotateResult -> [(Int, ByteString)] -> AnnotateResult
forall a t.
a
-> Vector (Maybe a, ByteString)
-> [(Int, t)]
-> Vector (Maybe a, ByteString)
merge PatchInfo
i AnnotateResult
a ([(Int, ByteString)] -> AnnotateResult)
-> [(Int, ByteString)] -> AnnotateResult
forall a b. (a -> b) -> a -> b
$ ((Int, ByteString) -> (Int, ByteString))
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, ByteString) -> (Int, ByteString)
forall a. (Int, a) -> (Int, a)
eval ([(Int, ByteString)] -> [(Int, ByteString)])
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> a -> b
$ ((Int, ByteString) -> Bool)
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a. (a -> Bool) -> [a] -> [a]
filter (ByteString -> Bool
test (ByteString -> Bool)
-> ((Int, ByteString) -> ByteString) -> (Int, ByteString) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, ByteString) -> ByteString
forall a b. (a, b) -> b
snd) ([(Int, ByteString)] -> [(Int, ByteString)])
-> [(Int, ByteString)] -> [(Int, ByteString)]
forall a b. (a -> b) -> a -> b
$ [(Int, ByteString)]
c
        }
    -- TODO what if the status of a file changed from text to binary?
    Binary ByteString
_ ByteString
_ -> AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn (AnnotatedM () -> AnnotatedM ()) -> AnnotatedM () -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ String -> AnnotatedM ()
forall a. HasCallStack => String -> a
error String
"annotate: can't handle binary changes"
  annotate (DP AnchoredPath
_ DirPatchType wX wY
AddDir) = () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  annotate (DP AnchoredPath
fn DirPatchType wX wY
RmDir) = ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
withDirectory (([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ())
-> ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \[(Int, AnchoredPath)]
c -> do
    AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn (AnnotatedM () -> AnnotatedM ()) -> AnnotatedM () -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (\Annotated
s -> Annotated
s { currentPath :: Maybe AnchoredPath
currentPath = Maybe AnchoredPath
forall a. Maybe a
Nothing })
    AnchoredPath -> [(Int, AnchoredPath)] -> AnnotatedM ()
updateDirectory AnchoredPath
fn [(Int, AnchoredPath)]
c
  annotate (Move AnchoredPath
fn AnchoredPath
fn') = do
    (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify' (\Annotated
s -> Annotated
s { currentPath :: Maybe AnchoredPath
currentPath = (AnchoredPath -> AnchoredPath)
-> Maybe AnchoredPath -> Maybe AnchoredPath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (AnchoredPath -> AnchoredPath -> AnchoredPath -> AnchoredPath
movedirfilename AnchoredPath
fn AnchoredPath
fn') (Annotated -> Maybe AnchoredPath
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> Maybe AnchoredPath
currentPath Annotated
s) })
    ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
withDirectory (([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ())
-> ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \[(Int, AnchoredPath)]
c -> do
      let fix :: (a, AnchoredPath) -> (a, AnchoredPath)
fix (a
i, AnchoredPath
x) = (a
i, AnchoredPath -> AnchoredPath -> AnchoredPath -> AnchoredPath
movedirfilename AnchoredPath
fn AnchoredPath
fn' AnchoredPath
x)
      (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((Annotated -> Annotated) -> AnnotatedM ())
-> (Annotated -> Annotated) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \Annotated
s -> Annotated
s { current :: Content
current = [(Int, AnchoredPath)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g AnchoredPath) -> Content2 f g
DirContent ([(Int, AnchoredPath)] -> Content)
-> [(Int, AnchoredPath)] -> Content
forall a b. (a -> b) -> a -> b
$ ((Int, AnchoredPath) -> (Int, AnchoredPath))
-> [(Int, AnchoredPath)] -> [(Int, AnchoredPath)]
forall a b. (a -> b) -> [a] -> [b]
map (Int, AnchoredPath) -> (Int, AnchoredPath)
forall a. (a, AnchoredPath) -> (a, AnchoredPath)
fix [(Int, AnchoredPath)]
c }
  annotate (ChangePref String
_ String
_ String
_) = () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

instance Annotate FileUUID.Prim where
  annotate :: Prim wX wY -> AnnotatedM ()
annotate Prim wX wY
_ = String -> AnnotatedM ()
forall a. HasCallStack => String -> a
error String
"annotate not implemented for FileUUID patches"

annotatePIAP :: AnnotateRP p => PatchInfoAnd rt p wX wY -> AnnotatedM ()
annotatePIAP :: PatchInfoAnd rt p wX wY -> AnnotatedM ()
annotatePIAP =
  [AnnotatedM ()] -> AnnotatedM ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ ([AnnotatedM ()] -> AnnotatedM ())
-> (PatchInfoAnd rt p wX wY -> [AnnotatedM ()])
-> PatchInfoAnd rt p wX wY
-> AnnotatedM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall wW wZ. PrimOf p wW wZ -> AnnotatedM ())
-> FL (PrimOf p) wY wX -> [AnnotatedM ()]
forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL forall wW wZ. PrimOf p wW wZ -> AnnotatedM ()
forall (p :: * -> * -> *) wX wY.
Annotate p =>
p wX wY -> AnnotatedM ()
annotate (FL (PrimOf p) wY wX -> [AnnotatedM ()])
-> (PatchInfoAnd rt p wX wY -> FL (PrimOf p) wY wX)
-> PatchInfoAnd rt p wX wY
-> [AnnotatedM ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FL (PrimOf p) wX wY -> FL (PrimOf p) wY wX
forall (p :: * -> * -> *) wX wY. Invert p => p wX wY -> p wY wX
invert (FL (PrimOf p) wX wY -> FL (PrimOf p) wY wX)
-> (PatchInfoAnd rt p wX wY -> FL (PrimOf p) wX wY)
-> PatchInfoAnd rt p wX wY
-> FL (PrimOf p) wY wX
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FL p wX wY -> FL (PrimOf p) wX wY
forall (p :: * -> * -> *) wX wY.
Effect p =>
p wX wY -> FL (PrimOf p) wX wY
effect (FL p wX wY -> FL (PrimOf p) wX wY)
-> (PatchInfoAnd rt p wX wY -> FL p wX wY)
-> PatchInfoAnd rt p wX wY
-> FL (PrimOf p) wX wY
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Named p wX wY -> FL p wX wY
forall (p :: * -> * -> *) wX wY. Named p wX wY -> FL p wX wY
patchcontents (Named p wX wY -> FL p wX wY)
-> (PatchInfoAnd rt p wX wY -> Named p wX wY)
-> PatchInfoAnd rt p wX wY
-> FL p wX wY
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchInfoAnd rt p wX wY -> Named p wX wY
forall (rt :: RepoType) (p :: * -> * -> *) wA wB.
PatchInfoAndG rt p wA wB -> p wA wB
hopefully

withDirectory :: ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
withDirectory :: ([(Int, AnchoredPath)] -> AnnotatedM ()) -> AnnotatedM ()
withDirectory [(Int, AnchoredPath)] -> AnnotatedM ()
actions = do
  Content
what <- (Annotated -> Content) -> StateT Annotated Identity Content
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> Content
forall (f :: * -> *) (g :: * -> *). Annotated2 f g -> Content2 f g
current
  case Content
what of
    DirContent [(Int, AnchoredPath)]
c -> [(Int, AnchoredPath)] -> AnnotatedM ()
actions [(Int, AnchoredPath)]
c
    FileContent [(Int, ByteString)]
_ -> () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

withFile :: ([(Int, B.ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
withFile :: ([(Int, ByteString)] -> AnnotatedM ()) -> AnnotatedM ()
withFile [(Int, ByteString)] -> AnnotatedM ()
actions = do
  Content
what <- (Annotated -> Content) -> StateT Annotated Identity Content
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> Content
forall (f :: * -> *) (g :: * -> *). Annotated2 f g -> Content2 f g
current
  case Content
what of
    FileContent [(Int, ByteString)]
c -> [(Int, ByteString)] -> AnnotatedM ()
actions [(Int, ByteString)]
c
    DirContent [(Int, AnchoredPath)]
_ -> () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

whenPathIs :: AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs :: AnchoredPath -> AnnotatedM () -> AnnotatedM ()
whenPathIs AnchoredPath
fn AnnotatedM ()
actions = do
  Maybe AnchoredPath
p <- (Annotated -> Maybe AnchoredPath)
-> StateT Annotated Identity (Maybe AnchoredPath)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets Annotated -> Maybe AnchoredPath
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> Maybe AnchoredPath
currentPath
  Bool -> AnnotatedM () -> AnnotatedM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe AnchoredPath
p Maybe AnchoredPath -> Maybe AnchoredPath -> Bool
forall a. Eq a => a -> a -> Bool
== AnchoredPath -> Maybe AnchoredPath
forall a. a -> Maybe a
Just AnchoredPath
fn) AnnotatedM ()
actions

eval :: (Int, a) -> (Int, a)
eval :: (Int, a) -> (Int, a)
eval (Int
i,a
b) = Int -> (Int, a) -> (Int, a)
seq Int
i ((Int, a) -> (Int, a)) -> (Int, a) -> (Int, a)
forall a b. (a -> b) -> a -> b
$ a -> (Int, a) -> (Int, a)
seq a
b ((Int, a) -> (Int, a)) -> (Int, a) -> (Int, a)
forall a b. (a -> b) -> a -> b
$ (Int
i,a
b)

merge :: a
      -> V.Vector (Maybe a, BC.ByteString)
      -> [(Int, t)]
      -> V.Vector (Maybe a, BC.ByteString)
merge :: a
-> Vector (Maybe a, ByteString)
-> [(Int, t)]
-> Vector (Maybe a, ByteString)
merge a
i Vector (Maybe a, ByteString)
a [(Int, t)]
l = Vector (Maybe a, ByteString)
a Vector (Maybe a, ByteString)
-> [(Int, (Maybe a, ByteString))] -> Vector (Maybe a, ByteString)
forall a. Vector a -> [(Int, a)] -> Vector a
V.// [ (Int
line, (a -> Maybe a
forall a. a -> Maybe a
Just a
i, ByteString
B.empty))
                     | (Int
line, t
_) <- [(Int, t)]
l, Int
line Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
line Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Vector (Maybe a, ByteString) -> Int
forall a. Vector a -> Int
V.length Vector (Maybe a, ByteString)
a]

updateDirectory :: AnchoredPath -> [(Int,AnchoredPath)] -> AnnotatedM ()
updateDirectory :: AnchoredPath -> [(Int, AnchoredPath)] -> AnnotatedM ()
updateDirectory AnchoredPath
path [(Int, AnchoredPath)]
files = do
    case ((Int, AnchoredPath) -> Bool)
-> [(Int, AnchoredPath)] -> [(Int, AnchoredPath)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((AnchoredPath -> AnchoredPath -> Bool
forall a. Eq a => a -> a -> Bool
==AnchoredPath
path) (AnchoredPath -> Bool)
-> ((Int, AnchoredPath) -> AnchoredPath)
-> (Int, AnchoredPath)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, AnchoredPath) -> AnchoredPath
forall a b. (a, b) -> b
snd) [(Int, AnchoredPath)]
files of
      [match :: (Int, AnchoredPath)
match@(Int
ident, AnchoredPath
_)] -> Int -> (Int, AnchoredPath) -> AnnotatedM ()
reannotate Int
ident (Int, AnchoredPath)
match
      [(Int, AnchoredPath)]
_ -> () -> AnnotatedM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    reannotate :: Int -> (Int, AnchoredPath) -> AnnotatedM ()
    reannotate :: Int -> (Int, AnchoredPath) -> AnnotatedM ()
reannotate Int
ident (Int, AnchoredPath)
match =
      (Annotated -> Annotated) -> AnnotatedM ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((Annotated -> Annotated) -> AnnotatedM ())
-> (Annotated -> Annotated) -> AnnotatedM ()
forall a b. (a -> b) -> a -> b
$ \Annotated
x -> Annotated
x { annotated :: AnnotateResult
annotated = Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated Annotated
x AnnotateResult
-> [(Int, (Maybe PatchInfo, ByteString))] -> AnnotateResult
forall a. Vector a -> [(Int, a)] -> Vector a
V.// [ (Int
ident, PatchInfo -> (Maybe PatchInfo, ByteString)
forall a. a -> (Maybe a, ByteString)
update (PatchInfo -> (Maybe PatchInfo, ByteString))
-> PatchInfo -> (Maybe PatchInfo, ByteString)
forall a b. (a -> b) -> a -> b
$ Annotated -> PatchInfo
forall (f :: * -> *) (g :: * -> *). Annotated2 f g -> PatchInfo
currentInfo Annotated
x) ]
                       , current :: Content
current = [(Int, AnchoredPath)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g AnchoredPath) -> Content2 f g
DirContent ([(Int, AnchoredPath)] -> Content)
-> [(Int, AnchoredPath)] -> Content
forall a b. (a -> b) -> a -> b
$ ((Int, AnchoredPath) -> Bool)
-> [(Int, AnchoredPath)] -> [(Int, AnchoredPath)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Int, AnchoredPath) -> (Int, AnchoredPath) -> Bool
forall a. Eq a => a -> a -> Bool
/= (Int, AnchoredPath)
match) [(Int, AnchoredPath)]
files }
    update :: a -> (Maybe a, ByteString)
update a
inf = (a -> Maybe a
forall a. a -> Maybe a
Just a
inf, AnchoredPath -> ByteString
flatten AnchoredPath
path)

complete :: Annotated -> Bool
complete :: Annotated -> Bool
complete Annotated
x = ((Maybe PatchInfo, ByteString) -> Bool) -> AnnotateResult -> Bool
forall a. (a -> Bool) -> Vector a -> Bool
V.all (Maybe PatchInfo -> Bool
forall a. Maybe a -> Bool
isJust (Maybe PatchInfo -> Bool)
-> ((Maybe PatchInfo, ByteString) -> Maybe PatchInfo)
-> (Maybe PatchInfo, ByteString)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe PatchInfo, ByteString) -> Maybe PatchInfo
forall a b. (a, b) -> a
fst) (AnnotateResult -> Bool) -> AnnotateResult -> Bool
forall a b. (a -> b) -> a -> b
$ Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated Annotated
x

annotate' :: AnnotateRP p
          => RL (PatchInfoAnd rt p) wX wY
          -> Annotated
          -> Annotated
annotate' :: RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
annotate' RL (PatchInfoAnd rt p) wX wY
NilRL Annotated
ann = Annotated
ann
annotate' (RL (PatchInfoAnd rt p) wX wY
ps :<: PatchInfoAnd rt p wY wY
p) Annotated
ann
    | Annotated -> Bool
complete Annotated
ann = Annotated
ann
    | Bool
otherwise = RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
annotate' RL (PatchInfoAnd rt p) wX wY
ps (Annotated -> Annotated) -> Annotated -> Annotated
forall a b. (a -> b) -> a -> b
$ AnnotatedM () -> Annotated -> Annotated
forall s a. State s a -> s -> s
execState (PatchInfoAnd rt p wY wY -> AnnotatedM ()
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
PatchInfoAnd rt p wX wY -> AnnotatedM ()
annotatePIAP PatchInfoAnd rt p wY wY
p) (Annotated
ann { currentInfo :: PatchInfo
currentInfo = PatchInfoAnd rt p wY wY -> PatchInfo
forall (rt :: RepoType) (p :: * -> * -> *) wA wB.
PatchInfoAndG rt p wA wB -> PatchInfo
info PatchInfoAnd rt p wY wY
p })

annotateFile :: AnnotateRP p
             => RL (PatchInfoAnd rt p) wX wY
             -> AnchoredPath
             -> B.ByteString
             -> AnnotateResult
annotateFile :: RL (PatchInfoAnd rt p) wX wY
-> AnchoredPath -> ByteString -> AnnotateResult
annotateFile RL (PatchInfoAnd rt p) wX wY
patches AnchoredPath
inipath ByteString
inicontent = Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated (Annotated -> AnnotateResult) -> Annotated -> AnnotateResult
forall a b. (a -> b) -> a -> b
$ RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
annotate' RL (PatchInfoAnd rt p) wX wY
patches Annotated
initial
  where
    initial :: Annotated
initial = Annotated2 :: forall (f :: * -> *) (g :: * -> *).
AnnotateResult
-> Content2 f g
-> Maybe AnchoredPath
-> PatchInfo
-> Annotated2 f g
Annotated2 { currentPath :: Maybe AnchoredPath
currentPath = AnchoredPath -> Maybe AnchoredPath
forall a. a -> Maybe a
Just AnchoredPath
inipath
                        , currentInfo :: PatchInfo
currentInfo = String -> PatchInfo
forall a. HasCallStack => String -> a
error String
"There is no currentInfo."
                        , current :: Content
current = [(Int, ByteString)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g ByteString) -> Content2 f g
FileContent ([(Int, ByteString)] -> Content) -> [(Int, ByteString)] -> Content
forall a b. (a -> b) -> a -> b
$ [Int] -> [ByteString] -> [(Int, ByteString)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] (ByteString -> [ByteString]
linesPS ByteString
inicontent)
                        , annotated :: AnnotateResult
annotated = Int -> (Maybe PatchInfo, ByteString) -> AnnotateResult
forall a. Int -> a -> Vector a
V.replicate ([ByteString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([ByteString] -> Int) -> [ByteString] -> Int
forall a b. (a -> b) -> a -> b
$ ByteString -> [ByteString]
breakLines ByteString
inicontent)
                                                      (Maybe PatchInfo
forall a. Maybe a
Nothing, ByteString
B.empty)
                        }

annotateDirectory :: AnnotateRP p
                  => RL (PatchInfoAnd rt p) wX wY
                  -> AnchoredPath
                  -> [AnchoredPath]
                  -> AnnotateResult
annotateDirectory :: RL (PatchInfoAnd rt p) wX wY
-> AnchoredPath -> [AnchoredPath] -> AnnotateResult
annotateDirectory RL (PatchInfoAnd rt p) wX wY
patches AnchoredPath
inipath [AnchoredPath]
inicontent = Annotated -> AnnotateResult
forall (f :: * -> *) (g :: * -> *).
Annotated2 f g -> AnnotateResult
annotated (Annotated -> AnnotateResult) -> Annotated -> AnnotateResult
forall a b. (a -> b) -> a -> b
$ RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
RL (PatchInfoAnd rt p) wX wY -> Annotated -> Annotated
annotate' RL (PatchInfoAnd rt p) wX wY
patches Annotated
initial
  where
    initial :: Annotated
initial = Annotated2 :: forall (f :: * -> *) (g :: * -> *).
AnnotateResult
-> Content2 f g
-> Maybe AnchoredPath
-> PatchInfo
-> Annotated2 f g
Annotated2 { currentPath :: Maybe AnchoredPath
currentPath = AnchoredPath -> Maybe AnchoredPath
forall a. a -> Maybe a
Just AnchoredPath
inipath
                        , currentInfo :: PatchInfo
currentInfo = String -> PatchInfo
forall a. HasCallStack => String -> a
error String
"There is no currentInfo."
                        , current :: Content
current = [(Int, AnchoredPath)] -> Content
forall (f :: * -> *) (g :: * -> *).
f (g AnchoredPath) -> Content2 f g
DirContent ([(Int, AnchoredPath)] -> Content)
-> [(Int, AnchoredPath)] -> Content
forall a b. (a -> b) -> a -> b
$ [Int] -> [AnchoredPath] -> [(Int, AnchoredPath)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [AnchoredPath]
inicontent
                        , annotated :: AnnotateResult
annotated = Int -> (Maybe PatchInfo, ByteString) -> AnnotateResult
forall a. Int -> a -> Vector a
V.replicate ([AnchoredPath] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AnchoredPath]
inicontent) (Maybe PatchInfo
forall a. Maybe a
Nothing, ByteString
B.empty)
                        }

machineFormat :: B.ByteString -> AnnotateResult -> String
machineFormat :: ByteString -> AnnotateResult -> String
machineFormat ByteString
d AnnotateResult
a = [String] -> String
unlines [ case Maybe PatchInfo
i of
                                 Just PatchInfo
inf -> SHA1 -> String
forall a. Show a => a -> String
show (SHA1 -> String) -> SHA1 -> String
forall a b. (a -> b) -> a -> b
$ PatchInfo -> SHA1
makePatchname PatchInfo
inf
                                 Maybe PatchInfo
Nothing -> -- make unknowns uniform, for easier parsing
                                   Int -> ShowS
forall a. Int -> [a] -> [a]
take Int
40 ( Char -> String
forall a. a -> [a]
repeat Char
'0' ) -- fake hash of the right size
                              String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" | " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
BC.unpack ByteString
line String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteString -> String
BC.unpack ByteString
add
                            | ((Maybe PatchInfo
i, ByteString
add), ByteString
line) <- [(Maybe PatchInfo, ByteString)]
-> [ByteString] -> [((Maybe PatchInfo, ByteString), ByteString)]
forall a b. [a] -> [b] -> [(a, b)]
zip (AnnotateResult -> [(Maybe PatchInfo, ByteString)]
forall a. Vector a -> [a]
V.toList AnnotateResult
a) (ByteString -> [ByteString]
breakLines ByteString
d) ]

format :: B.ByteString -> AnnotateResult -> String
format :: ByteString -> AnnotateResult -> String
format ByteString
d AnnotateResult
a = String
pi_list String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
numbered
  where
    numberedLines :: [(Int, String)]
numberedLines = [Int] -> [String] -> [(Int, String)]
forall a b. [a] -> [b] -> [(a, b)]
zip [(Int
1 :: Int)..] ([String] -> [(Int, String)])
-> (String -> [String]) -> String -> [(Int, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines (String -> [(Int, String)]) -> String -> [(Int, String)]
forall a b. (a -> b) -> a -> b
$ String
file

    prependNum :: (a, String) -> String
prependNum (a
lnum, String
annLine) =
        let maxDigits :: Int
maxDigits = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (String -> Int)
-> ([(Int, String)] -> String) -> [(Int, String)] -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show (Int -> String)
-> ([(Int, String)] -> Int) -> [(Int, String)] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, String)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([(Int, String)] -> Int) -> [(Int, String)] -> Int
forall a b. (a -> b) -> a -> b
$ [(Int, String)]
numberedLines
            lnumStr :: String
lnumStr = a -> String
forall a. Show a => a -> String
show a
lnum
            paddingNum :: Int
paddingNum = Int
maxDigits Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
lnumStr
        in Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
paddingNum Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
lnumStr String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
annLine

    numbered :: String
numbered = [String] -> String
unlines ([String] -> String)
-> ([(Int, String)] -> [String]) -> [(Int, String)] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, String) -> String) -> [(Int, String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (Int, String) -> String
forall a. Show a => (a, String) -> String
prependNum ([(Int, String)] -> String) -> [(Int, String)] -> String
forall a b. (a -> b) -> a -> b
$ [(Int, String)]
numberedLines

    pi_list :: String
pi_list = [String] -> String
unlines [ Int -> String
forall a. Show a => a -> String
show Int
n String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Doc -> String
renderString (PatchInfo -> Doc
displayPatchInfo PatchInfo
i)
                      | (Int
n :: Int, PatchInfo
i) <- [Int] -> [PatchInfo] -> [(Int, PatchInfo)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] [PatchInfo]
pis ]

    file :: String
file = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ (Maybe PatchInfo, ByteString) -> String
forall b. (Maybe PatchInfo, b) -> String
annotation (((Maybe PatchInfo, ByteString), ByteString)
-> (Maybe PatchInfo, ByteString)
forall a b. (a, b) -> a
fst (((Maybe PatchInfo, ByteString), ByteString)
 -> (Maybe PatchInfo, ByteString))
-> ((Maybe PatchInfo, ByteString), ByteString)
-> (Maybe PatchInfo, ByteString)
forall a b. (a -> b) -> a -> b
$ [((Maybe PatchInfo, ByteString), ByteString)]
-> ((Maybe PatchInfo, ByteString), ByteString)
forall a. [a] -> a
head [((Maybe PatchInfo, ByteString), ByteString)]
chunk) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" | " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ((Maybe PatchInfo, ByteString), ByteString) -> String
forall a. ((a, ByteString), ByteString) -> String
line ([((Maybe PatchInfo, ByteString), ByteString)]
-> ((Maybe PatchInfo, ByteString), ByteString)
forall a. [a] -> a
head [((Maybe PatchInfo, ByteString), ByteString)]
chunk) String -> ShowS
forall a. [a] -> [a] -> [a]
++
                    String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
unlines [ Int -> ShowS
indent Int
25 (String
" | " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ((Maybe PatchInfo, ByteString), ByteString) -> String
forall a. ((a, ByteString), ByteString) -> String
line ((Maybe PatchInfo, ByteString), ByteString)
l) | ((Maybe PatchInfo, ByteString), ByteString)
l <- [((Maybe PatchInfo, ByteString), ByteString)]
-> [((Maybe PatchInfo, ByteString), ByteString)]
forall a. [a] -> [a]
tail [((Maybe PatchInfo, ByteString), ByteString)]
chunk ]
                  | [((Maybe PatchInfo, ByteString), ByteString)]
chunk <- [[((Maybe PatchInfo, ByteString), ByteString)]]
file_ann ]

    pis :: [PatchInfo]
pis = [PatchInfo] -> [PatchInfo]
forall a. Eq a => [a] -> [a]
nub ([PatchInfo] -> [PatchInfo]) -> [PatchInfo] -> [PatchInfo]
forall a b. (a -> b) -> a -> b
$ ((Maybe PatchInfo, ByteString) -> Maybe PatchInfo)
-> [(Maybe PatchInfo, ByteString)] -> [PatchInfo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Maybe PatchInfo, ByteString) -> Maybe PatchInfo
forall a b. (a, b) -> a
fst ([(Maybe PatchInfo, ByteString)] -> [PatchInfo])
-> [(Maybe PatchInfo, ByteString)] -> [PatchInfo]
forall a b. (a -> b) -> a -> b
$ AnnotateResult -> [(Maybe PatchInfo, ByteString)]
forall a. Vector a -> [a]
V.toList AnnotateResult
a

    pi_map :: Map PatchInfo Int
pi_map = [(PatchInfo, Int)] -> Map PatchInfo Int
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([PatchInfo] -> [Int] -> [(PatchInfo, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [PatchInfo]
pis [Int
1 :: Int ..])

    file_ann :: [[((Maybe PatchInfo, ByteString), ByteString)]]
file_ann = (((Maybe PatchInfo, ByteString), ByteString)
 -> ((Maybe PatchInfo, ByteString), ByteString) -> Bool)
-> [((Maybe PatchInfo, ByteString), ByteString)]
-> [[((Maybe PatchInfo, ByteString), ByteString)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy ((Maybe PatchInfo, ByteString)
-> (Maybe PatchInfo, ByteString) -> Bool
forall a. Eq a => a -> a -> Bool
(==) ((Maybe PatchInfo, ByteString)
 -> (Maybe PatchInfo, ByteString) -> Bool)
-> (((Maybe PatchInfo, ByteString), ByteString)
    -> (Maybe PatchInfo, ByteString))
-> ((Maybe PatchInfo, ByteString), ByteString)
-> ((Maybe PatchInfo, ByteString), ByteString)
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` ((Maybe PatchInfo, ByteString), ByteString)
-> (Maybe PatchInfo, ByteString)
forall a b. (a, b) -> a
fst) ([((Maybe PatchInfo, ByteString), ByteString)]
 -> [[((Maybe PatchInfo, ByteString), ByteString)]])
-> [((Maybe PatchInfo, ByteString), ByteString)]
-> [[((Maybe PatchInfo, ByteString), ByteString)]]
forall a b. (a -> b) -> a -> b
$ [(Maybe PatchInfo, ByteString)]
-> [ByteString] -> [((Maybe PatchInfo, ByteString), ByteString)]
forall a b. [a] -> [b] -> [(a, b)]
zip (AnnotateResult -> [(Maybe PatchInfo, ByteString)]
forall a. Vector a -> [a]
V.toList AnnotateResult
a) (ByteString -> [ByteString]
breakLines ByteString
d)

    line :: ((a, ByteString), ByteString) -> String
line ((a
_, ByteString
add), ByteString
l) = ByteString -> String
decodeLocale (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
BC.concat [ByteString
l, ByteString
" ", ByteString
add]

    annotation :: (Maybe PatchInfo, b) -> String
annotation (Just PatchInfo
i, b
_) | Just Int
n <- PatchInfo -> Map PatchInfo Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup PatchInfo
i Map PatchInfo Int
pi_map =
        Int -> ShowS
pad Int
20 (PatchInfo -> String
piMail PatchInfo
i) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
pad Int
4 (Char
'#' Char -> ShowS
forall a. a -> [a] -> [a]
: Int -> String
forall a. Show a => a -> String
show Int
n)
    annotation (Maybe PatchInfo, b)
_ = Int -> ShowS
pad Int
25 String
"unknown"

    pad :: Int -> ShowS
pad Int
n String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str) Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> ShowS
forall a. Int -> [a] -> [a]
take Int
n String
str

    indent :: Int -> ShowS
indent Int
n String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
n Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str

    piMail :: PatchInfo -> String
piMail PatchInfo
pi
        | Char
'<' Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` PatchInfo -> String
piAuthor PatchInfo
pi = (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'>') ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Int -> [a] -> [a]
drop Int
1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'<') ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ PatchInfo -> String
piAuthor PatchInfo
pi
        | Bool
otherwise = PatchInfo -> String
piAuthor PatchInfo
pi

breakLines :: BC.ByteString -> [BC.ByteString]
breakLines :: ByteString -> [ByteString]
breakLines ByteString
s = case Char -> ByteString -> [ByteString]
BC.split Char
'\n' ByteString
s of
    [] -> []
    [ByteString]
split | ByteString -> Bool
BC.null ([ByteString] -> ByteString
forall a. [a] -> a
last [ByteString]
split) -> [ByteString] -> [ByteString]
forall a. [a] -> [a]
init [ByteString]
split
          | Bool
otherwise -> [ByteString]
split