--  Copyright (C) 2003 David Roundy, 2010-2011 Petr Rockai
--
--  This program is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2, or (at your option)
--  any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program; see the file COPYING.  If not, write to
--  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
--  Boston, MA 02110-1301, USA.
{-# LANGUAGE OverloadedStrings #-}

module Darcs.UI.Commands.Annotate ( annotate ) where

import Darcs.Prelude

import Control.Monad ( when )

import Darcs.UI.Commands ( DarcsCommand(..), withStdOpts, nodefaults, amInHashedRepository )
import Darcs.UI.Completion ( knownFileArgs )
import Darcs.UI.External ( viewDocWith )
import Darcs.UI.Flags ( DarcsFlag, useCache, patchIndexYes, pathsFromArgs )
import Darcs.UI.Options ( (^), odesc, ocheck
                        , defaultFlags, parseFlags, (?) )
import qualified Darcs.UI.Options.All as O

import Darcs.Repository.State ( readRecorded )
import Darcs.Repository
    ( withRepository
    , withRepoLockCanFail
    , RepoJob(..)
    , readRepo
    )
import Darcs.Repository.PatchIndex ( attemptCreatePatchIndex )
import Darcs.Patch.Set ( patchSet2RL )
import qualified Data.ByteString.Char8 as BC ( pack, concat, intercalate )
import Data.ByteString.Lazy ( toChunks )
import Darcs.Patch.ApplyMonad( withFileNames )
import Darcs.Patch.Match ( patchSetMatch, rollbackToPatchSetMatch  )
import Darcs.Repository.Match ( getOnePatchset )
import Darcs.Repository.PatchIndex ( getRelevantSubsequence, canUsePatchIndex )
import Darcs.Patch.Witnesses.Sealed ( Sealed(..), seal )
import qualified Darcs.Patch.Annotate as A

import Darcs.Util.Tree( TreeItem(..) )
import qualified Darcs.Util.Tree as T ( readBlob, list, expand )
import Darcs.Util.Tree.Monad( findM, virtualTreeIO )
import Darcs.Util.Path( AbsolutePath, AnchoredPath, displayPath, catPaths )
import Darcs.Util.Printer ( Doc, simplePrinters, renderString, text )
import Darcs.Util.Exception ( die )

annotateDescription :: String
annotateDescription :: String
annotateDescription = String
"Annotate lines of a file with the last patch that modified it."

annotateHelp :: Doc
annotateHelp :: Doc
annotateHelp = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
 [ String
"When `darcs annotate` is called on a file, it will find the patch that"
 , String
"last modified each line in that file. This also works on directories."
 , String
""
 , String
"The `--machine-readable` option can be used to generate output for"
 , String
"machine postprocessing."
 ]

annotate :: DarcsCommand
annotate :: DarcsCommand
annotate = DarcsCommand :: String
-> String
-> Doc
-> String
-> Int
-> [String]
-> ((AbsolutePath, AbsolutePath)
    -> [DarcsFlag] -> [String] -> IO ())
-> ([DarcsFlag] -> IO (Either String ()))
-> ((AbsolutePath, AbsolutePath)
    -> [DarcsFlag] -> [String] -> IO [String])
-> ([DarcsFlag] -> AbsolutePath -> [String] -> IO [String])
-> [DarcsOptDescr DarcsFlag]
-> [DarcsOptDescr DarcsFlag]
-> [DarcsFlag]
-> ([DarcsFlag] -> [String])
-> DarcsCommand
DarcsCommand
    { commandProgramName :: String
commandProgramName = String
"darcs"
    , commandName :: String
commandName = String
"annotate"
    , commandHelp :: Doc
commandHelp = Doc
annotateHelp
    , commandDescription :: String
commandDescription = String
annotateDescription
    , commandExtraArgs :: Int
commandExtraArgs = Int
1
    , commandExtraArgHelp :: [String]
commandExtraArgHelp = [String
"[FILE or DIRECTORY]"]
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
commandCommand = (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
annotateCmd
    , commandPrereq :: [DarcsFlag] -> IO (Either String ())
commandPrereq = [DarcsFlag] -> IO (Either String ())
amInHashedRepository
    , commandCompleteArgs :: (AbsolutePath, AbsolutePath)
-> [DarcsFlag] -> [String] -> IO [String]
commandCompleteArgs = (AbsolutePath, AbsolutePath)
-> [DarcsFlag] -> [String] -> IO [String]
knownFileArgs
    , commandArgdefaults :: [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
commandArgdefaults = [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
nodefaults
    , commandAdvancedOptions :: [DarcsOptDescr DarcsFlag]
commandAdvancedOptions = OptSpec DarcsOptDescr DarcsFlag Any (WithPatchIndex -> Any)
-> [DarcsOptDescr DarcsFlag]
forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc OptSpec DarcsOptDescr DarcsFlag Any (WithPatchIndex -> Any)
forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
annotateAdvancedOpts
    , commandBasicOptions :: [DarcsOptDescr DarcsFlag]
commandBasicOptions = OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Bool -> [MatchFlag] -> Maybe String -> Any)
-> [DarcsOptDescr DarcsFlag]
forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Bool -> [MatchFlag] -> Maybe String -> Any)
forall a.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Bool -> [MatchFlag] -> Maybe String -> a)
annotateBasicOpts
    , commandDefaults :: [DarcsFlag]
commandDefaults = OptSpec
  DarcsOptDescr
  DarcsFlag
  [DarcsFlag]
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> [DarcsFlag])
-> [DarcsFlag]
forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags OptSpec
  DarcsOptDescr
  DarcsFlag
  [DarcsFlag]
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> [DarcsFlag])
forall a.
DarcsOption
  a
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
annotateOpts
    , commandCheckOptions :: [DarcsFlag] -> [String]
commandCheckOptions = OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> Any)
-> [DarcsFlag] -> [String]
forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [String]
ocheck OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> Any)
forall a.
DarcsOption
  a
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
annotateOpts
    }
  where
    annotateBasicOpts :: OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Bool -> [MatchFlag] -> Maybe String -> a)
annotateBasicOpts = PrimOptSpec
  DarcsOptDescr DarcsFlag ([MatchFlag] -> Maybe String -> a) Bool
PrimDarcsOption Bool
O.machineReadable PrimOptSpec
  DarcsOptDescr DarcsFlag ([MatchFlag] -> Maybe String -> a) Bool
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Maybe String -> a)
     ([MatchFlag] -> Maybe String -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Maybe String -> a)
     (Bool -> [MatchFlag] -> Maybe String -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe String -> a)
  ([MatchFlag] -> Maybe String -> a)
MatchOption
O.matchUpToOne OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe String -> a)
  (Bool -> [MatchFlag] -> Maybe String -> a)
-> OptSpec DarcsOptDescr DarcsFlag a (Maybe String -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     a
     (Bool -> [MatchFlag] -> Maybe String -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec DarcsOptDescr DarcsFlag a (Maybe String -> a)
PrimDarcsOption (Maybe String)
O.repoDir
    annotateAdvancedOpts :: PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
annotateAdvancedOpts = PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
O.patchIndexYes
    annotateOpts :: DarcsOption
  a
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
annotateOpts = OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
forall a.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Bool -> [MatchFlag] -> Maybe String -> a)
annotateBasicOpts OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
  (Bool
   -> [MatchFlag]
   -> Maybe String
   -> Maybe StdCmdAction
   -> Verbosity
   -> WithPatchIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
     (WithPatchIndex
      -> UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
-> DarcsOption
     a
     (Bool
      -> [MatchFlag]
      -> Maybe String
      -> Maybe StdCmdAction
      -> Verbosity
      -> WithPatchIndex
      -> UseCache
      -> HooksConfig
      -> Bool
      -> Bool
      -> Bool
      -> a)
forall b c a.
DarcsOption (Maybe StdCmdAction -> Verbosity -> b) c
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a) b
-> DarcsOption a c
`withStdOpts` DarcsOption
  (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
  (WithPatchIndex
   -> UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
annotateAdvancedOpts

annotateCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
annotateCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
annotateCmd (AbsolutePath, AbsolutePath)
fps [DarcsFlag]
opts [String]
args = do
  [AnchoredPath]
paths <- (AbsolutePath, AbsolutePath) -> [String] -> IO [AnchoredPath]
pathsFromArgs (AbsolutePath, AbsolutePath)
fps [String]
args
  case [AnchoredPath]
paths of
    [AnchoredPath
path] -> do
      Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
patchIndexYes (forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex)
-> [DarcsFlag] -> WithPatchIndex
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts WithPatchIndex -> WithPatchIndex -> Bool
forall a. Eq a => a -> a -> Bool
== WithPatchIndex
O.YesPatchIndex)
        (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ UseCache -> RepoJob () -> IO ()
withRepoLockCanFail (PrimDarcsOption UseCache
useCache PrimDarcsOption UseCache -> [DarcsFlag] -> UseCache
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)
        (RepoJob () -> IO ()) -> RepoJob () -> IO ()
forall a b. (a -> b) -> a -> b
$ (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO ())
-> RepoJob ()
forall a.
(forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
RepoJob (\Repository rt p wR wU wR
repo -> Repository rt p wR wU wR -> IO (PatchSet rt p Origin wR)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
(IsRepoType rt, RepoPatch p) =>
Repository rt p wR wU wT -> IO (PatchSet rt p Origin wR)
readRepo Repository rt p wR wU wR
repo IO (PatchSet rt p Origin wR)
-> (PatchSet rt p Origin wR -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Repository rt p wR wU wR -> PatchSet rt p Origin wR -> IO ()
forall (p :: * -> * -> *) (rt :: RepoType) wR wU wT.
(RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wT -> PatchSet rt p Origin wR -> IO ()
attemptCreatePatchIndex Repository rt p wR wU wR
repo)
      [DarcsFlag] -> AnchoredPath -> IO ()
annotateCmd' [DarcsFlag]
opts AnchoredPath
path
    [AnchoredPath]
_ -> String -> IO ()
forall a. String -> IO a
die String
"Error: annotate requires a single filepath argument"

annotateCmd' :: [DarcsFlag] -> AnchoredPath -> IO ()
annotateCmd' :: [DarcsFlag] -> AnchoredPath -> IO ()
annotateCmd' [DarcsFlag]
opts AnchoredPath
fixed_path = UseCache -> RepoJob () -> IO ()
forall a. UseCache -> RepoJob a -> IO a
withRepository (PrimDarcsOption UseCache
useCache PrimDarcsOption UseCache -> [DarcsFlag] -> UseCache
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) (RepoJob () -> IO ()) -> RepoJob () -> IO ()
forall a b. (a -> b) -> a -> b
$ (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO ())
-> RepoJob ()
forall a.
(forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
RepoJob ((forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
  (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
  Repository rt p wR wU wR -> IO ())
 -> RepoJob ())
-> (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
    (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
    Repository rt p wR wU wR -> IO ())
-> RepoJob ()
forall a b. (a -> b) -> a -> b
$ \Repository rt p wR wU wR
repository -> do
  let matchFlags :: [MatchFlag]
matchFlags = MatchOption -> [DarcsFlag] -> [MatchFlag]
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags MatchOption
O.matchUpToOne [DarcsFlag]
opts
  PatchSet rt p Origin wR
r <- Repository rt p wR wU wR -> IO (PatchSet rt p Origin wR)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
(IsRepoType rt, RepoPatch p) =>
Repository rt p wR wU wT -> IO (PatchSet rt p Origin wR)
readRepo Repository rt p wR wU wR
repository
  Tree IO
recorded <- Repository rt p wR wU wR -> IO (Tree IO)
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> IO (Tree IO)
readRecorded Repository rt p wR wU wR
repository
  (Sealed (RL (PatchInfoAnd rt p) Origin)
patches, Tree IO
initial, AnchoredPath
path) <-
    case [MatchFlag] -> Maybe PatchSetMatch
patchSetMatch [MatchFlag]
matchFlags of
      Just PatchSetMatch
psm -> do
        Sealed PatchSet rt p Origin wX
x <- Repository rt p wR wU wR
-> PatchSetMatch -> IO (Sealed (PatchSet rt p Origin))
forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
(IsRepoType rt, RepoPatch p) =>
Repository rt p wR wU wR
-> PatchSetMatch -> IO (SealedPatchSet rt p Origin)
getOnePatchset Repository rt p wR wU wR
repository PatchSetMatch
psm
        let ([AnchoredPath]
_, [AnchoredPath
path'], [OrigFileNameOf]
_) =
              Maybe [OrigFileNameOf]
-> [AnchoredPath]
-> FilePathMonad ()
-> ([AnchoredPath], [AnchoredPath], [OrigFileNameOf])
forall a.
Maybe [OrigFileNameOf]
-> [AnchoredPath]
-> FilePathMonad a
-> ([AnchoredPath], [AnchoredPath], [OrigFileNameOf])
withFileNames Maybe [OrigFileNameOf]
forall a. Maybe a
Nothing [AnchoredPath
fixed_path] (PatchSetMatch -> PatchSet rt p Origin wR -> FilePathMonad ()
forall (p :: * -> * -> *) (m :: * -> *) (rt :: RepoType) wX.
(ApplyMonad (ApplyState p) m, IsRepoType rt, MatchableRP p,
 ApplyState p ~ Tree) =>
PatchSetMatch -> PatchSet rt p Origin wX -> m ()
rollbackToPatchSetMatch PatchSetMatch
psm PatchSet rt p Origin wR
r)
        Tree IO
initial <- ((), Tree IO) -> Tree IO
forall a b. (a, b) -> b
snd (((), Tree IO) -> Tree IO) -> IO ((), Tree IO) -> IO (Tree IO)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` TreeIO () -> Tree IO -> IO ((), Tree IO)
forall a. TreeIO a -> Tree IO -> IO (a, Tree IO)
virtualTreeIO (PatchSetMatch -> PatchSet rt p Origin wR -> TreeIO ()
forall (p :: * -> * -> *) (m :: * -> *) (rt :: RepoType) wX.
(ApplyMonad (ApplyState p) m, IsRepoType rt, MatchableRP p,
 ApplyState p ~ Tree) =>
PatchSetMatch -> PatchSet rt p Origin wX -> m ()
rollbackToPatchSetMatch PatchSetMatch
psm PatchSet rt p Origin wR
r) Tree IO
recorded
        (Sealed (RL (PatchInfoAnd rt p) Origin), Tree IO, AnchoredPath)
-> IO
     (Sealed (RL (PatchInfoAnd rt p) Origin), Tree IO, AnchoredPath)
forall (m :: * -> *) a. Monad m => a -> m a
return (RL (PatchInfoAnd rt p) Origin wX
-> Sealed (RL (PatchInfoAnd rt p) Origin)
forall (a :: * -> *) wX. a wX -> Sealed a
seal (RL (PatchInfoAnd rt p) Origin wX
 -> Sealed (RL (PatchInfoAnd rt p) Origin))
-> RL (PatchInfoAnd rt p) Origin wX
-> Sealed (RL (PatchInfoAnd rt p) Origin)
forall a b. (a -> b) -> a -> b
$ PatchSet rt p Origin wX -> RL (PatchInfoAnd rt p) Origin wX
forall (rt :: RepoType) (p :: * -> * -> *) wStart wX.
PatchSet rt p wStart wX -> RL (PatchInfoAnd rt p) wStart wX
patchSet2RL PatchSet rt p Origin wX
x, Tree IO
initial, AnchoredPath
path')
      Maybe PatchSetMatch
Nothing ->
        (Sealed (RL (PatchInfoAnd rt p) Origin), Tree IO, AnchoredPath)
-> IO
     (Sealed (RL (PatchInfoAnd rt p) Origin), Tree IO, AnchoredPath)
forall (m :: * -> *) a. Monad m => a -> m a
return (RL (PatchInfoAnd rt p) Origin wR
-> Sealed (RL (PatchInfoAnd rt p) Origin)
forall (a :: * -> *) wX. a wX -> Sealed a
seal (RL (PatchInfoAnd rt p) Origin wR
 -> Sealed (RL (PatchInfoAnd rt p) Origin))
-> RL (PatchInfoAnd rt p) Origin wR
-> Sealed (RL (PatchInfoAnd rt p) Origin)
forall a b. (a -> b) -> a -> b
$ PatchSet rt p Origin wR -> RL (PatchInfoAnd rt p) Origin wR
forall (rt :: RepoType) (p :: * -> * -> *) wStart wX.
PatchSet rt p wStart wX -> RL (PatchInfoAnd rt p) wStart wX
patchSet2RL PatchSet rt p Origin wR
r, Tree IO
recorded, AnchoredPath
fixed_path)

  Maybe (TreeItem IO)
found <- Tree IO -> AnchoredPath -> IO (Maybe (TreeItem IO))
forall (m :: * -> *).
Monad m =>
Tree m -> AnchoredPath -> m (Maybe (TreeItem m))
findM Tree IO
initial AnchoredPath
path
  -- TODO need to decide about the --machine flag
  let (ByteString -> AnnotateResult -> String
fmt, Doc -> IO ()
view) = if PrimDarcsOption Bool -> [DarcsFlag] -> Bool
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags PrimDarcsOption Bool
O.machineReadable [DarcsFlag]
opts
                      then (ByteString -> AnnotateResult -> String
A.machineFormat, String -> IO ()
putStrLn (String -> IO ()) -> (Doc -> String) -> Doc -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> String
renderString)
                      else (ByteString -> AnnotateResult -> String
A.format, Printers -> Doc -> IO ()
viewDocWith Printers
simplePrinters)
  Bool
usePatchIndex <- (WithPatchIndex -> Bool
forall a. YesNo a => a -> Bool
O.yes (forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex
O.patchIndexYes (forall a. PrimOptSpec DarcsOptDescr DarcsFlag a WithPatchIndex)
-> [DarcsFlag] -> WithPatchIndex
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) Bool -> Bool -> Bool
&&) (Bool -> Bool) -> IO Bool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Repository rt p wR wU wR -> IO Bool
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> IO Bool
canUsePatchIndex Repository rt p wR wU wR
repository
  case Maybe (TreeItem IO)
found of
    Maybe (TreeItem IO)
Nothing -> String -> IO ()
forall a. String -> IO a
die (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"Error: path not found in repository: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ AnchoredPath -> String
displayPath AnchoredPath
fixed_path
    Just (SubTree Tree IO
s) -> do
      -- TODO the semantics and implementation of annotating of directories need to be revised
      Tree IO
s' <- Tree IO -> IO (Tree IO)
forall (m :: * -> *). Monad m => Tree m -> m (Tree m)
T.expand Tree IO
s
      let subs :: [AnchoredPath]
subs = ((AnchoredPath, TreeItem IO) -> AnchoredPath)
-> [(AnchoredPath, TreeItem IO)] -> [AnchoredPath]
forall a b. (a -> b) -> [a] -> [b]
map (AnchoredPath -> AnchoredPath -> AnchoredPath
catPaths AnchoredPath
path (AnchoredPath -> AnchoredPath)
-> ((AnchoredPath, TreeItem IO) -> AnchoredPath)
-> (AnchoredPath, TreeItem IO)
-> AnchoredPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AnchoredPath, TreeItem IO) -> AnchoredPath
forall a b. (a, b) -> a
fst) ([(AnchoredPath, TreeItem IO)] -> [AnchoredPath])
-> [(AnchoredPath, TreeItem IO)] -> [AnchoredPath]
forall a b. (a -> b) -> a -> b
$ Tree IO -> [(AnchoredPath, TreeItem IO)]
forall (m :: * -> *). Tree m -> [(AnchoredPath, TreeItem m)]
T.list Tree IO
s'
          showPath :: (AnchoredPath, TreeItem m) -> ByteString
showPath (AnchoredPath
n, File Blob m
_) = String -> ByteString
BC.pack (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$ AnchoredPath -> String
displayPath (AnchoredPath -> String) -> AnchoredPath -> String
forall a b. (a -> b) -> a -> b
$ AnchoredPath
path AnchoredPath -> AnchoredPath -> AnchoredPath
`catPaths` AnchoredPath
n
          showPath (AnchoredPath
n, TreeItem m
_) = [ByteString] -> ByteString
BC.concat [String -> ByteString
BC.pack (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$ AnchoredPath -> String
displayPath (AnchoredPath -> String) -> AnchoredPath -> String
forall a b. (a -> b) -> a -> b
$ AnchoredPath
path AnchoredPath -> AnchoredPath -> AnchoredPath
`catPaths` AnchoredPath
n, ByteString
"/"]
      (Sealed RL (PatchInfoAnd rt p) Origin wX
ans_patches) <- do
         if Bool -> Bool
not Bool
usePatchIndex
            then Sealed (RL (PatchInfoAnd rt p) Origin)
-> IO (Sealed (RL (PatchInfoAnd rt p) Origin))
forall (m :: * -> *) a. Monad m => a -> m a
return Sealed (RL (PatchInfoAnd rt p) Origin)
patches
            else Sealed (RL (PatchInfoAnd rt p) Origin)
-> Repository rt p wR wU wR
-> PatchSet rt p Origin wR
-> [AnchoredPath]
-> IO (Sealed (RL (PatchInfoAnd rt p) Origin))
forall (p :: * -> * -> *) (a :: * -> * -> *) (rt :: RepoType) wK wR
       wU.
(RepoPatch p, ApplyState p ~ Tree, a ~ PatchInfoAnd rt p) =>
Sealed (RL a wK)
-> Repository rt p wR wU wR
-> PatchSet rt p Origin wR
-> [AnchoredPath]
-> IO (Sealed (RL a Origin))
getRelevantSubsequence Sealed (RL (PatchInfoAnd rt p) Origin)
patches Repository rt p wR wU wR
repository PatchSet rt p Origin wR
r [AnchoredPath]
subs
      Doc -> IO ()
view (Doc -> IO ()) -> (String -> Doc) -> String -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
text (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$
        ByteString -> AnnotateResult -> String
fmt (ByteString -> [ByteString] -> ByteString
BC.intercalate ByteString
"\n" ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$ ((AnchoredPath, TreeItem IO) -> ByteString)
-> [(AnchoredPath, TreeItem IO)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (AnchoredPath, TreeItem IO) -> ByteString
forall (m :: * -> *). (AnchoredPath, TreeItem m) -> ByteString
showPath ([(AnchoredPath, TreeItem IO)] -> [ByteString])
-> [(AnchoredPath, TreeItem IO)] -> [ByteString]
forall a b. (a -> b) -> a -> b
$ Tree IO -> [(AnchoredPath, TreeItem IO)]
forall (m :: * -> *). Tree m -> [(AnchoredPath, TreeItem m)]
T.list Tree IO
s') (AnnotateResult -> String) -> AnnotateResult -> String
forall a b. (a -> b) -> a -> b
$
        RL (PatchInfoAnd rt p) Origin wX
-> AnchoredPath -> [AnchoredPath] -> AnnotateResult
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
RL (PatchInfoAnd rt p) wX wY
-> AnchoredPath -> [AnchoredPath] -> AnnotateResult
A.annotateDirectory RL (PatchInfoAnd rt p) Origin wX
ans_patches AnchoredPath
path [AnchoredPath]
subs
    Just (File Blob IO
b) -> do (Sealed RL (PatchInfoAnd rt p) Origin wX
ans_patches) <- do
                           if Bool -> Bool
not Bool
usePatchIndex
                              then Sealed (RL (PatchInfoAnd rt p) Origin)
-> IO (Sealed (RL (PatchInfoAnd rt p) Origin))
forall (m :: * -> *) a. Monad m => a -> m a
return Sealed (RL (PatchInfoAnd rt p) Origin)
patches
                              else Sealed (RL (PatchInfoAnd rt p) Origin)
-> Repository rt p wR wU wR
-> PatchSet rt p Origin wR
-> [AnchoredPath]
-> IO (Sealed (RL (PatchInfoAnd rt p) Origin))
forall (p :: * -> * -> *) (a :: * -> * -> *) (rt :: RepoType) wK wR
       wU.
(RepoPatch p, ApplyState p ~ Tree, a ~ PatchInfoAnd rt p) =>
Sealed (RL a wK)
-> Repository rt p wR wU wR
-> PatchSet rt p Origin wR
-> [AnchoredPath]
-> IO (Sealed (RL a Origin))
getRelevantSubsequence Sealed (RL (PatchInfoAnd rt p) Origin)
patches Repository rt p wR wU wR
repository PatchSet rt p Origin wR
r [AnchoredPath
path]
                        ByteString
con <- [ByteString] -> ByteString
BC.concat ([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` ByteString -> [ByteString]
toChunks (ByteString -> ByteString) -> IO ByteString -> IO ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Blob IO -> IO ByteString
forall (m :: * -> *). Blob m -> m ByteString
T.readBlob Blob IO
b
                        Doc -> IO ()
view (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> Doc
text (String -> Doc)
-> (AnnotateResult -> String) -> AnnotateResult -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> AnnotateResult -> String
fmt ByteString
con (AnnotateResult -> Doc) -> AnnotateResult -> Doc
forall a b. (a -> b) -> a -> b
$
                          RL (PatchInfoAnd rt p) Origin wX
-> AnchoredPath -> ByteString -> AnnotateResult
forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
AnnotateRP p =>
RL (PatchInfoAnd rt p) wX wY
-> AnchoredPath -> ByteString -> AnnotateResult
A.annotateFile RL (PatchInfoAnd rt p) Origin wX
ans_patches AnchoredPath
path ByteString
con
    Just (Stub IO (Tree IO)
_ Hash
_) -> String -> IO ()
forall a. HasCallStack => String -> a
error String
"impossible case"