-- Copyright (C) 2006-2007 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; if not, write to the Free Software Foundation,
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

module Darcs.Repository.InternalTypes ( Repository, PristineType(..)
                                      , repoCache, modifyCache
                                      , repoPatchType
                                      , repoFormat
                                      , repoLocation
                                      , withRepoLocation
                                      , repoPristineType
                                      , coerceR
                                      , coerceU
                                      , coerceT
                                      , mkRepo
                                      ) where

import Prelude ()
import Darcs.Prelude

import Data.Coerce ( coerce )
import Data.List ( nub, sortBy )
import Darcs.Repository.Cache ( Cache (..) , compareByLocality )
import Darcs.Repository.Format ( RepoFormat )
import Darcs.Patch ( RepoType )
import Darcs.Patch.Type ( PatchType(..) )
import Darcs.Util.File ( withCurrentDirectory )

data PristineType
  = NoPristine
  | PlainPristine
  | HashedPristine
    deriving ( Int -> PristineType -> ShowS
[PristineType] -> ShowS
PristineType -> String
(Int -> PristineType -> ShowS)
-> (PristineType -> String)
-> ([PristineType] -> ShowS)
-> Show PristineType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PristineType] -> ShowS
$cshowList :: [PristineType] -> ShowS
show :: PristineType -> String
$cshow :: PristineType -> String
showsPrec :: Int -> PristineType -> ShowS
$cshowsPrec :: Int -> PristineType -> ShowS
Show, PristineType -> PristineType -> Bool
(PristineType -> PristineType -> Bool)
-> (PristineType -> PristineType -> Bool) -> Eq PristineType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PristineType -> PristineType -> Bool
$c/= :: PristineType -> PristineType -> Bool
== :: PristineType -> PristineType -> Bool
$c== :: PristineType -> PristineType -> Bool
Eq )

-- |A @Repository@ is a token representing the state of a repository on disk.
-- It is parameterized by the patch type in the repository, and witnesses for
-- the recorded state of the repository (i.e. what darcs get would retrieve),
-- the unrecorded state (what's in the working directory now),
-- and the tentative state, which represents work in progress that will
-- eventually become the new recorded state unless something goes wrong.
data Repository (rt :: RepoType) (p :: * -> * -> *) wRecordedstate wUnrecordedstate wTentativestate =
  Repo !String !RepoFormat !PristineType Cache deriving ( Int
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> ShowS
[Repository rt p wRecordedstate wUnrecordedstate wTentativestate]
-> ShowS
Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> String
(Int
 -> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
 -> ShowS)
-> (Repository rt p wRecordedstate wUnrecordedstate wTentativestate
    -> String)
-> ([Repository
       rt p wRecordedstate wUnrecordedstate wTentativestate]
    -> ShowS)
-> Show
     (Repository rt p wRecordedstate wUnrecordedstate wTentativestate)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
Int
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> ShowS
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
[Repository rt p wRecordedstate wUnrecordedstate wTentativestate]
-> ShowS
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> String
showList :: [Repository rt p wRecordedstate wUnrecordedstate wTentativestate]
-> ShowS
$cshowList :: forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
[Repository rt p wRecordedstate wUnrecordedstate wTentativestate]
-> ShowS
show :: Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> String
$cshow :: forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> String
showsPrec :: Int
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> ShowS
$cshowsPrec :: forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
Int
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> ShowS
Show )

repoLocation :: Repository rt p wR wU wT -> String
repoLocation :: Repository rt p wR wU wT -> String
repoLocation (Repo loc :: String
loc _ _ _) = String
loc

withRepoLocation :: Repository rt p wR wU wT -> IO a -> IO a
withRepoLocation :: Repository rt p wR wU wT -> IO a -> IO a
withRepoLocation repo :: Repository rt p wR wU wT
repo = String -> IO a -> IO a
forall p a. FilePathLike p => p -> IO a -> IO a
withCurrentDirectory (Repository rt p wR wU wT -> String
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
Repository rt p wRecordedstate wUnrecordedstate wTentativestate
-> String
repoLocation Repository rt p wR wU wT
repo)

repoFormat :: Repository rt p wR wU wT -> RepoFormat
repoFormat :: Repository rt p wR wU wT -> RepoFormat
repoFormat (Repo _ fmt :: RepoFormat
fmt _ _) = RepoFormat
fmt

repoPristineType :: Repository rt p wR wU wT -> PristineType
repoPristineType :: Repository rt p wR wU wT -> PristineType
repoPristineType (Repo _ _ pr :: PristineType
pr _) = PristineType
pr

repoCache :: Repository rt p wR wU wT -> Cache
repoCache :: Repository rt p wR wU wT -> Cache
repoCache (Repo _ _ _ c :: Cache
c) = Cache
c

-- | 'modifyCache' @repository function@ modifies the cache of
--   @repository@ with @function@, remove duplicates and sort the results with 'compareByLocality'.
modifyCache :: forall rt p wR wU wT . Repository rt p wR wU wT -> (Cache -> Cache) -> Repository rt p wR wU wT
modifyCache :: Repository rt p wR wU wT
-> (Cache -> Cache) -> Repository rt p wR wU wT
modifyCache (Repo dir :: String
dir rf :: RepoFormat
rf pristine :: PristineType
pristine cache :: Cache
cache) f :: Cache -> Cache
f
   = String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
String
-> RepoFormat
-> PristineType
-> Cache
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
Repo String
dir RepoFormat
rf PristineType
pristine (Cache -> Repository rt p wR wU wT)
-> Cache -> Repository rt p wR wU wT
forall a b. (a -> b) -> a -> b
$ ([CacheLoc] -> [CacheLoc]) -> Cache -> Cache
cmap ( (CacheLoc -> CacheLoc -> Ordering) -> [CacheLoc] -> [CacheLoc]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy CacheLoc -> CacheLoc -> Ordering
compareByLocality ([CacheLoc] -> [CacheLoc])
-> ([CacheLoc] -> [CacheLoc]) -> [CacheLoc] -> [CacheLoc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CacheLoc] -> [CacheLoc]
forall a. Eq a => [a] -> [a]
nub ) (Cache -> Cache) -> Cache -> Cache
forall a b. (a -> b) -> a -> b
$ Cache -> Cache
f Cache
cache
  where cmap :: ([CacheLoc] -> [CacheLoc]) -> Cache -> Cache
cmap g :: [CacheLoc] -> [CacheLoc]
g (Ca c :: [CacheLoc]
c) = [CacheLoc] -> Cache
Ca ([CacheLoc] -> [CacheLoc]
g [CacheLoc]
c)

repoPatchType :: Repository rt p wR wU wT -> PatchType rt p
repoPatchType :: Repository rt p wR wU wT -> PatchType rt p
repoPatchType _ = PatchType rt p
forall (rt :: RepoType) (p :: * -> * -> *). PatchType rt p
PatchType

coerceR :: Repository rt p wR wU wT -> Repository rt p wR' wU wT
coerceR :: Repository rt p wR wU wT -> Repository rt p wR' wU wT
coerceR = Repository rt p wR wU wT -> Repository rt p wR' wU wT
forall a b. Coercible a b => a -> b
coerce

coerceU :: Repository rt p wR wU wT -> Repository rt p wR wU' wT
coerceU :: Repository rt p wR wU wT -> Repository rt p wR wU' wT
coerceU = Repository rt p wR wU wT -> Repository rt p wR wU' wT
forall a b. Coercible a b => a -> b
coerce

coerceT :: Repository rt p wR wU wT -> Repository rt p wR wU wT'
coerceT :: Repository rt p wR wU wT -> Repository rt p wR wU wT'
coerceT = Repository rt p wR wU wT -> Repository rt p wR wU wT'
forall a b. Coercible a b => a -> b
coerce

mkRepo :: String -> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
mkRepo :: String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
mkRepo = String
-> RepoFormat -> PristineType -> Cache -> Repository rt p wR wU wT
forall (rt :: RepoType) (p :: * -> * -> *) wRecordedstate
       wUnrecordedstate wTentativestate.
String
-> RepoFormat
-> PristineType
-> Cache
-> Repository rt p wRecordedstate wUnrecordedstate wTentativestate
Repo