2013-08-31 21:38:33 +00:00
|
|
|
{- git-annex branch transitions
|
|
|
|
-
|
2019-01-03 17:21:48 +00:00
|
|
|
- Copyright 2013-2019 Joey Hess <id@joeyh.name>
|
2013-08-31 21:38:33 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Branch.Transitions (
|
|
|
|
FileTransition(..),
|
|
|
|
getTransitionCalculator
|
|
|
|
) where
|
|
|
|
|
2019-01-03 17:21:48 +00:00
|
|
|
import Common
|
2013-08-31 21:38:33 +00:00
|
|
|
import Logs
|
|
|
|
import Logs.Transitions
|
2014-07-24 20:23:36 +00:00
|
|
|
import qualified Logs.UUIDBased as UUIDBased
|
|
|
|
import qualified Logs.Presence.Pure as Presence
|
|
|
|
import qualified Logs.Chunk.Pure as Chunk
|
2018-09-05 17:20:10 +00:00
|
|
|
import qualified Logs.MetaData.Pure as MetaData
|
2013-08-31 21:38:33 +00:00
|
|
|
import Types.TrustLevel
|
|
|
|
import Types.UUID
|
2018-09-05 17:20:10 +00:00
|
|
|
import Types.MetaData
|
2013-08-31 21:38:33 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2018-09-12 18:10:08 +00:00
|
|
|
import qualified Data.Set as S
|
2019-01-03 17:21:48 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2019-01-10 17:23:42 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
2019-01-03 17:21:48 +00:00
|
|
|
import Data.ByteString.Builder
|
2013-08-31 21:38:33 +00:00
|
|
|
|
|
|
|
data FileTransition
|
2019-01-09 18:10:05 +00:00
|
|
|
= ChangeFile Builder
|
2013-08-31 21:38:33 +00:00
|
|
|
| RemoveFile
|
|
|
|
| PreserveFile
|
|
|
|
|
2019-01-03 17:21:48 +00:00
|
|
|
type TransitionCalculator = FilePath -> L.ByteString -> TrustMap -> FileTransition
|
2013-08-31 21:38:33 +00:00
|
|
|
|
|
|
|
getTransitionCalculator :: Transition -> Maybe TransitionCalculator
|
|
|
|
getTransitionCalculator ForgetGitHistory = Nothing
|
|
|
|
getTransitionCalculator ForgetDeadRemotes = Just dropDead
|
|
|
|
|
2019-01-03 17:21:48 +00:00
|
|
|
dropDead :: FilePath -> L.ByteString -> TrustMap -> FileTransition
|
2013-08-31 21:38:33 +00:00
|
|
|
dropDead f content trustmap = case getLogVariety f of
|
2014-03-26 17:28:26 +00:00
|
|
|
Just UUIDBasedLog
|
|
|
|
-- Don't remove the dead repo from the trust log,
|
|
|
|
-- because git remotes may still exist, and they need
|
|
|
|
-- to still know it's dead.
|
|
|
|
| f == trustLog -> PreserveFile
|
2019-01-09 18:10:05 +00:00
|
|
|
| otherwise -> ChangeFile $
|
2019-01-09 18:00:35 +00:00
|
|
|
UUIDBased.buildLog (byteString . encodeBS) $
|
|
|
|
dropDeadFromMapLog trustmap id $ UUIDBased.parseLog Just (decodeBL content)
|
2019-01-09 18:10:05 +00:00
|
|
|
Just NewUUIDBasedLog -> ChangeFile $
|
2019-01-10 17:23:42 +00:00
|
|
|
UUIDBased.buildLogNew byteString $
|
2019-01-09 17:06:37 +00:00
|
|
|
dropDeadFromMapLog trustmap id $
|
2019-01-10 17:23:42 +00:00
|
|
|
UUIDBased.parseLogNew A.takeByteString content
|
2019-01-09 18:10:05 +00:00
|
|
|
Just (ChunkLog _) -> ChangeFile $
|
2019-01-10 17:23:42 +00:00
|
|
|
Chunk.buildLog $ dropDeadFromMapLog trustmap fst $ Chunk.parseLog content
|
2013-08-31 21:38:33 +00:00
|
|
|
Just (PresenceLog _) ->
|
|
|
|
let newlog = Presence.compactLog $ dropDeadFromPresenceLog trustmap $ Presence.parseLog content
|
|
|
|
in if null newlog
|
|
|
|
then RemoveFile
|
2019-01-09 18:10:05 +00:00
|
|
|
else ChangeFile $ Presence.buildLog newlog
|
2018-09-05 17:20:10 +00:00
|
|
|
Just RemoteMetaDataLog ->
|
2019-01-07 19:51:05 +00:00
|
|
|
let newlog = dropDeadFromRemoteMetaDataLog trustmap $ MetaData.simplifyLog $ MetaData.parseLog content
|
2018-09-12 18:10:08 +00:00
|
|
|
in if S.null newlog
|
2018-09-05 17:20:10 +00:00
|
|
|
then RemoveFile
|
2019-01-09 18:10:05 +00:00
|
|
|
else ChangeFile $ MetaData.buildLog newlog
|
2014-02-13 01:12:22 +00:00
|
|
|
Just OtherLog -> PreserveFile
|
2013-08-31 21:38:33 +00:00
|
|
|
Nothing -> PreserveFile
|
|
|
|
|
2016-01-27 13:44:38 +00:00
|
|
|
dropDeadFromMapLog :: TrustMap -> (k -> UUID) -> M.Map k v -> M.Map k v
|
2018-09-05 17:20:10 +00:00
|
|
|
dropDeadFromMapLog trustmap getuuid =
|
|
|
|
M.filterWithKey $ \k _v -> notDead trustmap getuuid k
|
2013-08-31 21:38:33 +00:00
|
|
|
|
|
|
|
{- Presence logs can contain UUIDs or other values. Any line that matches
|
|
|
|
- a dead uuid is dropped; any other values are passed through. -}
|
|
|
|
dropDeadFromPresenceLog :: TrustMap -> [Presence.LogLine] -> [Presence.LogLine]
|
2018-09-05 17:20:10 +00:00
|
|
|
dropDeadFromPresenceLog trustmap =
|
2019-01-03 17:21:48 +00:00
|
|
|
filter $ notDead trustmap (toUUID . Presence.fromLogInfo . Presence.info)
|
2018-09-05 17:20:10 +00:00
|
|
|
|
|
|
|
dropDeadFromRemoteMetaDataLog :: TrustMap -> MetaData.Log MetaData -> MetaData.Log MetaData
|
|
|
|
dropDeadFromRemoteMetaDataLog trustmap =
|
|
|
|
MetaData.filterOutEmpty . MetaData.filterRemoteMetaData (notDead trustmap id)
|
2013-08-31 21:38:33 +00:00
|
|
|
|
|
|
|
notDead :: TrustMap -> (v -> UUID) -> v -> Bool
|
2014-10-14 18:10:22 +00:00
|
|
|
notDead trustmap a v = M.findWithDefault def (a v) trustmap /= DeadTrusted
|