-- 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
(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 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
[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 )

type role Repository nominal nominal nominal nominal nominal

repoLocation :: Repository rt p wR wU wT -> String
repoLocation :: Repository rt p wR wU wT -> String
repoLocation (Repo String
loc RepoFormat
_ PristineType
_ Cache
_) = 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 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 String
_ RepoFormat
fmt PristineType
_ Cache
_) = RepoFormat
fmt

repoPristineType :: Repository rt p wR wU wT -> PristineType
repoPristineType :: 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 :: 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 :: (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) = 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
l RepoFormat
f PristineType
p (Cache -> Cache
g Cache
c)

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

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

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

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

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

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