--  Copyright (C) 2003-2004 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.Diff ( diffCommand ) where

import Darcs.Prelude hiding ( all )

import Control.Monad ( unless, when )
import Data.Maybe ( fromMaybe )
import Data.Maybe ( isJust )
import System.Directory ( copyFile, createDirectory, findExecutable, listDirectory )
import System.FilePath.Posix ( takeFileName, (</>) )

import Darcs.Patch ( listTouchedFiles )
import Darcs.Patch.Apply ( Apply(..) )
import Darcs.Patch.Depends ( findCommonWithThem )
import Darcs.Patch.Info ( displayPatchInfo )
import Darcs.Patch.Match ( matchFirstPatchset, matchSecondPatchset, secondMatch )
import Darcs.Patch.Named ( anonymous )
import Darcs.Patch.PatchInfoAnd ( info, n2pia )
import Darcs.Patch.Set ( patchSetSnoc )
import Darcs.Patch.Witnesses.Ordered ( (:>)(..), mapFL )
import Darcs.Patch.Witnesses.Sealed ( Sealed(..), seal )
import Darcs.Repository ( RepoJob(..), readRepo, withRepository )
import Darcs.Repository.Flags ( DiffAlgorithm(MyersDiff), WantGuiPause(..) )
import Darcs.Repository.Paths ( pristineDirPath )
import Darcs.Repository.State
    ( ScanKnown(..)
    , applyTreeFilter
    , readRecorded
    , restrictSubpaths
    , unrecordedChanges
    )
import Darcs.UI.Commands
    ( DarcsCommand(..)
    , amInHashedRepository
    , nodefaults
    , withStdOpts
    )
import Darcs.UI.Completion ( knownFileArgs )
import Darcs.UI.External ( diffProgram )
import Darcs.UI.Flags ( DarcsFlag, pathSetFromArgs, useCache, wantGuiPause )
import Darcs.UI.Options ( defaultFlags, ocheck, odesc, parseFlags, (?), (^) )
import qualified Darcs.UI.Options.All as O
import Darcs.Util.CommandLine ( parseCmd )
import Darcs.Util.Exec ( execInteractive )
import Darcs.Util.File ( withCurrentDirectory )
import Darcs.Util.Global ( debugMessage )
import Darcs.Util.Lock ( withTempDir )
import Darcs.Util.Path ( AbsolutePath, AnchoredPath, isPrefix, toFilePath )
import Darcs.Util.Printer ( Doc, putDoc, text, vcat )
import Darcs.Util.Prompt ( askEnter )
import Darcs.Util.Tree.Hashed ( hashedTreeIO )
import Darcs.Util.Tree.Plain ( writePlainTree )
import Darcs.Util.Workaround ( getCurrentDirectory )

diffDescription :: String
diffDescription :: String
diffDescription = String
"Create a diff between two versions of the repository."

diffHelp :: Doc
diffHelp :: Doc
diffHelp = String -> Doc
text forall a b. (a -> b) -> a -> b
$
 String
"The `darcs diff` command compares two versions of the working tree of\n" forall a. [a] -> [a] -> [a]
++
 String
"the current repository.  Without options, the pristine (recorded) and\n" forall a. [a] -> [a] -> [a]
++
 String
"unrecorded working trees are compared.  This is lower-level than\n" forall a. [a] -> [a] -> [a]
++
 String
"the `darcs whatsnew` command, since it outputs a line-by-line diff,\n" forall a. [a] -> [a] -> [a]
++
 String
"and it is also slower.  As with `darcs whatsnew`, if you specify\n" forall a. [a] -> [a] -> [a]
++
 String
"files or directories, changes to other files are not listed.\n" forall a. [a] -> [a] -> [a]
++
 String
"The command always uses an external diff utility.\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"With the `--patch` option, the comparison will be made between working\n" forall a. [a] -> [a] -> [a]
++
 String
"trees with and without that patch.  Patches *after* the selected patch\n" forall a. [a] -> [a] -> [a]
++
 String
"are not present in either of the compared working trees.  The\n" forall a. [a] -> [a] -> [a]
++
 String
"`--from-patch` and `--to-patch` options allow the set of patches in the\n" forall a. [a] -> [a] -> [a]
++
 String
"`old' and `new' working trees to be specified separately.\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"The associated tag and match options are also understood, e.g. `darcs\n" forall a. [a] -> [a] -> [a]
++
 String
"diff --from-tag 1.0 --to-tag 1.1`.  All these options assume an\n" forall a. [a] -> [a] -> [a]
++
 String
"ordering of the patch set, so results may be affected by operations\n" forall a. [a] -> [a] -> [a]
++
 String
"such as `darcs optimize reorder`.\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"diff(1) is always called with the arguments `-rN` and by default also\n" forall a. [a] -> [a] -> [a]
++
 String
"with `-u` to show the differences in unified format. This can be turned\n" forall a. [a] -> [a] -> [a]
++
 String
"off by passing `--no-unified`. An additional argument can be passed\n" forall a. [a] -> [a] -> [a]
++
 String
"using `--diff-opts`, such as `--diff-opts=-ud` or `--diff-opts=-wU9`.\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"The `--diff-command` option can be used to specify an alternative\n" forall a. [a] -> [a] -> [a]
++
 String
"utility. Arguments may be included, separated by whitespace.  The value\n" forall a. [a] -> [a] -> [a]
++
 String
"is not interpreted by a shell, so shell constructs cannot be used.  The\n" forall a. [a] -> [a] -> [a]
++
 String
"arguments %1 and %2 MUST be included, these are substituted for the two\n" forall a. [a] -> [a] -> [a]
++
 String
"working trees being compared. For instance:\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"    darcs diff -p . --diff-command \"meld %1 %2\"\n" forall a. [a] -> [a] -> [a]
++
 String
"\n" forall a. [a] -> [a] -> [a]
++
 String
"If this option is used, `--diff-opts` is ignored.\n"

diffCommand :: DarcsCommand
diffCommand :: DarcsCommand
diffCommand = DarcsCommand
    { commandProgramName :: String
commandProgramName = String
"darcs"
    , commandName :: String
commandName = String
"diff"
    , commandHelp :: Doc
commandHelp = Doc
diffHelp
    , commandDescription :: String
commandDescription = String
diffDescription
    , commandExtraArgs :: Int
commandExtraArgs = -Int
1
    , commandExtraArgHelp :: [String]
commandExtraArgHelp = [String
"[FILE or DIRECTORY]..."]
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
commandCommand = (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
diffCmd
    , 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 = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall {a}.
OptSpec DarcsOptDescr DarcsFlag a (WantGuiPause -> UseIndex -> a)
diffAdvancedOpts
    , commandBasicOptions :: [DarcsOptDescr DarcsFlag]
commandBasicOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  ([MatchFlag] -> ExternalDiff -> Maybe String -> Bool -> a)
diffBasicOpts
    , commandDefaults :: [DarcsFlag]
commandDefaults = forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags forall {a}.
DarcsOption
  a
  ([MatchFlag]
   -> ExternalDiff
   -> Maybe String
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> WantGuiPause
   -> UseIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
diffOpts
    , commandCheckOptions :: [DarcsFlag] -> [String]
commandCheckOptions = forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [String]
ocheck forall {a}.
DarcsOption
  a
  ([MatchFlag]
   -> ExternalDiff
   -> Maybe String
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> WantGuiPause
   -> UseIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
diffOpts
    }
  where
    diffBasicOpts :: OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  ([MatchFlag] -> ExternalDiff -> Maybe String -> Bool -> a)
diffBasicOpts
      = MatchOption
O.matchOneOrRange
      forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ PrimDarcsOption ExternalDiff
O.extDiff
      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 Bool
O.storeInMemory
    diffAdvancedOpts :: OptSpec DarcsOptDescr DarcsFlag a (WantGuiPause -> UseIndex -> a)
diffAdvancedOpts = PrimDarcsOption WantGuiPause
O.pauseForGui 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
    diffOpts :: DarcsOption
  a
  ([MatchFlag]
   -> ExternalDiff
   -> Maybe String
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> WantGuiPause
   -> UseIndex
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
diffOpts = forall {a}.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  ([MatchFlag] -> ExternalDiff -> Maybe String -> Bool -> a)
diffBasicOpts 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 (WantGuiPause -> UseIndex -> a)
diffAdvancedOpts

diffCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
diffCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
diffCmd (AbsolutePath, AbsolutePath)
fps [DarcsFlag]
opts [String]
args
  | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null (MatchOption
O.matchLast forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)) Bool -> Bool -> Bool
&&
      Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null (MatchOption
O.matchFrom forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)) =
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"using --patch and --last at the same time with the 'diff'" forall a. [a] -> [a] -> [a]
++
          String
" command doesn't make sense. Use --from-patch to create a diff" forall a. [a] -> [a] -> [a]
++
          String
" from this patch to the present, or use just '--patch' to view" forall a. [a] -> [a] -> [a]
++
          String
" this specific patch."
  | Bool
otherwise = [DarcsFlag] -> Maybe [AnchoredPath] -> IO ()
doDiff [DarcsFlag]
opts forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (AbsolutePath, AbsolutePath)
-> [String] -> IO (Maybe [AnchoredPath])
pathSetFromArgs (AbsolutePath, AbsolutePath)
fps [String]
args

doDiff :: [DarcsFlag] -> Maybe [AnchoredPath] ->  IO ()
doDiff :: [DarcsFlag] -> Maybe [AnchoredPath] -> IO ()
doDiff [DarcsFlag]
opts Maybe [AnchoredPath]
mpaths = forall a. UseCache -> RepoJob a -> IO a
withRepository (PrimDarcsOption UseCache
useCache forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) 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 -> do
  PatchSet rt p Origin wR
patchset <- 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
  -- We pass @mpaths@ here which means we get only the changes that affect the
  -- given paths (if any).
  FL (PrimOf p) wR wU
unrecorded <- 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) wR wU)
unrecordedChanges (PrimDarcsOption UseIndex
O.useIndex forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts, ScanKnown
ScanKnown, DiffAlgorithm
MyersDiff)
    LookForMoves
O.NoLookForMoves LookForReplaces
O.NoLookForReplaces
    Repository rt p wR wU wR
repository Maybe [AnchoredPath]
mpaths
  -- Use of 'anonymous' is unproblematic here as we don't store any patches.
  -- But we must take care not to show its fake patch info to the user.
  PatchInfoAndG rt (Named p) wR wU
unrecorded' <- forall (p :: * -> * -> *) wX wY (rt :: RepoType).
(Ident p, PatchId p ~ PatchInfo) =>
p wX wY -> PatchInfoAndG rt p wX wY
n2pia forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall (p :: * -> * -> *) wX wY.
FromPrim p =>
FL (PrimOf p) wX wY -> IO (Named p wX wY)
anonymous FL (PrimOf p) wR wU
unrecorded

  let matchFlags :: [MatchFlag]
matchFlags = forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags MatchOption
O.matchOneOrRange [DarcsFlag]
opts

  -- If no secondMatch (--to-xxx) is specified, include unrecorded changes
  Sealed PatchSet rt p Origin wX
all <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    if [MatchFlag] -> Bool
secondMatch [MatchFlag]
matchFlags
      then forall (a :: * -> *) wX. a wX -> Sealed a
seal PatchSet rt p Origin wR
patchset
      else forall (a :: * -> *) wX. a wX -> Sealed a
seal forall a b. (a -> b) -> a -> b
$ forall (rt :: RepoType) (p :: * -> * -> *) wX wY wZ.
PatchSet rt p wX wY
-> PatchInfoAnd rt p wY wZ -> PatchSet rt p wX wZ
patchSetSnoc PatchSet rt p Origin wR
patchset PatchInfoAndG rt (Named p) wR wU
unrecorded'
  -- Note how this differs from how firstMatch defaults for log command
  Sealed PatchSet rt p Origin wX
ctx <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    forall a. a -> Maybe a -> a
fromMaybe (forall (a :: * -> *) wX. a wX -> Sealed a
seal PatchSet rt p Origin wR
patchset) forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wStart wX.
MatchableRP p =>
[MatchFlag]
-> PatchSet rt p wStart wX -> Maybe (SealedPatchSet rt p wStart)
matchFirstPatchset [MatchFlag]
matchFlags PatchSet rt p Origin wR
patchset
  Sealed PatchSet rt p Origin wX
match <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    forall a. a -> Maybe a -> a
fromMaybe (forall (a :: * -> *) wX. a wX -> Sealed a
seal PatchSet rt p Origin wX
all) forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wStart wX.
MatchableRP p =>
[MatchFlag]
-> PatchSet rt p wStart wX -> Maybe (SealedPatchSet rt p wStart)
matchSecondPatchset [MatchFlag]
matchFlags PatchSet rt p Origin wR
patchset

  (PatchSet rt p Origin wZ
_ :> FL (PatchInfoAnd rt p) wZ wX
todiff) <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
Commute p =>
PatchSet rt p Origin wX
-> PatchSet rt p Origin wY
-> (:>) (PatchSet rt p) (FL (PatchInfoAnd rt p)) Origin wX
findCommonWithThem PatchSet rt p Origin wX
match PatchSet rt p Origin wX
ctx
  (PatchSet rt p Origin wZ
_ :> FL (PatchInfoAnd rt p) wZ wX
tounapply) <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
Commute p =>
PatchSet rt p Origin wX
-> PatchSet rt p Origin wY
-> (:>) (PatchSet rt p) (FL (PatchInfoAnd rt p)) Origin wX
findCommonWithThem PatchSet rt p Origin wX
all PatchSet rt p Origin wX
match

  Sealed PatchSet rt p Origin wX
logmatch <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    if [MatchFlag] -> Bool
secondMatch [MatchFlag]
matchFlags
      then forall (a :: * -> *) wX. a wX -> Sealed a
seal PatchSet rt p Origin wX
match
      else forall (a :: * -> *) wX. a wX -> Sealed a
seal PatchSet rt p Origin wR
patchset
  -- Same as @todiff@ but without trailing @unrecorded'@ changes
  (PatchSet rt p Origin wZ
_ :> FL (PatchInfoAnd rt p) wZ wX
tolog) <- forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (p :: * -> * -> *) (rt :: RepoType) wX wY.
Commute p =>
PatchSet rt p Origin wX
-> PatchSet rt p Origin wY
-> (:>) (PatchSet rt p) (FL (PatchInfoAnd rt p)) Origin wX
findCommonWithThem PatchSet rt p Origin wX
logmatch PatchSet rt p Origin wX
ctx

  let touched :: [AnchoredPath]
touched = forall (p :: * -> * -> *) wX wY.
PatchInspect p =>
p wX wY -> [AnchoredPath]
listTouchedFiles FL (PatchInfoAnd rt p) wZ wX
todiff
      files :: [AnchoredPath]
files = case Maybe [AnchoredPath]
mpaths of
               Maybe [AnchoredPath]
Nothing -> [AnchoredPath]
touched
               Just [AnchoredPath]
paths ->
                  forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\AnchoredPath
path -> forall a. (a -> Bool) -> [a] -> [a]
filter (AnchoredPath -> AnchoredPath -> Bool
isPrefix AnchoredPath
path) [AnchoredPath]
touched) [AnchoredPath]
paths
  TreeFilter IO
relevant <- forall (p :: * -> * -> *) (rt :: RepoType) wR wU wT (m :: * -> *).
(RepoPatch p, ApplyState p ~ Tree) =>
Repository rt p wR wU wT -> [AnchoredPath] -> IO (TreeFilter m)
restrictSubpaths Repository rt p wR wU wR
repository [AnchoredPath]
files

  String
formerdir <- IO String
getCurrentDirectory
  let thename :: String
thename = String -> String
takeFileName String
formerdir
  forall a. String -> (AbsolutePath -> IO a) -> IO a
withTempDir String
"darcs-diff" forall a b. (a -> b) -> a -> b
$ \AbsolutePath
tmpdir -> do
      IO String
getCurrentDirectory forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO ()
debugMessage forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"doDiff: I am now in "forall a. [a] -> [a] -> [a]
++)
      let pdir :: String
pdir = forall a. FilePathLike a => a -> String
toFilePath AbsolutePath
tmpdir String -> String -> String
</> (String
"pristine.hashed-"forall a. [a] -> [a] -> [a]
++String
thename)
      String -> IO ()
createDirectory String
pdir
      let odir :: String
odir = forall a. FilePathLike a => a -> String
toFilePath AbsolutePath
tmpdir String -> String -> String
</> (String
"old-"forall a. [a] -> [a] -> [a]
++String
thename)
      String -> IO ()
createDirectory String
odir
      let ndir :: String
ndir = forall a. FilePathLike a => a -> String
toFilePath AbsolutePath
tmpdir String -> String -> String
</> (String
"new-"forall a. [a] -> [a] -> [a]
++String
thename)
      String -> IO ()
createDirectory String
ndir

      -- Prepare the (plain) trees we want to compare. Since we need to access
      -- our repository, we have to restore the working directory.
      forall p a. FilePathLike p => p -> IO a -> IO a
withCurrentDirectory String
formerdir forall a b. (a -> b) -> a -> b
$ do
        -- Make a temporary copy of pristineDirPath where we have write access.
        -- The result (@pdirpath@) serves as our storage for hashed 'Tree' items
        -- during the 'apply' and 'unapply' operations below.
        let pdirpath :: String
pdirpath = forall a. FilePathLike a => a -> String
toFilePath String
pdir
        [String]
pfiles <- String -> IO [String]
listDirectory String
pristineDirPath
        let copy :: String -> String -> String -> IO ()
copy String
srcdir String
destdir String
name = String -> String -> IO ()
copyFile (String
srcdirString -> String -> String
</>String
name) (String
destdirString -> String -> String
</>String
name)
        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> String -> String -> IO ()
copy String
pristineDirPath String
pdirpath) [String]
pfiles

        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

        -- @base@ will be like our working tree, /except/ that it contains only
        -- the unrecorded changes that affect the given file paths, see comment
        -- above when we called 'unrecordedChanges'.
        Tree IO
base <- if [MatchFlag] -> Bool
secondMatch [MatchFlag]
matchFlags
                 then forall (m :: * -> *) a. Monad m => a -> m a
return Tree IO
pristine
                 else forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. TreeIO a -> Tree IO -> String -> IO (a, Tree IO)
hashedTreeIO (forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
apply PatchInfoAndG rt (Named p) wR wU
unrecorded') Tree IO
pristine String
pdirpath

        Tree IO
newtree <- forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. TreeIO a -> Tree IO -> String -> IO (a, Tree IO)
hashedTreeIO (forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
unapply FL (PatchInfoAnd rt p) wZ wX
tounapply) Tree IO
base String
pdirpath
        -- @todiff@ may have our @unrecorded'@ changes as its last element. If
        -- we used our full working tree as @base@, then we would now unapply
        -- filtered changes from an unfiltered 'Tree', so the result would be
        -- the pristine Tree with the filtered-out unrecorded changes /still
        -- applied/. Unapplying the (unfiltered) recorded changes that touch
        -- paths that we filtered out would then fail (issue2639).
        -- We cannot use 'readUnrecorded' and pass it @mpaths@ because that
        -- would filter the whole Tree, so again unapplying recorded changes
        -- that touch irrelevant paths would fail.
        -- A valid alternative solution would be to not pre-filter unrecorded
        -- changes at all, since we filter the resulting Trees anyway (see
        -- below). But that may be less efficient if there are many unrecorded
        -- changes but we are interested in just a small subset of the affected
        -- paths.
        Tree IO
oldtree <- forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. TreeIO a -> Tree IO -> String -> IO (a, Tree IO)
hashedTreeIO (forall (p :: * -> * -> *) (m :: * -> *) wX wY.
(Apply p, ApplyMonad (ApplyState p) m) =>
p wX wY -> m ()
unapply FL (PatchInfoAnd rt p) wZ wX
todiff) Tree IO
newtree String
pdirpath

        Tree IO -> String -> IO ()
writePlainTree (forall (m :: * -> *).
TreeFilter m
-> forall (tr :: (* -> *) -> *). FilterTree tr m => tr m -> tr m
applyTreeFilter TreeFilter IO
relevant Tree IO
oldtree) (forall a. FilePathLike a => a -> String
toFilePath String
odir)
        Tree IO -> String -> IO ()
writePlainTree (forall (m :: * -> *).
TreeFilter m
-> forall (tr :: (* -> *) -> *). FilterTree tr m => tr m -> tr m
applyTreeFilter TreeFilter IO
relevant Tree IO
newtree) (forall a. FilePathLike a => a -> String
toFilePath String
ndir)
        -- Display patch info for (only) the recorded patches that we diff
        Doc -> IO ()
putDoc 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 forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ forall (a :: * -> * -> *) b wX wY.
(forall wW wZ. a wW wZ -> b) -> FL a wX wY -> [b]
mapFL forall (rt :: RepoType) (p :: * -> * -> *) wA wB.
PatchInfoAndG rt p wA wB -> PatchInfo
info FL (PatchInfoAnd rt p) wZ wX
tolog

      -- Call the external diff program. Note we are now back in our
      -- temporary directory.
      String
cmd <- IO String
diffProgram
      let old :: String
old = String -> String
takeFileName forall a b. (a -> b) -> a -> b
$ forall a. FilePathLike a => a -> String
toFilePath String
odir
          new :: String
new = String -> String
takeFileName forall a b. (a -> b) -> a -> b
$ forall a. FilePathLike a => a -> String
toFilePath String
ndir
      case String
-> [DarcsFlag]
-> String
-> String
-> Either String (String, [String])
getDiffCmdAndArgs String
cmd [DarcsFlag]
opts String
old String
new of
        Left String
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err
        Right (String
d_cmd, [String]
d_args) -> do
          Maybe String
cmdExists <- String -> IO (Maybe String)
findExecutable String
d_cmd
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall a. Maybe a -> Bool
isJust Maybe String
cmdExists) forall a b. (a -> b) -> a -> b
$
            forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
d_cmd forall a. [a] -> [a] -> [a]
++ String
" is not an executable in --diff-command"
          let pausingForGui :: Bool
pausingForGui = ([DarcsFlag] -> WantGuiPause
wantGuiPause [DarcsFlag]
opts forall a. Eq a => a -> a -> Bool
== WantGuiPause
YesWantGuiPause)
              cmdline :: String
cmdline = [String] -> String
unwords (String
d_cmd forall a. a -> [a] -> [a]
: [String]
d_args)
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
pausingForGui forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ String
"Running command '" forall a. [a] -> [a] -> [a]
++ String
cmdline forall a. [a] -> [a] -> [a]
++ String
"'"
          ExitCode
_ <- String -> Maybe String -> IO ExitCode
execInteractive String
cmdline forall a. Maybe a
Nothing
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
pausingForGui forall a b. (a -> b) -> a -> b
$ String -> IO ()
askEnter String
"Hit return to move on..."

-- | Returns the command we should use for diff as a tuple (command, arguments).
-- This will either be whatever the user specified via --diff-command  or the
-- default 'diffProgram'.  Note that this potentially involves parsing the
-- user's diff-command, hence the possibility for failure.
getDiffCmdAndArgs :: String -> [DarcsFlag] -> String -> String
                  -> Either String (String, [String])
getDiffCmdAndArgs :: String
-> [DarcsFlag]
-> String
-> String
-> Either String (String, [String])
getDiffCmdAndArgs String
cmd [DarcsFlag]
opts String
f1 String
f2 = ExternalDiff -> Either String (String, [String])
helper (PrimDarcsOption ExternalDiff
O.extDiff forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) where
  helper :: ExternalDiff -> Either String (String, [String])
helper ExternalDiff
extDiff =
    case ExternalDiff -> Maybe String
O.diffCmd ExternalDiff
extDiff of
      Just String
c ->
        case FTable -> String -> Either ParseError ([String], Bool)
parseCmd [ (Char
'1', String
f1) , (Char
'2', String
f2) ] String
c of
          Left ParseError
err      -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show ParseError
err
          Right ([],Bool
_)  -> forall a. HasCallStack => String -> a
error String
"parseCmd should never return empty list"
          Right (String
cmd':[String]
args,Bool
_)
            | forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
== String
f1) [String]
args) forall a. Eq a => a -> a -> Bool
== Int
1
            , forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
== String
f2) [String]
args) forall a. Eq a => a -> a -> Bool
== Int
1 -> forall a b. b -> Either a b
Right (String
cmd',[String]
args)
            | Bool
otherwise -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ String
"Invalid argument (%1 or %2) in --diff-command"
      Maybe String
Nothing -> -- if no command specified, use 'diff'
        forall a b. b -> Either a b
Right (String
cmd, String
"-rN"forall a. a -> [a] -> [a]
:ExternalDiff -> [String]
getDiffOpts ExternalDiff
extDiffforall a. [a] -> [a] -> [a]
++[String
f1,String
f2])

getDiffOpts :: O.ExternalDiff -> [String]
getDiffOpts :: ExternalDiff -> [String]
getDiffOpts O.ExternalDiff {diffOpts :: ExternalDiff -> [String]
O.diffOpts=[String]
os,diffUnified :: ExternalDiff -> Bool
O.diffUnified=Bool
u} = [String] -> [String]
addUnified [String]
os where
  addUnified :: [String] -> [String]
addUnified = if Bool
u then (String
"-u"forall a. a -> [a] -> [a]
:) else forall a. a -> a
id