--  Copyright (C) 2002-2003 David Roundy
--
--  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.Record
    ( record
    , commit
    ) where

import Darcs.Prelude
import Data.Foldable ( traverse_ )

import Control.Exception ( handleJust )
import Control.Monad ( when, unless, void )
import Data.Char ( ord )
import System.Exit ( exitFailure, exitSuccess, ExitCode(..) )
import System.Directory ( removeFile )

import Darcs.Patch.PatchInfoAnd ( n2pia )
import Darcs.Repository
    ( Repository
    , withRepoLock
    , RepoJob(..)
    , tentativelyAddPatch
    , finalizeRepositoryChanges
    , invalidateIndex
    , readPendingAndWorking
    , readRecorded
    )
import Darcs.Repository.Pending ( tentativelyRemoveFromPW )

import Darcs.Patch ( IsRepoType, RepoPatch, PrimOf, sortCoalesceFL )
import Darcs.Patch.Named ( infopatch, adddeps )
import Darcs.Patch.Witnesses.Ordered
    ( FL(..), (:>)(..), nullFL, (+>+) )
import Darcs.Patch.Info ( PatchInfo, patchinfo )
import Darcs.Patch.Apply( ApplyState )
import Darcs.Patch.Split ( primSplitter )
import Darcs.UI.SelectChanges
    (  WhichChanges(..)
    , selectionConfigPrim
    , runInvertibleSelection
    , askAboutDepends
    )
import qualified Darcs.UI.SelectChanges as S ( PatchSelectionOptions(..) )
import Darcs.Util.Path ( AnchoredPath, displayPath, AbsolutePath )
import Darcs.UI.Commands
    ( DarcsCommand(..), withStdOpts
    , nodefaults
    , commandAlias
    , setEnvDarcsFiles
    , setEnvDarcsPatches
    , amInHashedRepository
    )
import Darcs.UI.Commands.Util ( announceFiles, filterExistingPaths,
                                testTentativeAndMaybeExit )
import Darcs.UI.Completion ( modifiedFileArgs )
import Darcs.UI.Flags
    ( DarcsFlag
    , fileHelpAuthor
    , getAuthor
    , getDate
    , diffOpts
    , scanKnown
    , pathSetFromArgs
    )
import Darcs.UI.Options ( DarcsOption, (^), odesc, ocheck, oparse, defaultFlags )
import Darcs.UI.PatchHeader ( getLog )
import qualified Darcs.UI.Options.All as O
import Darcs.Repository.Flags ( UpdatePending (..), DryRun(NoDryRun), ScanKnown(..) )
import Darcs.Util.Exception ( clarifyErrors )
import Darcs.Util.Prompt ( promptYorn )
import Darcs.Util.Progress ( debugMessage )
import Darcs.Util.Global ( darcsLastMessage )
import Darcs.Patch.Progress ( progressFL )
import Darcs.Util.Printer
    ( Doc
    , ($+$)
    , (<+>)
    , formatWords
    , pathlist
    , putDocLn
    , text
    , vcat
    , vsep
    )
import Darcs.Util.Tree( Tree )

recordHelp :: Doc
recordHelp :: Doc
recordHelp =
  [Doc] -> Doc
vsep (forall a b. (a -> b) -> [a] -> [b]
map [[Char]] -> Doc
formatWords
  [ [ [Char]
"The `darcs record` command is used to create a patch from changes in"
    , [Char]
"the working tree.  If you specify a set of files and directories,"
    , [Char]
"changes to other files will be skipped."
    ]
  , [ [Char]
"Every patch has a name, an optional description, an author and a date."
    ]
  , [ [Char]
"Darcs will launch a text editor (see `darcs help environment`) after the"
    , [Char]
"interactive selection, to let you enter the patch name (first line) and"
    , [Char]
"the patch description (subsequent lines)."
    ]
  , [ [Char]
"You can supply the patch name in advance with the `-m` option, in which"
    , [Char]
"case no text editor is launched, unless you use `--edit-long-comment`."
    ]
  , [ [Char]
"The patch description is an optional block of free-form text.  It is"
    , [Char]
"used to supply additional information that doesn't fit in the patch"
    , [Char]
"name.  For example, it might include a rationale of WHY the change was"
    , [Char]
"necessary."
    ]
  , [ [Char]
"A technical difference between patch name and patch description, is"
    , [Char]
"that matching with the flag `-p` is only done on patch names."
    ]
  , [ [Char]
"Finally, the `--logfile` option allows you to supply a file that already"
    , [Char]
"contains the patch name and description.  This is useful if a previous"
    , [Char]
"record failed and left a `_darcs/patch_description.txt` file."
    ]
  , [[Char]]
fileHelpAuthor
  , [ [Char]
"If you want to manually define any explicit dependencies for your patch,"
    , [Char]
"you can use the `--ask-deps` flag. Some dependencies may be automatically"
    , [Char]
"inferred from the patch's content and cannot be removed. A patch with"
    , [Char]
"specific dependencies can be empty."
    ]
  , [ [Char]
"The patch date is generated automatically.  It can only be spoofed by"
    , [Char]
"using the `--pipe` option."
    ]
  , [ [Char]
"If you run record with the `--pipe` option, you will be prompted for"
    , [Char]
"the patch date, author, and the long comment. The long comment will extend"
    , [Char]
"until the end of file or stdin is reached. This interface is intended for"
    , [Char]
"scripting darcs, in particular for writing repository conversion scripts."
    , [Char]
"The prompts are intended mostly as a useful guide (since scripts won't"
    , [Char]
"need them), to help you understand the input format. Here's an example of"
    , [Char]
"what the `--pipe` prompts look like:"
    ]
  ])
  Doc -> Doc -> Doc
$+$ [Doc] -> Doc
vcat
    [ Doc
"    What is the date? Mon Nov 15 13:38:01 EST 2004"
    , Doc
"    Who is the author? David Roundy"
    , Doc
"    What is the log? One or more comment lines"
    ]
  Doc -> Doc -> Doc
$+$ [Doc] -> Doc
vsep (forall a b. (a -> b) -> [a] -> [b]
map [[Char]] -> Doc
formatWords
  [ [ [Char]
"If a test command has been defined with `darcs setpref`, attempting to"
    , [Char]
"record a patch will cause the test command to be run in a clean copy"
    , [Char]
"of the working tree (that is, including only recorded changes).  If"
    , [Char]
"the test fails, you will be offered to abort the record operation."
    ]
  , [ [Char]
"The `--set-scripts-executable` option causes scripts to be made"
    , [Char]
"executable in the clean copy of the working tree, prior to running the"
    , [Char]
"test.  See `darcs clone` for an explanation of the script heuristic."
    ]
  , [ [Char]
"If your test command is tediously slow (e.g. `make all`) and you are"
    , [Char]
"recording several patches in a row, you may wish to use `--no-test` to"
    , [Char]
"skip all but the final test."
    ]
  , [ [Char]
"To see some context (unchanged lines) around each change, use the"
    , [Char]
"`--unified` option."
    ]
  ])

recordBasicOpts :: DarcsOption a
                   (Maybe String
                    -> Maybe String
                    -> O.TestChanges
                    -> Maybe Bool
                    -> Bool
                    -> Bool
                    -> Maybe O.AskLongComment
                    -> O.LookFor
                    -> Maybe String
                    -> O.WithContext
                    -> O.DiffAlgorithm
                    -> a)
recordBasicOpts :: forall a.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> a)
recordBasicOpts
    = PrimDarcsOption (Maybe [Char])
O.patchname
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe [Char])
O.author
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption TestChanges
O.testChanges
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe Bool)
O.interactive
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption Bool
O.pipe
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption Bool
O.askDeps
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe AskLongComment)
O.askLongComment
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption LookFor
O.lookfor
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe [Char])
O.repoDir
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption WithContext
O.withContext
    forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption DiffAlgorithm
O.diffAlgorithm

recordAdvancedOpts :: DarcsOption a
                      (O.Logfile -> O.Compression -> O.UseIndex -> O.UMask -> O.SetScriptsExecutable -> O.IncludeBoring -> a)
recordAdvancedOpts :: forall a.
DarcsOption
  a
  (Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> a)
recordAdvancedOpts = PrimDarcsOption Logfile
O.logfile forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption Compression
O.compress forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption UseIndex
O.useIndex forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption UMask
O.umask forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption SetScriptsExecutable
O.setScriptsExecutable forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption IncludeBoring
O.includeBoring

data RecordConfig = RecordConfig
    { RecordConfig -> Maybe [Char]
patchname :: Maybe String
    , RecordConfig -> Maybe [Char]
author :: Maybe String
    , RecordConfig -> TestChanges
testChanges :: O.TestChanges
    , RecordConfig -> Maybe Bool
interactive :: Maybe Bool
    , RecordConfig -> Bool
pipe :: Bool
    , RecordConfig -> Bool
askDeps :: Bool
    , RecordConfig -> Maybe AskLongComment
askLongComment :: Maybe O.AskLongComment
    , RecordConfig -> LookFor
lookfor :: O.LookFor
    , RecordConfig -> Maybe [Char]
_workingRepoDir :: Maybe String
    , RecordConfig -> WithContext
withContext :: O.WithContext
    , RecordConfig -> DiffAlgorithm
diffAlgorithm :: O.DiffAlgorithm
    , RecordConfig -> Verbosity
verbosity :: O.Verbosity
    , RecordConfig -> Logfile
logfile :: O.Logfile
    , RecordConfig -> Compression
compress :: O.Compression
    , RecordConfig -> UseIndex
useIndex :: O.UseIndex
    , RecordConfig -> UMask
umask :: O.UMask
    , RecordConfig -> SetScriptsExecutable
sse :: O.SetScriptsExecutable
    , RecordConfig -> IncludeBoring
includeBoring :: O.IncludeBoring
    , RecordConfig -> UseCache
useCache :: O.UseCache
    }

recordConfig :: [DarcsFlag] -> RecordConfig
recordConfig :: [Flag] -> RecordConfig
recordConfig = forall (d :: * -> *) f a b. OptSpec d f a b -> b -> [f] -> a
oparse (forall a.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> a)
recordBasicOpts forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption Verbosity
O.verbosity forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ forall a.
DarcsOption
  a
  (Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> a)
recordAdvancedOpts forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption UseCache
O.useCache) Maybe [Char]
-> Maybe [Char]
-> TestChanges
-> Maybe Bool
-> Bool
-> Bool
-> Maybe AskLongComment
-> LookFor
-> Maybe [Char]
-> WithContext
-> DiffAlgorithm
-> Verbosity
-> Logfile
-> Compression
-> UseIndex
-> UMask
-> SetScriptsExecutable
-> IncludeBoring
-> UseCache
-> RecordConfig
RecordConfig

record :: DarcsCommand
record :: DarcsCommand
record = DarcsCommand
    { commandProgramName :: [Char]
commandProgramName = [Char]
"darcs"
    , commandName :: [Char]
commandName = [Char]
"record"
    , commandHelp :: Doc
commandHelp = Doc
recordHelp
    , commandDescription :: [Char]
commandDescription = [Char]
"Create a patch from unrecorded changes."
    , commandExtraArgs :: Int
commandExtraArgs = -Int
1
    , commandExtraArgHelp :: [[Char]]
commandExtraArgHelp = [[Char]
"[FILE or DIRECTORY]..."]
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [Flag] -> [[Char]] -> IO ()
commandCommand = (AbsolutePath, AbsolutePath) -> [Flag] -> [[Char]] -> IO ()
recordCmd
    , commandPrereq :: [Flag] -> IO (Either [Char] ())
commandPrereq = [Flag] -> IO (Either [Char] ())
amInHashedRepository
    , commandCompleteArgs :: (AbsolutePath, AbsolutePath) -> [Flag] -> [[Char]] -> IO [[Char]]
commandCompleteArgs = (AbsolutePath, AbsolutePath) -> [Flag] -> [[Char]] -> IO [[Char]]
modifiedFileArgs
    , commandArgdefaults :: [Flag] -> AbsolutePath -> [[Char]] -> IO [[Char]]
commandArgdefaults = [Flag] -> AbsolutePath -> [[Char]] -> IO [[Char]]
nodefaults
    , commandAdvancedOptions :: [DarcsOptDescr Flag]
commandAdvancedOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall a.
DarcsOption
  a
  (Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> a)
recordAdvancedOpts
    , commandBasicOptions :: [DarcsOptDescr Flag]
commandBasicOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall a.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> a)
recordBasicOpts
    , commandDefaults :: [Flag]
commandDefaults = forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags forall {a}.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
recordOpts
    , commandCheckOptions :: [Flag] -> [[Char]]
commandCheckOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [[Char]]
ocheck forall {a}.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
recordOpts
    }
  where
    recordOpts :: DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
recordOpts = forall a.
DarcsOption
  a
  (Maybe [Char]
   -> Maybe [Char]
   -> TestChanges
   -> Maybe Bool
   -> Bool
   -> Bool
   -> Maybe AskLongComment
   -> LookFor
   -> Maybe [Char]
   -> WithContext
   -> DiffAlgorithm
   -> a)
recordBasicOpts forall b c a.
DarcsOption (Maybe StdCmdAction -> Verbosity -> b) c
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a) b
-> DarcsOption a c
`withStdOpts` forall a.
DarcsOption
  a
  (Logfile
   -> Compression
   -> UseIndex
   -> UMask
   -> SetScriptsExecutable
   -> IncludeBoring
   -> a)
recordAdvancedOpts

-- | commit is an alias for record
commit :: DarcsCommand
commit :: DarcsCommand
commit = [Char] -> Maybe DarcsCommand -> DarcsCommand -> DarcsCommand
commandAlias [Char]
"commit" forall a. Maybe a
Nothing DarcsCommand
record

reportNonExisting :: ScanKnown -> ([AnchoredPath], [AnchoredPath]) -> IO ()
reportNonExisting :: ScanKnown -> ([AnchoredPath], [AnchoredPath]) -> IO ()
reportNonExisting ScanKnown
scan ([AnchoredPath]
paths_only_in_working, [AnchoredPath]
_) = do
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ScanKnown
scan forall a. Eq a => a -> a -> Bool
/= ScanKnown
ScanKnown Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [AnchoredPath]
paths_only_in_working) forall a b. (a -> b) -> a -> b
$  Doc -> IO ()
putDocLn forall a b. (a -> b) -> a -> b
$
    Doc
"These paths are not yet in the repository and will be added:" Doc -> Doc -> Doc
<+>
    [[Char]] -> Doc
pathlist (forall a b. (a -> b) -> [a] -> [b]
map AnchoredPath -> [Char]
displayPath [AnchoredPath]
paths_only_in_working)

recordCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
recordCmd :: (AbsolutePath, AbsolutePath) -> [Flag] -> [[Char]] -> IO ()
recordCmd (AbsolutePath, AbsolutePath)
fps [Flag]
flags [[Char]]
args = do
    let cfg :: RecordConfig
cfg = [Flag] -> RecordConfig
recordConfig [Flag]
flags
    Maybe [Char] -> Bool -> IO ()
checkNameIsNotOption (RecordConfig -> Maybe [Char]
patchname RecordConfig
cfg) (RecordConfig -> Bool
isInteractive RecordConfig
cfg)
    forall a.
DryRun -> UseCache -> UpdatePending -> UMask -> RepoJob a -> IO a
withRepoLock DryRun
NoDryRun (RecordConfig -> UseCache
useCache RecordConfig
cfg) UpdatePending
YesUpdatePending (RecordConfig -> UMask
umask RecordConfig
cfg) forall a b. (a -> b) -> a -> b
$ 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 a b. (a -> b) -> a -> b
$ \(Repository rt p wR wU wR
repository :: Repository rt p wR wU wR) -> do
      let scan :: ScanKnown
scan = LookForAdds -> IncludeBoring -> ScanKnown
scanKnown (LookFor -> LookForAdds
O.adds (RecordConfig -> LookFor
lookfor RecordConfig
cfg)) (RecordConfig -> IncludeBoring
includeBoring RecordConfig
cfg)
      Maybe [AnchoredPath]
existing_files <- do
        Maybe [AnchoredPath]
files <- (AbsolutePath, AbsolutePath)
-> [[Char]] -> IO (Maybe [AnchoredPath])
pathSetFromArgs (AbsolutePath, AbsolutePath)
fps [[Char]]
args
        Maybe ([AnchoredPath], [AnchoredPath])
files' <-
          forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse
            (forall (p :: * -> * -> *) (rt :: RepoType) wR wU.
(RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> Verbosity
-> UseIndex
-> ScanKnown
-> LookForMoves
-> [AnchoredPath]
-> IO ([AnchoredPath], [AnchoredPath])
filterExistingPaths
              Repository rt p wR wU wR
repository (RecordConfig -> Verbosity
verbosity RecordConfig
cfg) (RecordConfig -> UseIndex
useIndex RecordConfig
cfg) ScanKnown
scan (LookFor -> LookForMoves
O.moves (RecordConfig -> LookFor
lookfor RecordConfig
cfg)))
            Maybe [AnchoredPath]
files
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RecordConfig -> Verbosity
verbosity RecordConfig
cfg forall a. Eq a => a -> a -> Bool
/= Verbosity
O.Quiet) forall a b. (a -> b) -> a -> b
$
            forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (ScanKnown -> ([AnchoredPath], [AnchoredPath]) -> IO ()
reportNonExisting ScanKnown
scan) Maybe ([AnchoredPath], [AnchoredPath])
files'
        let files'' :: Maybe [AnchoredPath]
files'' = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd Maybe ([AnchoredPath], [AnchoredPath])
files'
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe [AnchoredPath]
files'' forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just []) forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"None of the files you specified exist."
        forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [AnchoredPath]
files''
      Verbosity -> Maybe [AnchoredPath] -> [Char] -> IO ()
announceFiles (RecordConfig -> Verbosity
verbosity RecordConfig
cfg) Maybe [AnchoredPath]
existing_files [Char]
"Recording changes in"
      [Char] -> IO ()
debugMessage [Char]
"About to get the unrecorded changes."
      (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
changes <- forall (p :: * -> * -> *) (rt :: RepoType) wR wU.
(RepoPatch p, ApplyState p ~ Tree) =>
(UseIndex, ScanKnown, DiffAlgorithm)
-> LookForMoves
-> LookForReplaces
-> Repository rt p wR wU wR
-> Maybe [AnchoredPath]
-> IO ((:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU)
readPendingAndWorking (RecordConfig -> (UseIndex, ScanKnown, DiffAlgorithm)
diffingOpts RecordConfig
cfg)
                   (LookFor -> LookForMoves
O.moves (RecordConfig -> LookFor
lookfor RecordConfig
cfg)) (LookFor -> LookForReplaces
O.replaces (RecordConfig -> LookFor
lookfor RecordConfig
cfg))
                   Repository rt p wR wU wR
repository Maybe [AnchoredPath]
existing_files
      [Char] -> IO ()
debugMessage [Char]
"I've got unrecorded changes."
      case (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
changes of
          FL (PrimOf p) wR wZ
NilFL :> FL (PrimOf p) wZ wU
NilFL | Bool -> Bool
not (RecordConfig -> Bool
askDeps RecordConfig
cfg) -> do
              -- We need to grab any input waiting for us, since we
              -- might break scripts expecting to send it to us; we
              -- don't care what that input is, though.
              forall (f :: * -> *) a. Functor f => f a -> f ()
void (Bool -> IO [Char]
getDate (RecordConfig -> Bool
pipe RecordConfig
cfg))
              [Char] -> IO ()
putStrLn [Char]
"No changes!"
              forall a. IO a
exitFailure
          (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
_ -> forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> RecordConfig
-> Maybe [AnchoredPath]
-> (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
-> IO ()
doRecord Repository rt p wR wU wR
repository RecordConfig
cfg Maybe [AnchoredPath]
existing_files (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
changes

-- | Check user specified patch name is not accidentally a command line flag
checkNameIsNotOption :: Maybe String -> Bool -> IO ()
checkNameIsNotOption :: Maybe [Char] -> Bool -> IO ()
checkNameIsNotOption Maybe [Char]
Nothing     Bool
_      = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkNameIsNotOption Maybe [Char]
_           Bool
False  = forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkNameIsNotOption (Just [Char]
name) Bool
True   =
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
name forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
|| (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
name forall a. Eq a => a -> a -> Bool
== Int
2 Bool -> Bool -> Bool
&& forall a. [a] -> a
head [Char]
name forall a. Eq a => a -> a -> Bool
== Char
'-')) forall a b. (a -> b) -> a -> b
$ do
        Bool
confirmed <- [Char] -> IO Bool
promptYorn forall a b. (a -> b) -> a -> b
$ [Char]
"You specified " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show [Char]
name forall a. [a] -> [a] -> [a]
++ [Char]
" as the patch name. Is that really what you want?"
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
confirmed forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
putStrLn [Char]
"Okay, aborting the record." forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. IO a
exitFailure

doRecord :: (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree)
         => Repository rt p wR wU wR -> RecordConfig -> Maybe [AnchoredPath]
         -> (FL (PrimOf p) :> FL (PrimOf p)) wR wU -> IO ()
doRecord :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> RecordConfig
-> Maybe [AnchoredPath]
-> (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
-> IO ()
doRecord Repository rt p wR wU wR
repository RecordConfig
cfg Maybe [AnchoredPath]
files pw :: (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
pw@(FL (PrimOf p) wR wZ
pending :> FL (PrimOf p) wZ wU
working) = do
    [Char]
date <- Bool -> IO [Char]
getDate (RecordConfig -> Bool
pipe RecordConfig
cfg)
    [Char]
my_author <- Maybe [Char] -> Bool -> IO [Char]
getAuthor (RecordConfig -> Maybe [Char]
author RecordConfig
cfg) (RecordConfig -> Bool
pipe RecordConfig
cfg)
    [Char] -> IO ()
debugMessage [Char]
"I'm slurping the repository."
    Tree IO
pristine <- forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> IO (Tree IO)
readRecorded Repository rt p wR wU wR
repository
    [Char] -> IO ()
debugMessage [Char]
"About to select changes..."
    (FL (PrimOf p) wR wZ
chs :> FL (PrimOf p) wZ wU
_ ) <- forall (p :: * -> * -> *) wX wY.
(Invert p, MatchableRP p, ShowPatch p, ShowContextPatch p,
 ApplyState p ~ Tree) =>
FL p wX wY -> SelectionConfig p -> IO ((:>) (FL p) (FL p) wX wY)
runInvertibleSelection (forall (prim :: * -> * -> *) wX wY.
PrimCanonize prim =>
FL prim wX wY -> FL prim wX wY
sortCoalesceFL forall a b. (a -> b) -> a -> b
$ FL (PrimOf p) wR wZ
pending forall (a :: * -> * -> *) wX wY wZ.
FL a wX wY -> FL a wY wZ -> FL a wX wZ
+>+ FL (PrimOf p) wZ wU
working) forall a b. (a -> b) -> a -> b
$
                      forall (prim :: * -> * -> *).
WhichChanges
-> [Char]
-> PatchSelectionOptions
-> Maybe (Splitter prim)
-> Maybe [AnchoredPath]
-> Maybe (Tree IO)
-> SelectionConfig prim
selectionConfigPrim
                          WhichChanges
First [Char]
"record" (RecordConfig -> PatchSelectionOptions
patchSelOpts RecordConfig
cfg)
                          (forall a. a -> Maybe a
Just (forall (p :: * -> * -> *).
PrimPatch p =>
DiffAlgorithm -> Splitter p
primSplitter (RecordConfig -> DiffAlgorithm
diffAlgorithm RecordConfig
cfg)))
                          Maybe [AnchoredPath]
files (forall a. a -> Maybe a
Just Tree IO
pristine)
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (RecordConfig -> Bool
askDeps RecordConfig
cfg) Bool -> Bool -> Bool
&& forall (a :: * -> * -> *) wX wZ. FL a wX wZ -> Bool
nullFL FL (PrimOf p) wR wZ
chs) forall a b. (a -> b) -> a -> b
$
              do [Char] -> IO ()
putStrLn [Char]
"Ok, if you don't want to record anything, that's fine!"
                 forall a. IO a
exitSuccess
    forall e b a.
Exception e =>
(e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
handleJust ExitCode -> Maybe ()
onlySuccessfulExits (\()
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()) forall a b. (a -> b) -> a -> b
$
             do [PatchInfo]
deps <- if RecordConfig -> Bool
askDeps RecordConfig
cfg
                        then forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT wY.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wT
-> FL (PrimOf p) wT wY
-> PatchSelectionOptions
-> [PatchInfo]
-> IO [PatchInfo]
askAboutDepends Repository rt p wR wU wR
repository FL (PrimOf p) wR wZ
chs (RecordConfig -> PatchSelectionOptions
patchSelOpts RecordConfig
cfg) []
                        else forall (m :: * -> *) a. Monad m => a -> m a
return []
                forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RecordConfig -> Bool
askDeps RecordConfig
cfg) forall a b. (a -> b) -> a -> b
$ [Char] -> IO ()
debugMessage [Char]
"I've asked about dependencies."
                if forall (a :: * -> * -> *) wX wZ. FL a wX wZ -> Bool
nullFL FL (PrimOf p) wR wZ
chs Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PatchInfo]
deps
                  then [Char] -> IO ()
putStrLn [Char]
"Ok, if you don't want to record anything, that's fine!"
                  else do forall (p :: * -> * -> *) wX wY. PatchInspect p => p wX wY -> IO ()
setEnvDarcsFiles FL (PrimOf p) wR wZ
chs
                          ([Char]
name, [[Char]]
my_log, Maybe [Char]
logf) <- forall (prim :: * -> * -> *) wX wY.
PrimPatch prim =>
Maybe [Char]
-> Bool
-> Logfile
-> Maybe AskLongComment
-> Maybe ([Char], [[Char]])
-> FL prim wX wY
-> IO ([Char], [[Char]], Maybe [Char])
getLog (RecordConfig -> Maybe [Char]
patchname RecordConfig
cfg) (RecordConfig -> Bool
pipe RecordConfig
cfg) (RecordConfig -> Logfile
logfile RecordConfig
cfg) (RecordConfig -> Maybe AskLongComment
askLongComment RecordConfig
cfg) forall a. Maybe a
Nothing FL (PrimOf p) wR wZ
chs
                          [Char] -> IO ()
debugMessage ([Char]
"Patch name as received from getLog: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (forall a b. (a -> b) -> [a] -> [b]
map Char -> Int
ord [Char]
name))
                          forall (rt :: RepoType) (p :: * -> * -> *) wR wU wX.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> RecordConfig
-> [Char]
-> [Char]
-> [Char]
-> [[Char]]
-> Maybe [Char]
-> [PatchInfo]
-> FL (PrimOf p) wR wX
-> (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
-> IO ()
doActualRecord Repository rt p wR wU wR
repository RecordConfig
cfg [Char]
name [Char]
date [Char]
my_author [[Char]]
my_log Maybe [Char]
logf [PatchInfo]
deps FL (PrimOf p) wR wZ
chs (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
pw

doActualRecord :: (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree)
               => Repository rt p wR wU wR
               -> RecordConfig
               -> String -> String -> String
               -> [String] -> Maybe String
               -> [PatchInfo] -> FL (PrimOf p) wR wX
               -> (FL (PrimOf p) :> FL (PrimOf p)) wR wU -> IO ()
doActualRecord :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wX.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> RecordConfig
-> [Char]
-> [Char]
-> [Char]
-> [[Char]]
-> Maybe [Char]
-> [PatchInfo]
-> FL (PrimOf p) wR wX
-> (:>) (FL (PrimOf p)) (FL (PrimOf p)) wR wU
-> IO ()
doActualRecord Repository rt p wR wU wR
_repository RecordConfig
cfg [Char]
name [Char]
date [Char]
my_author [[Char]]
my_log Maybe [Char]
logf [PatchInfo]
deps FL (PrimOf p) wR wX
chs
      (FL (PrimOf p) wR wZ
pending :> FL (PrimOf p) wZ wU
working) = do
    [Char] -> IO ()
debugMessage [Char]
"Writing the patch file..."
    PatchInfo
myinfo <- [Char] -> [Char] -> [Char] -> [[Char]] -> IO PatchInfo
patchinfo [Char]
date [Char]
name [Char]
my_author [[Char]]
my_log
    let mypatch :: Named p wR wX
mypatch = forall (p :: * -> * -> *) wX wY.
FromPrim p =>
PatchInfo -> FL (PrimOf p) wX wY -> Named p wX wY
infopatch PatchInfo
myinfo forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) wX wY. [Char] -> FL a wX wY -> FL a wX wY
progressFL [Char]
"Writing changes:" FL (PrimOf p) wR wX
chs
    let pia :: PatchInfoAndG rt (Named p) wR wX
pia = forall (p :: * -> * -> *) wX wY (rt :: RepoType).
(Ident p, PatchId p ~ PatchInfo) =>
p wX wY -> PatchInfoAndG rt p wX wY
n2pia forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) wX wY.
Named p wX wY -> [PatchInfo] -> Named p wX wY
adddeps Named p wR wX
mypatch [PatchInfo]
deps
    Repository rt p wR wU wX
_repository <-
      forall (p :: * -> * -> *) (rt :: RepoType) wR wU wT wY.
(RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wT
-> Compression
-> Verbosity
-> UpdatePending
-> PatchInfoAnd rt p wT wY
-> IO (Repository rt p wR wU wY)
tentativelyAddPatch Repository rt p wR wU wR
_repository (RecordConfig -> Compression
compress RecordConfig
cfg) (RecordConfig -> Verbosity
verbosity RecordConfig
cfg)
        UpdatePending
NoUpdatePending forall {rt :: RepoType}. PatchInfoAndG rt (Named p) wR wX
pia
    forall t. t -> IO ()
invalidateIndex Repository rt p wR wU wX
_repository
    [Char] -> IO ()
debugMessage [Char]
"Applying to pristine..."
    forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT
-> Verbosity
-> TestChanges
-> SetScriptsExecutable
-> Bool
-> [Char]
-> [Char]
-> Maybe [Char]
-> IO ()
testTentativeAndMaybeExit Repository rt p wR wU wX
_repository (RecordConfig -> Verbosity
verbosity RecordConfig
cfg) (RecordConfig -> TestChanges
testChanges RecordConfig
cfg)
      (RecordConfig -> SetScriptsExecutable
sse RecordConfig
cfg) (RecordConfig -> Bool
isInteractive RecordConfig
cfg) ([Char]
"you have a bad patch: '" forall a. [a] -> [a] -> [a]
++ [Char]
name forall a. [a] -> [a] -> [a]
++ [Char]
"'")
      [Char]
"record it" (forall a. a -> Maybe a
Just [Char]
failuremessage)
    forall (rt :: RepoType) (p :: * -> * -> *) wR wO wT wP wU.
RepoPatch p =>
Repository rt p wR wU wT
-> FL (PrimOf p) wO wT
-> FL (PrimOf p) wO wP
-> FL (PrimOf p) wP wU
-> IO ()
tentativelyRemoveFromPW Repository rt p wR wU wX
_repository FL (PrimOf p) wR wX
chs FL (PrimOf p) wR wZ
pending FL (PrimOf p) wZ wU
working
    Repository rt p wX wU wX
_repository <-
      forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
(IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wT
-> UpdatePending -> Compression -> IO (Repository rt p wT wU wT)
finalizeRepositoryChanges Repository rt p wR wU wX
_repository UpdatePending
YesUpdatePending (RecordConfig -> Compression
compress RecordConfig
cfg)
      forall a. IO a -> [Char] -> IO a
`clarifyErrors` [Char]
failuremessage
    [Char] -> IO ()
debugMessage [Char]
"Syncing timestamps..."
    Maybe [Char] -> IO ()
removeLogFile Maybe [Char]
logf
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (RecordConfig -> Verbosity
verbosity RecordConfig
cfg forall a. Eq a => a -> a -> Bool
== Verbosity
O.Quiet) forall a b. (a -> b) -> a -> b
$
      Doc -> IO ()
putDocLn forall a b. (a -> b) -> a -> b
$ [Char] -> Doc
text forall a b. (a -> b) -> a -> b
$ [Char]
"Finished recording patch '" forall a. [a] -> [a] -> [a]
++ [Char]
name forall a. [a] -> [a] -> [a]
++ [Char]
"'"
    forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
RepoPatch p =>
FL (PatchInfoAnd rt p) wX wY -> IO ()
setEnvDarcsPatches (forall {rt :: RepoType}. PatchInfoAndG rt (Named p) wR wX
pia forall (a :: * -> * -> *) wX wY wZ.
a wX wY -> FL a wY wZ -> FL a wX wZ
:>: forall (a :: * -> * -> *) wX. FL a wX wX
NilFL)
  where
    removeLogFile :: Maybe String -> IO ()
    removeLogFile :: Maybe [Char] -> IO ()
removeLogFile Maybe [Char]
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    removeLogFile (Just [Char]
lf)
      | [Char]
lf forall a. Eq a => a -> a -> Bool
== [Char]
darcsLastMessage = forall (m :: * -> *) a. Monad m => a -> m a
return ()
      | Bool
otherwise = [Char] -> IO ()
removeFile [Char]
lf
    failuremessage :: [Char]
failuremessage =
      [Char]
"Failed to record patch '" forall a. [a] -> [a] -> [a]
++ [Char]
name forall a. [a] -> [a] -> [a]
++ [Char]
"'" forall a. [a] -> [a] -> [a]
++
        case Maybe [Char]
logf of
          Just [Char]
lf -> [Char]
"\nLogfile left in " forall a. [a] -> [a] -> [a]
++ [Char]
lf forall a. [a] -> [a] -> [a]
++ [Char]
"."
          Maybe [Char]
Nothing -> [Char]
""

onlySuccessfulExits :: ExitCode -> Maybe ()
onlySuccessfulExits :: ExitCode -> Maybe ()
onlySuccessfulExits ExitCode
ExitSuccess = forall a. a -> Maybe a
Just ()
onlySuccessfulExits ExitCode
_ = forall a. Maybe a
Nothing

patchSelOpts :: RecordConfig -> S.PatchSelectionOptions
patchSelOpts :: RecordConfig -> PatchSelectionOptions
patchSelOpts RecordConfig
cfg = S.PatchSelectionOptions
    { verbosity :: Verbosity
S.verbosity = RecordConfig -> Verbosity
verbosity RecordConfig
cfg
    , matchFlags :: [MatchFlag]
S.matchFlags = []
    , interactive :: Bool
S.interactive = RecordConfig -> Bool
isInteractive RecordConfig
cfg
    , selectDeps :: SelectDeps
S.selectDeps = SelectDeps
O.PromptDeps -- option not supported, use default
    , withSummary :: WithSummary
S.withSummary = WithSummary
O.NoSummary -- option not supported, use default
    , withContext :: WithContext
S.withContext = RecordConfig -> WithContext
withContext RecordConfig
cfg
    }

diffingOpts :: RecordConfig -> (O.UseIndex, O.ScanKnown, O.DiffAlgorithm)
diffingOpts :: RecordConfig -> (UseIndex, ScanKnown, DiffAlgorithm)
diffingOpts RecordConfig
cfg = UseIndex
-> LookForAdds
-> IncludeBoring
-> DiffAlgorithm
-> (UseIndex, ScanKnown, DiffAlgorithm)
diffOpts (RecordConfig -> UseIndex
useIndex RecordConfig
cfg) (LookFor -> LookForAdds
O.adds (RecordConfig -> LookFor
lookfor RecordConfig
cfg)) IncludeBoring
O.NoIncludeBoring (RecordConfig -> DiffAlgorithm
diffAlgorithm RecordConfig
cfg)

isInteractive :: RecordConfig -> Bool
isInteractive :: RecordConfig -> Bool
isInteractive = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. RecordConfig -> Maybe Bool
interactive