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

-- |
-- Module      : Darcs.UI.Commands.Dist
-- Copyright   : 2003 David Roundy
-- License     : GPL
-- Maintainer  : darcs-devel@darcs.net
-- Stability   : experimental
-- Portability : portable

module Darcs.UI.Commands.Dist
    (
      dist
    , doFastZip -- libdarcs export
    , doFastZip'
    ) where

import Darcs.Prelude hiding ( writeFile )

import Data.ByteString.Lazy ( writeFile )
import Control.Monad ( when )
import System.Directory ( createDirectory, setCurrentDirectory )
import System.Process ( system )
import System.Exit ( ExitCode(..), exitWith )
import System.FilePath.Posix ( takeFileName, (</>) )

import Darcs.Util.Workaround ( getCurrentDirectory )
import Codec.Archive.Tar ( pack, write )
import Codec.Archive.Tar.Entry ( entryPath )
import Codec.Compression.GZip ( compress )

import Codec.Archive.Zip ( emptyArchive, fromArchive, addEntryToArchive, toEntry )
import Darcs.Util.External ( fetchFilePS, Cachable( Uncachable ) )
import Darcs.Repository.Inventory ( peekPristineHash )
import Darcs.Repository.HashedIO ( pathsAndContents )
import Darcs.Repository.Paths ( hashedInventoryPath )
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import Darcs.UI.Flags as F ( DarcsFlag, useCache )
import qualified Darcs.UI.Flags as F ( setScriptsExecutable )
import Darcs.UI.Options
    ( (^), oid, odesc, ocheck
    , defaultFlags, parseFlags, (?)
    )
import qualified Darcs.UI.Options.All as O

import Darcs.UI.Commands
    ( DarcsCommand(..), withStdOpts, nodefaults, amInHashedRepository
    , putVerbose, putInfo
    )
import Darcs.UI.Completion ( noArgs )
import Darcs.Util.Lock ( withTempDir )
import Darcs.Patch.Match ( patchSetMatch )
import Darcs.Repository.Match ( getRecordedUpToMatch )
import Darcs.Repository ( withRepository, withRepositoryLocation, RepoJob(..),
                          setScriptsExecutable, repoCache,
                          createPartialsPristineDirectoryTree )
import Darcs.Repository.Prefs ( getPrefval )

import Darcs.Util.DateTime ( getCurrentTime, toSeconds )
import Darcs.Util.Path ( AbsolutePath, toFilePath, anchoredRoot )
import Darcs.Util.File ( withCurrentDirectory )
import Darcs.Util.Printer ( Doc, text, vcat )


distDescription :: String
distDescription :: String
distDescription = String
"Create a distribution archive."

distHelp :: Doc
distHelp :: Doc
distHelp = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
  [ String
"`darcs dist` creates a compressed archive in the repository's root"
  , String
"directory, containing the recorded state of the working tree"
  , String
"(unrecorded changes and the `_darcs` directory are excluded)."
  , String
"The command accepts matchers to create an archive of some past"
  , String
"repository state, for instance `--tag`."
  , String
""
  , String
"By default, the archive (and the top-level directory within the"
  , String
"archive) has the same name as the repository, but this can be"
  , String
"overridden with the `--dist-name` option."
  , String
""
  , String
"If a predist command is set (see `darcs setpref`), that command will"
  , String
"be run on the recorded state prior to archiving.  For example,"
  , String
"autotools projects would set it to `autoconf && automake`."
  , String
""
  , String
"If `--zip` is used, matchers and the predist command are ignored."
  ]

dist :: DarcsCommand
dist :: DarcsCommand
dist = DarcsCommand :: String
-> String
-> Doc
-> String
-> Int
-> [String]
-> ((AbsolutePath, AbsolutePath)
    -> [DarcsFlag] -> [String] -> IO ())
-> ([DarcsFlag] -> IO (Either String ()))
-> ((AbsolutePath, AbsolutePath)
    -> [DarcsFlag] -> [String] -> IO [String])
-> ([DarcsFlag] -> AbsolutePath -> [String] -> IO [String])
-> [DarcsOptDescr DarcsFlag]
-> [DarcsOptDescr DarcsFlag]
-> [DarcsFlag]
-> ([DarcsFlag] -> [String])
-> DarcsCommand
DarcsCommand
    { commandProgramName :: String
commandProgramName = String
"darcs"
    , commandName :: String
commandName = String
"dist"
    , commandHelp :: Doc
commandHelp = Doc
distHelp
    , commandDescription :: String
commandDescription = String
distDescription
    , commandExtraArgs :: Int
commandExtraArgs = Int
0
    , commandExtraArgHelp :: [String]
commandExtraArgHelp = []
    , commandCommand :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
commandCommand = (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
distCmd
    , 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]
noArgs
    , commandArgdefaults :: [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
commandArgdefaults = [DarcsFlag] -> AbsolutePath -> [String] -> IO [String]
nodefaults
    , commandAdvancedOptions :: [DarcsOptDescr DarcsFlag]
commandAdvancedOptions = []
    , commandBasicOptions :: [DarcsOptDescr DarcsFlag]
commandBasicOptions = OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Any)
-> [DarcsOptDescr DarcsFlag]
forall (d :: * -> *) f a b. OptSpec d f a b -> [d f]
odesc OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Any)
forall a.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
distBasicOpts
    , commandDefaults :: [DarcsFlag]
commandDefaults = OptSpec
  DarcsOptDescr
  DarcsFlag
  [DarcsFlag]
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> [DarcsFlag])
-> [DarcsFlag]
forall (d :: * -> *) f b. OptSpec d f [f] b -> [f]
defaultFlags OptSpec
  DarcsOptDescr
  DarcsFlag
  [DarcsFlag]
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> [DarcsFlag])
forall a.
DarcsOption
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
distOpts
    , commandCheckOptions :: [DarcsFlag] -> [String]
commandCheckOptions = OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> Any)
-> [DarcsFlag] -> [String]
forall (d :: * -> *) f a b. OptSpec d f a b -> [f] -> [String]
ocheck OptSpec
  DarcsOptDescr
  DarcsFlag
  Any
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> Any)
forall a.
DarcsOption
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
distOpts
    }
  where
    distBasicOpts :: OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
distBasicOpts
      = PrimOptSpec
  DarcsOptDescr
  DarcsFlag
  (Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
  (Maybe String)
PrimDarcsOption (Maybe String)
O.distname
      PrimOptSpec
  DarcsOptDescr
  DarcsFlag
  (Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
  (Maybe String)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
     (Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
  (Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
PrimDarcsOption Bool
O.distzip
      OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
     (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec
  DarcsOptDescr
  DarcsFlag
  ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
  (Maybe String -> [MatchFlag] -> SetScriptsExecutable -> Bool -> a)
PrimDarcsOption (Maybe String)
O.repoDir
      OptSpec
  DarcsOptDescr
  DarcsFlag
  ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (SetScriptsExecutable -> Bool -> a)
     ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (SetScriptsExecutable -> Bool -> a)
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec
  DarcsOptDescr
  DarcsFlag
  (SetScriptsExecutable -> Bool -> a)
  ([MatchFlag] -> SetScriptsExecutable -> Bool -> a)
MatchOption
O.matchUpToOne
      OptSpec
  DarcsOptDescr
  DarcsFlag
  (SetScriptsExecutable -> Bool -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Bool -> a)
     (SetScriptsExecutable -> Bool -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     (Bool -> a)
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec
  DarcsOptDescr
  DarcsFlag
  (Bool -> a)
  (SetScriptsExecutable -> Bool -> a)
PrimDarcsOption SetScriptsExecutable
O.setScriptsExecutable
      OptSpec
  DarcsOptDescr
  DarcsFlag
  (Bool -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
-> OptSpec DarcsOptDescr DarcsFlag a (Bool -> a)
-> OptSpec
     DarcsOptDescr
     DarcsFlag
     a
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> a)
forall (d :: * -> *) f b c a.
OptSpec d f b c -> OptSpec d f a b -> OptSpec d f a c
^ OptSpec DarcsOptDescr DarcsFlag a (Bool -> a)
PrimDarcsOption Bool
O.storeInMemory
    distOpts :: DarcsOption
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
distOpts = OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
forall a.
OptSpec
  DarcsOptDescr
  DarcsFlag
  a
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> a)
distBasicOpts OptSpec
  DarcsOptDescr
  DarcsFlag
  (Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
  (Maybe String
   -> Bool
   -> Maybe String
   -> [MatchFlag]
   -> SetScriptsExecutable
   -> Bool
   -> Maybe StdCmdAction
   -> Verbosity
   -> UseCache
   -> HooksConfig
   -> Bool
   -> Bool
   -> Bool
   -> a)
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
-> DarcsOption
     a
     (Maybe String
      -> Bool
      -> Maybe String
      -> [MatchFlag]
      -> SetScriptsExecutable
      -> Bool
      -> Maybe StdCmdAction
      -> Verbosity
      -> UseCache
      -> HooksConfig
      -> Bool
      -> Bool
      -> Bool
      -> a)
forall b c a.
DarcsOption (Maybe StdCmdAction -> Verbosity -> b) c
-> DarcsOption
     (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a) b
-> DarcsOption a c
`withStdOpts` DarcsOption
  (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
  (UseCache -> HooksConfig -> Bool -> Bool -> Bool -> a)
forall (d :: * -> *) f a. OptSpec d f a a
oid

distCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
distCmd :: (AbsolutePath, AbsolutePath) -> [DarcsFlag] -> [String] -> IO ()
distCmd (AbsolutePath, AbsolutePath)
_ [DarcsFlag]
opts [String]
_ | PrimDarcsOption Bool
O.distzip PrimDarcsOption Bool -> [DarcsFlag] -> Bool
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts = [DarcsFlag] -> IO ()
doFastZip [DarcsFlag]
opts
distCmd (AbsolutePath, AbsolutePath)
_ [DarcsFlag]
opts [String]
_ = UseCache -> RepoJob () -> IO ()
forall a. UseCache -> RepoJob a -> IO a
withRepository (PrimDarcsOption UseCache
useCache PrimDarcsOption UseCache -> [DarcsFlag] -> UseCache
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) (RepoJob () -> IO ()) -> RepoJob () -> IO ()
forall a b. (a -> b) -> a -> b
$ (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO ())
-> RepoJob ()
forall a.
(forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
RepoJob ((forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
  (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
  Repository rt p wR wU wR -> IO ())
 -> RepoJob ())
-> (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
    (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
    Repository rt p wR wU wR -> IO ())
-> RepoJob ()
forall a b. (a -> b) -> a -> b
$ \Repository rt p wR wU wR
repository -> do
  let matchFlags :: [MatchFlag]
matchFlags = MatchOption -> [DarcsFlag] -> [MatchFlag]
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
parseFlags MatchOption
O.matchUpToOne [DarcsFlag]
opts
  String
formerdir <- IO String
getCurrentDirectory
  let distname :: String
distname = String -> Maybe String -> String
getDistName String
formerdir (PrimDarcsOption (Maybe String)
O.distname PrimDarcsOption (Maybe String) -> [DarcsFlag] -> Maybe String
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)
  Maybe String
predist <- String -> IO (Maybe String)
getPrefval String
"predist"
  let resultfile :: String
resultfile = String
formerdir String -> String -> String
</> String
distname String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".tar.gz"
  String -> (AbsolutePath -> IO ()) -> IO ()
forall a. String -> (AbsolutePath -> IO a) -> IO a
withTempDir String
"darcsdist" ((AbsolutePath -> IO ()) -> IO ())
-> (AbsolutePath -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \AbsolutePath
tempdir -> do
      String -> IO ()
setCurrentDirectory String
formerdir
      let ddir :: String
ddir = AbsolutePath -> String
forall a. FilePathLike a => a -> String
toFilePath AbsolutePath
tempdir String -> String -> String
</> String
distname
      String -> IO ()
createDirectory String
ddir
      case [MatchFlag] -> Maybe PatchSetMatch
patchSetMatch [MatchFlag]
matchFlags of
        Just PatchSetMatch
psm -> String -> IO () -> IO ()
forall p a. FilePathLike p => p -> IO a -> IO a
withCurrentDirectory String
ddir (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Repository rt p wR wU wR -> PatchSetMatch -> IO ()
forall (p :: * -> * -> *) (rt :: RepoType) wR wU wT.
(ApplyMonad (ApplyState p) DefaultIO, IsRepoType rt, RepoPatch p,
 ApplyState p ~ Tree) =>
Repository rt p wR wU wT -> PatchSetMatch -> IO ()
getRecordedUpToMatch Repository rt p wR wU wR
repository PatchSetMatch
psm
        Maybe PatchSetMatch
Nothing -> Repository rt p wR wU wR -> [AnchoredPath] -> String -> IO ()
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> [AnchoredPath] -> String -> IO ()
createPartialsPristineDirectoryTree Repository rt p wR wU wR
repository [AnchoredPath
anchoredRoot] (String -> String
forall a. FilePathLike a => a -> String
toFilePath String
ddir)
      ExitCode
ec <- case Maybe String
predist of Maybe String
Nothing -> ExitCode -> IO ExitCode
forall (m :: * -> *) a. Monad m => a -> m a
return ExitCode
ExitSuccess
                            Just String
pd -> String -> IO ExitCode
system String
pd
      if ExitCode
ec ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
== ExitCode
ExitSuccess
        then do
          String -> IO () -> IO ()
forall p a. FilePathLike p => p -> IO a -> IO a
withCurrentDirectory String
ddir (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when
              (PrimDarcsOption SetScriptsExecutable
F.setScriptsExecutable PrimDarcsOption SetScriptsExecutable
-> [DarcsFlag] -> SetScriptsExecutable
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts SetScriptsExecutable -> SetScriptsExecutable -> Bool
forall a. Eq a => a -> a -> Bool
== SetScriptsExecutable
O.YesSetScriptsExecutable)
              IO ()
setScriptsExecutable
          [DarcsFlag] -> AbsolutePath -> String -> String -> IO ()
doDist [DarcsFlag]
opts AbsolutePath
tempdir String
distname String
resultfile
        else do
          String -> IO ()
putStrLn String
"Dist aborted due to predist failure"
          ExitCode -> IO ()
forall a. ExitCode -> IO a
exitWith ExitCode
ec


-- | This function performs the actual distribution action itself.
-- NB - it does /not/ perform the pre-dist, that should already
-- have completed successfully before this is invoked.
doDist :: [DarcsFlag] -> AbsolutePath -> String -> FilePath -> IO ()
doDist :: [DarcsFlag] -> AbsolutePath -> String -> String -> IO ()
doDist [DarcsFlag]
opts AbsolutePath
tempdir String
name String
resultfile = do
    String -> IO ()
setCurrentDirectory (AbsolutePath -> String
forall a. FilePathLike a => a -> String
toFilePath AbsolutePath
tempdir)
    [Entry]
entries <- String -> [String] -> IO [Entry]
pack String
"." [String
name]
    [DarcsFlag] -> Doc -> IO ()
putVerbose [DarcsFlag]
opts (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Entry -> Doc) -> [Entry] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Doc
text (String -> Doc) -> (Entry -> String) -> Entry -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Entry -> String
entryPath) [Entry]
entries
    String -> ByteString -> IO ()
writeFile String
resultfile (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
compress (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ [Entry] -> ByteString
write [Entry]
entries
    [DarcsFlag] -> Doc -> IO ()
putInfo [DarcsFlag]
opts (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String
"Created dist as " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
resultfile


getDistName :: FilePath -> Maybe String -> FilePath
getDistName :: String -> Maybe String -> String
getDistName String
_ (Just String
dn) = String -> String
takeFileName String
dn
getDistName String
currentDirectory Maybe String
_ = String -> String
takeFileName String
currentDirectory

doFastZip :: [DarcsFlag] -> IO ()
doFastZip :: [DarcsFlag] -> IO ()
doFastZip [DarcsFlag]
opts = do
  String
currentdir <- IO String
getCurrentDirectory
  let distname :: String
distname = String -> Maybe String -> String
getDistName String
currentdir (PrimDarcsOption (Maybe String)
O.distname PrimDarcsOption (Maybe String) -> [DarcsFlag] -> Maybe String
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)
  let resultfile :: String
resultfile = String
currentdir String -> String -> String
</> String
distname String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".zip"
  [DarcsFlag] -> String -> (ByteString -> IO ()) -> IO ()
forall a. [DarcsFlag] -> String -> (ByteString -> IO a) -> IO a
doFastZip' [DarcsFlag]
opts String
currentdir (String -> ByteString -> IO ()
writeFile String
resultfile)
  [DarcsFlag] -> Doc -> IO ()
putInfo [DarcsFlag]
opts (Doc -> IO ()) -> Doc -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String
"Created " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
resultfile

doFastZip' :: [DarcsFlag]              -- ^ Flags/options
           -> FilePath                 -- ^ The path to the repository
           -> (BL.ByteString -> IO a)  -- ^ An action to perform on the archive contents
           -> IO a
doFastZip' :: [DarcsFlag] -> String -> (ByteString -> IO a) -> IO a
doFastZip' [DarcsFlag]
opts String
path ByteString -> IO a
act = UseCache -> String -> RepoJob a -> IO a
forall a. UseCache -> String -> RepoJob a -> IO a
withRepositoryLocation (PrimDarcsOption UseCache
useCache PrimDarcsOption UseCache -> [DarcsFlag] -> UseCache
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts) String
path (RepoJob a -> IO a) -> RepoJob a -> IO a
forall a b. (a -> b) -> a -> b
$ (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
forall a.
(forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
 (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
 Repository rt p wR wU wR -> IO a)
-> RepoJob a
RepoJob ((forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
  (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
  Repository rt p wR wU wR -> IO a)
 -> RepoJob a)
-> (forall (rt :: RepoType) (p :: * -> * -> *) wR wU.
    (IsRepoType rt, RepoPatch p, ApplyState p ~ Tree) =>
    Repository rt p wR wU wR -> IO a)
-> RepoJob a
forall a b. (a -> b) -> a -> b
$ \Repository rt p wR wU wR
repo -> do
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (PrimDarcsOption SetScriptsExecutable
F.setScriptsExecutable PrimDarcsOption SetScriptsExecutable
-> [DarcsFlag] -> SetScriptsExecutable
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts SetScriptsExecutable -> SetScriptsExecutable -> Bool
forall a. Eq a => a -> a -> Bool
== SetScriptsExecutable
O.YesSetScriptsExecutable) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    String -> IO ()
putStrLn String
"WARNING: Zip archives cannot store executable flag."  
  let distname :: String
distname = String -> Maybe String -> String
getDistName String
path (PrimDarcsOption (Maybe String)
O.distname PrimDarcsOption (Maybe String) -> [DarcsFlag] -> Maybe String
forall (d :: * -> *) f v.
(forall a. PrimOptSpec d f a v) -> [f] -> v
? [DarcsFlag]
opts)
  ByteString
i <- String -> Cachable -> IO ByteString
fetchFilePS (String
path String -> String -> String
</> String
hashedInventoryPath) Cachable
Uncachable
  [(String, ByteString)]
pristine <- String -> Cache -> PristineHash -> IO [(String, ByteString)]
pathsAndContents (String
distname String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/") (Repository rt p wR wU wR -> Cache
forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> Cache
repoCache Repository rt p wR wU wR
repo) (ByteString -> PristineHash
peekPristineHash ByteString
i)
  Integer
epochtime <- UTCTime -> Integer
toSeconds (UTCTime -> Integer) -> IO UTCTime -> IO Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO UTCTime
getCurrentTime
  let entries :: [Entry]
entries = [ String -> Integer -> ByteString -> Entry
toEntry String
filepath Integer
epochtime (ByteString -> ByteString
toLazy ByteString
contents) | (String
filepath,ByteString
contents) <- [(String, ByteString)]
pristine ]
  let archive :: Archive
archive = (Entry -> Archive -> Archive) -> Archive -> [Entry] -> Archive
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Entry -> Archive -> Archive
addEntryToArchive Archive
emptyArchive [Entry]
entries
  ByteString -> IO a
act (Archive -> ByteString
fromArchive Archive
archive)


toLazy :: B.ByteString -> BL.ByteString
toLazy :: ByteString -> ByteString
toLazy ByteString
bs = [ByteString] -> ByteString
BL.fromChunks [ByteString
bs]