--  Copyright (C) 2003-2005 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.

module Darcs.UI.Commands.Apply
    ( apply, applyCmd
    , getPatchBundle -- used by darcsden
    ) where

import Darcs.Prelude

import System.Exit ( exitSuccess )
import Control.Monad ( unless, when )
import Data.Maybe ( catMaybes )

import Darcs.Patch.PatchInfoAnd ( PatchInfoAnd, hopefullyM, info )
import Darcs.UI.Commands
    ( DarcsCommand(..), withStdOpts
    , putInfo
    , amInHashedRepository
    )
import Darcs.UI.Completion ( fileArgs )
import Darcs.UI.Flags
    ( DarcsFlag
    , changesReverse, verbosity, useCache, dryRun
    , reorder, umask
    , fixUrl
    , withContext
    )
import Darcs.UI.Options ( (^), odesc, ocheck, defaultFlags, parseFlags, (?) )
import qualified Darcs.UI.Options.All as O
import Darcs.Repository.Flags ( UpdatePending(..) )
import Darcs.Util.Path ( toFilePath, AbsolutePath )
import Darcs.Repository
    ( Repository
    , SealedPatchSet
    , withRepoLock
    , readRepo
    , filterOutConflicts
    )
import Darcs.Patch.Set ( PatchSet, Origin )
import Darcs.Patch ( IsRepoType, RepoPatch )
import Darcs.Patch.Apply( ApplyState )
import Darcs.Patch.Info ( PatchInfo, displayPatchInfo )
import Darcs.Patch.Witnesses.Ordered
    ( Fork(..), (:>)(..)
    , mapFL, nullFL )
import Darcs.Patch.Witnesses.Sealed ( Sealed(Sealed) )
import Darcs.Util.ByteString ( linesPS, unlinesPS, gzReadStdin )
import qualified Data.ByteString as B (ByteString, null, init)
import qualified Data.ByteString.Char8 as BC (last)

import Darcs.Util.Download ( Cachable(Uncachable) )
import Darcs.Util.External ( gzFetchFilePS )
import Darcs.UI.External
    ( verifyPS
    )
import Darcs.UI.Email ( readEmail )
import Darcs.Patch.Depends ( findCommonAndUncommon )
import Darcs.UI.ApplyPatches ( PatchApplier(..), StandardPatchApplier(..), PatchProxy )
import Darcs.UI.SelectChanges
    ( WhichChanges(..)
    , runSelection
    , selectionConfig
    )
import qualified Darcs.UI.SelectChanges as S
import Darcs.Patch.Bundle ( interpretBundle, parseBundle )
import Darcs.Util.Printer
    ( Doc, vcat, text
    , renderString
    , ($$)
    , vsep
    , formatWords
    )
import Darcs.Util.Tree( Tree )

applyDescription :: String
applyDescription :: String
applyDescription = String
"Apply a patch bundle created by `darcs send'."

applyHelp :: Doc
applyHelp :: Doc
applyHelp = [Doc] -> Doc
vsep forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map [String] -> Doc
formatWords
  [ [ String
"The `darcs apply` command takes a patch bundle and attempts to insert"
    , String
"it into the current repository.  In addition to invoking it directly"
    , String
"on bundles created by `darcs send`, it is used internally by `darcs"
    , String
"push` on the remote end of an SSH connection."
    ]
  , [ String
"If no file is supplied, the bundle is read from standard input."
    ]
  , [ String
"If given an email instead of a patch bundle, Darcs will look for the"
    , String
"bundle as a MIME attachment to that email.  Currently this will fail"
    , String
"if the MIME boundary is rewritten, such as in Courier and Mail.app."
    ]
  , [ String
"If gpg(1) is installed, you can use `--verify pubring.gpg` to reject"
    , String
"bundles that aren't signed by a key in `pubring.gpg`."
    ]
  , [ String
"If `--test` is supplied and a test is defined (see `darcs setpref`), the"
    , String
"bundle will be rejected if the test fails after applying it."
    ]
  , [ String
"A patch bundle may introduce unresolved conflicts with existing"
    , String
"patches or with the working tree.  By default, Darcs will add conflict"
    , String
"markers (see `darcs mark-conflicts`)."
    ]
  , [ String
"The `--external-merge` option lets you resolve these conflicts"
    , String
"using an external merge tool.  In the option, `%a` is replaced with"
    , String
"the common ancestor (merge base), `%1` with the first version, `%2`"
    , String
"with the second version, and `%o` with the path where your resolved"
    , String
"content should go.  For example, to use the xxdiff visual merge tool"
    , String
"you'd specify: `--external-merge='xxdiff -m -O -M %o %1 %a %2'`"
    ]
  , [ String
"The `--allow-conflicts` option will skip conflict marking; this is"
    , String
"useful when you want to treat a repository as just a bunch of patches,"
    , String
"such as using `darcs pull --union` to download of your co-workers"
    , String
"patches before going offline."
    ]
  , [ String
"This can mess up unrecorded changes in the working tree, forcing you"
    , String
"to resolve the conflict immediately.  To simply reject bundles that"
    , String
"introduce unresolved conflicts, using the `--dont-allow-conflicts`"
    , String
"option.  Making this the default in push-based workflows is strongly"
    , String
"recommended."
    ]
  , [ String
"Unlike most Darcs commands, `darcs apply` defaults to `--all`.  Use the"
    , String
"`--interactive` option to pick which patches to apply from a bundle."
    ]
  ]

stdindefault :: a -> [String] -> IO [String]
stdindefault :: forall a. a -> [String] -> IO [String]
stdindefault a
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return [String
"-"]
stdindefault a
_ [String]
x = forall (m :: * -> *) a. Monad m => a -> m a
return [String]
x

apply :: DarcsCommand
apply :: DarcsCommand
apply = DarcsCommand
    { commandProgramName :: String
commandProgramName = String
"darcs"
    , commandName :: String
commandName = String
"apply"
    , commandHelp :: Doc
commandHelp = Doc
applyHelp
    , commandDescription :: String
commandDescription = String
applyDescription
    , commandExtraArgs :: Int
commandExtraArgs = Int
1
    , commandExtraArgHelp :: [String]
commandExtraArgHelp = [String
"<PATCHFILE>"]
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
commandCommand = forall pa.
PatchApplier pa =>
pa
-> (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
applyCmd StandardPatchApplier
StandardPatchApplier
    , 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]
fileArgs
    , commandArgdefaults :: [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
commandArgdefaults = forall a b. a -> b -> a
const forall a. a -> [String] -> IO [String]
stdindefault
    , commandAdvancedOptions :: [DarcsOptDescr DarcsFlag]
commandAdvancedOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> a)
applyAdvancedOpts
    , commandBasicOptions :: [DarcsOptDescr DarcsFlag]
commandBasicOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> a)
applyBasicOpts
    , commandDefaults :: [DarcsFlag]
commandDefaults = forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags forall {a}.
DarcsOption
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
applyOpts
    , commandCheckOptions :: [DarcsFlag] -> [String]
commandCheckOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [String]
ocheck forall {a}.
DarcsOption
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
applyOpts
    }
  where
    applyBasicOpts :: OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> a)
applyBasicOpts
      = PrimDarcsOption Verify
O.verify
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption Reorder
O.reorder
      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
^ forall a. DarcsOption a (DryRun -> XmlOutput -> a)
O.dryRunXml
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ MatchOption
O.matchSeveral
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe AllowConflicts)
O.conflictsNo
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption ExternalMerge
O.externalMerge
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption RunTest
O.runTest
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption LeaveTestDir
O.leaveTestDir
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption (Maybe String)
O.repoDir
      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
    applyAdvancedOpts :: OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> a)
applyAdvancedOpts
      = 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 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 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 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 Bool
O.changesReverse
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption WantGuiPause
O.pauseForGui
    applyOpts :: DarcsOption
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
applyOpts = forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Verify
   -> Reorder
   -> Maybe Bool
   -> DryRun
   -> XmlOutput
   -> [MatchFlag]
   -> Maybe AllowConflicts
   -> ExternalMerge
   -> RunTest
   -> LeaveTestDir
   -> Maybe String
   -> DiffAlgorithm
   -> a)
applyBasicOpts forall b c a.
DarcsOption (Maybe StdCmdAction -> Verbosity -> b) c
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a) b
-> DarcsOption a c
`withStdOpts` forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (UseIndex
   -> Compression
   -> SetScriptsExecutable
   -> UMask
   -> Bool
   -> WantGuiPause
   -> a)
applyAdvancedOpts

applyCmd :: PatchApplier pa
         => pa
         -> (AbsolutePath, AbsolutePath)
         -> [DarcsFlag]
         -> [String]
         -> IO ()
applyCmd :: forall pa.
PatchApplier pa =>
pa
-> (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
applyCmd pa
patchApplier (AbsolutePath
_,AbsolutePath
orig) [DarcsFlag]
opts [String]
args =
  forall a.
DryRun -> UseCache -> UpdatePending -> UMask -> RepoJob a -> IO a
withRepoLock (PrimDarcsOption DryRun
dryRun forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) (PrimDarcsOption UseCache
useCache forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) UpdatePending
YesUpdatePending (PrimDarcsOption UMask
umask forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) forall a b. (a -> b) -> a -> b
$
  forall pa.
PatchApplier pa =>
pa
-> (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
    (IsRepoType rt, ApplierRepoTypeConstraint pa rt, RepoPatch p,
     ApplyState p ~ Tree) =>
    PatchProxy p -> Repository rt p wR wU wR -> IO ())
-> RepoJob ()
repoJob pa
patchApplier forall a b. (a -> b) -> a -> b
$ \PatchProxy p
patchProxy Repository rt p wR wU wR
repository -> do
    ByteString
bundle <- [String] -> IO ByteString
readBundle [String]
args
    forall (rt :: RepoType) pa (p :: * -> * -> *) wR wU.
(PatchApplier pa, RepoPatch p, ApplyState p ~ Tree,
 ApplierRepoTypeConstraint pa rt, IsRepoType rt) =>
pa
-> PatchProxy p
-> [DarcsFlag]
-> ByteString
-> Repository rt p wR wU wR
-> IO ()
applyCmdCommon pa
patchApplier PatchProxy p
patchProxy [DarcsFlag]
opts ByteString
bundle Repository rt p wR wU wR
repository
  where
    readBundle :: [String] -> IO ByteString
readBundle [String
"-"] = do
      -- For users who try out 'darcs apply' without any arguments.
      -- FIXME apparently some magic behind the scenes causes an empty argument
      -- list to be converted to a single "-". This is quite obscure and should
      -- be removed.
      [DarcsFlag] -> Doc -> IO ()
putInfo [DarcsFlag]
opts forall a b. (a -> b) -> a -> b
$ String -> Doc
text String
"reading patch bundle from stdin..."
      IO ByteString
gzReadStdin
    readBundle [String
""] = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Empty filename argument given to apply!"
    readBundle [String
unfixed_filename] = do
      String
patchesfile <- AbsolutePath -> String -> IO String
fixUrl AbsolutePath
orig String
unfixed_filename
      String -> Cachable -> IO ByteString
gzFetchFilePS (forall a. FilePathLike a => a -> String
toFilePath String
patchesfile) Cachable
Uncachable
    readBundle [String]
_ = forall a. HasCallStack => String -> a
error String
"impossible case"

applyCmdCommon
    :: forall rt pa p wR wU
     . ( PatchApplier pa, RepoPatch p, ApplyState p ~ Tree
       , ApplierRepoTypeConstraint pa rt, IsRepoType rt
       )
    => pa
    -> PatchProxy p
    -> [DarcsFlag]
    -> B.ByteString
    -> Repository rt p wR wU wR
    -> IO ()
applyCmdCommon :: forall (rt :: RepoType) pa (p :: * -> * -> *) wR wU.
(PatchApplier pa, RepoPatch p, ApplyState p ~ Tree,
 ApplierRepoTypeConstraint pa rt, IsRepoType rt) =>
pa
-> PatchProxy p
-> [DarcsFlag]
-> ByteString
-> Repository rt p wR wU wR
-> IO ()
applyCmdCommon pa
patchApplier PatchProxy p
patchProxy [DarcsFlag]
opts ByteString
bundle Repository rt p wR wU wR
repository = do
  PatchSet rt p Origin wR
us <- 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
  Sealed PatchSet rt p Origin wX
them <- forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
[DarcsFlag]
-> PatchSet rt p Origin wR
-> ByteString
-> IO (Either String (SealedPatchSet rt p Origin))
getPatchBundle [DarcsFlag]
opts PatchSet rt p Origin wR
us ByteString
bundle
  Fork PatchSet rt p Origin wU
common FL (PatchInfoAnd rt p) wU wR
us' FL (PatchInfoAnd rt p) wU wX
them' <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (rt :: RepoType) (p :: * -> * -> *) wX wY.
Commute p =>
PatchSet rt p Origin wX
-> PatchSet rt p Origin wY
-> Fork
     (PatchSet rt p)
     (FL (PatchInfoAnd rt p))
     (FL (PatchInfoAnd rt p))
     Origin
     wX
     wY
findCommonAndUncommon PatchSet rt p Origin wR
us PatchSet rt p Origin wX
them

  -- all patches in them' need to be available; check that
  let check :: PatchInfoAnd rt p wX wY -> Maybe PatchInfo
      check :: forall wX wY. PatchInfoAnd rt p wX wY -> Maybe PatchInfo
check PatchInfoAnd rt p wX wY
p = case forall (rt :: RepoType) (p :: * -> * -> *) wA wB.
PatchInfoAndG rt p wA wB -> Maybe (p wA wB)
hopefullyM PatchInfoAnd rt p wX wY
p of
        Maybe (Named p wX wY)
Nothing -> forall a. a -> Maybe a
Just (forall (rt :: RepoType) (p :: * -> * -> *) wA wB.
PatchInfoAndG rt p wA wB -> PatchInfo
info PatchInfoAnd rt p wX wY
p)
        Just Named p wX wY
_ -> forall a. Maybe a
Nothing
      bad :: [PatchInfo]
bad = forall a. [Maybe a] -> [a]
catMaybes (forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL forall wX wY. PatchInfoAnd rt p wX wY -> Maybe PatchInfo
check FL (PatchInfoAnd rt p) wU wX
them')
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PatchInfo]
bad) forall a b. (a -> b) -> a -> b
$
    forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$
    Doc -> String
renderString forall a b. (a -> b) -> a -> b
$
      ([Doc] -> Doc
vcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map PatchInfo -> Doc
displayPatchInfo [PatchInfo]
bad) Doc -> Doc -> Doc
$$ String -> Doc
text String
"" Doc -> Doc -> Doc
$$
      String -> Doc
text String
"Cannot apply this bundle. We are missing the above patches."

  (Bool
hadConflicts, Sealed FL (PatchInfoAnd rt p) wU wX
their_ps)
    <- if PrimDarcsOption (Maybe AllowConflicts)
O.conflictsNo forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts forall a. Eq a => a -> a -> Bool
== forall a. Maybe a
Nothing -- skip conflicts
        then forall (p :: * -> * -> *) (rt :: RepoType) wR wU wX wZ.
(RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wR
-> FL (PatchInfoAnd rt p) wX wR
-> FL (PatchInfoAnd rt p) wX wZ
-> IO (Bool, Sealed (FL (PatchInfoAnd rt p) wX))
filterOutConflicts Repository rt p wR wU wR
repository FL (PatchInfoAnd rt p) wU wR
us' FL (PatchInfoAnd rt p) wU wX
them'
        else forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, forall (a :: * -> *) wX. a wX -> Sealed a
Sealed FL (PatchInfoAnd rt p) wU wX
them')
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hadConflicts forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn String
"Skipping some patches which would cause conflicts."
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (a :: * -> * -> *) wX wZ. FL a wX wZ -> Bool
nullFL FL (PatchInfoAnd rt p) wU wX
their_ps) forall a b. (a -> b) -> a -> b
$
       do if Bool
hadConflicts
           then String -> IO ()
putStrLn (String
"All new patches of the bundle cause conflicts.  " forall a. [a] -> [a] -> [a]
++
                          String
"Nothing to do.") forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. IO a
exitSuccess
           else String -> IO ()
putStrLn (String
"All these patches have already been applied.  " forall a. [a] -> [a] -> [a]
++
                          String
"Nothing to do.") forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PrimDarcsOption Reorder
reorder forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts forall a. Eq a => a -> a -> Bool
/= Reorder
O.Reorder) forall a. IO a
exitSuccess
          
  let direction :: WhichChanges
direction = if PrimDarcsOption Bool
changesReverse forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts then WhichChanges
FirstReversed else WhichChanges
First
      selection_config :: SelectionConfig (PatchInfoAnd rt p)
selection_config = forall (p :: * -> * -> *).
Matchable p =>
WhichChanges
-> String
-> PatchSelectionOptions
-> Maybe (Splitter p)
-> Maybe [AnchoredPath]
-> SelectionConfig p
selectionConfig WhichChanges
direction String
"apply" ([DarcsFlag] -> PatchSelectionOptions
patchSelOpts [DarcsFlag]
opts) forall a. Maybe a
Nothing forall a. Maybe a
Nothing
  (FL (PatchInfoAnd rt p) wU wZ
to_be_applied :> FL (PatchInfoAnd rt p) wZ wX
_) <- forall (p :: * -> * -> *) wX wY.
(MatchableRP p, ShowPatch p, ShowContextPatch p,
 ApplyState p ~ Tree, ApplyState p ~ ApplyState (PrimOf p)) =>
FL p wX wY -> SelectionConfig p -> IO ((:>) (FL p) (FL p) wX wY)
runSelection FL (PatchInfoAnd rt p) wU wX
their_ps SelectionConfig (PatchInfoAnd rt p)
selection_config
  forall pa (rt :: RepoType) (p :: * -> * -> *) wR wU wZ.
(PatchApplier pa, ApplierRepoTypeConstraint pa rt, IsRepoType rt,
 RepoPatch p, ApplyState p ~ Tree) =>
pa
-> PatchProxy p
-> String
-> [DarcsFlag]
-> Repository rt p wR wU wR
-> Fork
     (PatchSet rt p)
     (FL (PatchInfoAnd rt p))
     (FL (PatchInfoAnd rt p))
     Origin
     wR
     wZ
-> IO ()
applyPatches pa
patchApplier PatchProxy p
patchProxy String
"apply" [DarcsFlag]
opts Repository rt p wR wU wR
repository (forall (common :: * -> * -> *) (left :: * -> * -> *)
       (right :: * -> * -> *) wA wX wY wU.
common wA wU
-> left wU wX -> right wU wY -> Fork common left right wA wX wY
Fork PatchSet rt p Origin wU
common FL (PatchInfoAnd rt p) wU wR
us' FL (PatchInfoAnd rt p) wU wZ
to_be_applied)

getPatchBundle :: RepoPatch p
               => [DarcsFlag]
               -> PatchSet rt p Origin wR
               -> B.ByteString
               -> IO (Either String (SealedPatchSet rt p Origin))
getPatchBundle :: forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
[DarcsFlag]
-> PatchSet rt p Origin wR
-> ByteString
-> IO (Either String (SealedPatchSet rt p Origin))
getPatchBundle [DarcsFlag]
opts PatchSet rt p Origin wR
us ByteString
fps = do
    let opt_verify :: Verify
opt_verify = forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags PrimDarcsOption Verify
O.verify [DarcsFlag]
opts
    Maybe ByteString
mps <- Verify -> ByteString -> IO (Maybe ByteString)
verifyPS Verify
opt_verify forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
readEmail ByteString
fps
    Maybe ByteString
mops <- Verify -> ByteString -> IO (Maybe ByteString)
verifyPS Verify
opt_verify ByteString
fps
    case (Maybe ByteString
mps, Maybe ByteString
mops) of
      (Maybe ByteString
Nothing, Maybe ByteString
Nothing) ->
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left String
"Patch bundle not properly signed, or gpg failed."
      (Just ByteString
bundle, Maybe ByteString
Nothing) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
PatchSet rt p Origin wR
-> ByteString -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle PatchSet rt p Origin wR
us ByteString
bundle
      (Maybe ByteString
Nothing, Just ByteString
bundle) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
PatchSet rt p Origin wR
-> ByteString -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle PatchSet rt p Origin wR
us ByteString
bundle
      -- We use careful_scan_bundle only below because in either of the two
      -- above case we know the patch was signed, so it really shouldn't
      -- need stripping of CRs.
      (Just ByteString
ps1, Just ByteString
ps2) -> case ByteString -> Either String (SealedPatchSet rt p Origin)
careful_scan_bundle ByteString
ps1 of
                              Left String
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteString -> Either String (SealedPatchSet rt p Origin)
careful_scan_bundle ByteString
ps2
                              Right SealedPatchSet rt p Origin
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right SealedPatchSet rt p Origin
x
          where careful_scan_bundle :: ByteString -> Either String (SealedPatchSet rt p Origin)
careful_scan_bundle ByteString
bundle =
                    case forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
PatchSet rt p Origin wR
-> ByteString -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle PatchSet rt p Origin wR
us ByteString
bundle of
                    Left String
e -> case forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
PatchSet rt p Origin wR
-> ByteString -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle PatchSet rt p Origin wR
us forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
stripCrPS ByteString
bundle of
                              Right SealedPatchSet rt p Origin
x -> forall a b. b -> Either a b
Right SealedPatchSet rt p Origin
x
                              Either String (SealedPatchSet rt p Origin)
_ -> forall a b. a -> Either a b
Left String
e
                    Either String (SealedPatchSet rt p Origin)
x -> Either String (SealedPatchSet rt p Origin)
x
                stripCrPS :: B.ByteString -> B.ByteString
                stripCrPS :: ByteString -> ByteString
stripCrPS ByteString
bundle = [ByteString] -> ByteString
unlinesPS forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map ByteString -> ByteString
stripline forall a b. (a -> b) -> a -> b
$ ByteString -> [ByteString]
linesPS ByteString
bundle
                stripline :: ByteString -> ByteString
stripline ByteString
p | ByteString -> Bool
B.null ByteString
p = ByteString
p
                            | ByteString -> Char
BC.last ByteString
p forall a. Eq a => a -> a -> Bool
== Char
'\r' = HasCallStack => ByteString -> ByteString
B.init ByteString
p
                            | Bool
otherwise = ByteString
p

parseAndInterpretBundle :: RepoPatch p
                        => PatchSet rt p Origin wR
                        -> B.ByteString
                        -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle :: forall (p :: * -> * -> *) (rt :: RepoType) wR.
RepoPatch p =>
PatchSet rt p Origin wR
-> ByteString -> Either String (SealedPatchSet rt p Origin)
parseAndInterpretBundle PatchSet rt p Origin wR
us ByteString
content = do
    Sealed Bundle rt p Any wX
bundle <- forall (p :: * -> * -> *) (rt :: RepoType) wX.
RepoPatch p =>
ByteString -> Either String (Sealed (Bundle rt p wX))
parseBundle ByteString
content
    forall (a :: * -> *) wX. a wX -> Sealed a
Sealed forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (p :: * -> * -> *) (rt :: RepoType) wT wA wB.
Commute p =>
PatchSet rt p Origin wT
-> Bundle rt p wA wB -> Either String (PatchSet rt p Origin wB)
interpretBundle PatchSet rt p Origin wR
us Bundle rt p Any wX
bundle

patchSelOpts :: [DarcsFlag] -> S.PatchSelectionOptions
patchSelOpts :: [DarcsFlag] -> PatchSelectionOptions
patchSelOpts [DarcsFlag]
flags = S.PatchSelectionOptions
    { verbosity :: Verbosity
S.verbosity = PrimDarcsOption Verbosity
verbosity forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
flags
    , matchFlags :: [MatchFlag]
S.matchFlags = forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags MatchOption
O.matchSeveral [DarcsFlag]
flags
    , interactive :: Bool
S.interactive = [DarcsFlag] -> Bool
maybeIsInteractive [DarcsFlag]
flags
    , 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 = PrimDarcsOption WithContext
withContext forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
flags
    }

maybeIsInteractive :: [DarcsFlag] -> Bool
maybeIsInteractive :: [DarcsFlag] -> Bool
maybeIsInteractive = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags PrimDarcsOption (Maybe Bool)
O.interactive