-- 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
                                      , repoFormat
                                      , repoLocation
                                      , withRepoLocation
                                      , repoPristineType
                                      , unsafeCoerceRepoType
                                      , unsafeCoercePatchType
                                      , unsafeCoerceR
                                      , unsafeCoerceU
                                      , unsafeCoerceT
                                      , mkRepo
                                      ) where

import Darcs.Prelude

import Darcs.Repository.Cache ( Cache )
import Darcs.Repository.Format ( RepoFormat )
import Darcs.Patch ( RepoType )
import Darcs.Util.File ( withCurrentDirectory )
import Unsafe.Coerce ( unsafeCoerce )

data PristineType
  = NoPristine
  | PlainPristine
  | HashedPristine
    deriving ( Int -> PristineType -> ShowS
[PristineType] -> ShowS
PristineType -> String
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
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 tree 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
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 )

type role Repository nominal nominal nominal nominal nominal

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

withRepoLocation :: Repository rt p wR wU wT -> IO a -> IO a
withRepoLocation :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT a.
Repository rt p wR wU wT -> IO a -> IO a
withRepoLocation Repository rt p wR wU wT
repo = forall p a. FilePathLike p => p -> IO a -> IO a
withCurrentDirectory (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 :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> RepoFormat
repoFormat (Repo String
_ RepoFormat
fmt PristineType
_ Cache
_) = RepoFormat
fmt

repoPristineType :: Repository rt p wR wU wT -> PristineType
repoPristineType :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> PristineType
repoPristineType (Repo String
_ RepoFormat
_ PristineType
pr Cache
_) = PristineType
pr

repoCache :: Repository rt p wR wU wT -> Cache
repoCache :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT.
Repository rt p wR wU wT -> Cache
repoCache (Repo String
_ RepoFormat
_ PristineType
_ Cache
c) = Cache
c

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

unsafeCoerceRepoType :: Repository rt p wR wU wT -> Repository rt' p wR wU wT
unsafeCoerceRepoType :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT
       (rt' :: RepoType).
Repository rt p wR wU wT -> Repository rt' p wR wU wT
unsafeCoerceRepoType = forall a b. a -> b
unsafeCoerce

unsafeCoercePatchType :: Repository rt p wR wU wT -> Repository rt p' wR wU wT
unsafeCoercePatchType :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT
       (p' :: * -> * -> *).
Repository rt p wR wU wT -> Repository rt p' wR wU wT
unsafeCoercePatchType = forall a b. a -> b
unsafeCoerce

unsafeCoerceR :: Repository rt p wR wU wT -> Repository rt p wR' wU wT
unsafeCoerceR :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT wR'.
Repository rt p wR wU wT -> Repository rt p wR' wU wT
unsafeCoerceR = forall a b. a -> b
unsafeCoerce

unsafeCoerceU :: Repository rt p wR wU wT -> Repository rt p wR wU' wT
unsafeCoerceU :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT wU'.
Repository rt p wR wU wT -> Repository rt p wR wU' wT
unsafeCoerceU = forall a b. a -> b
unsafeCoerce

unsafeCoerceT :: Repository rt p wR wU wT -> Repository rt p wR wU wT'
unsafeCoerceT :: forall (rt :: RepoType) (p :: * -> * -> *) wR wU wT wT'.
Repository rt p wR wU wT -> Repository rt p wR wU wT'
unsafeCoerceT = forall a b. a -> b
unsafeCoerce

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