2013-08-28 17:19:02 +00:00
|
|
|
{- git-annex transitions log
|
|
|
|
-
|
|
|
|
- This is used to record transitions that have been performed on the
|
|
|
|
- git-annex branch, and when the transition was first started.
|
|
|
|
-
|
|
|
|
- We can quickly detect when the local branch has already had an transition
|
|
|
|
- done that is listed in the remote branch by checking that the local
|
|
|
|
- branch contains the same transition, with the same or newer start time.
|
|
|
|
-
|
2019-01-10 21:13:30 +00:00
|
|
|
- Copyright 2013-2019 Joey Hess <id@joeyh.name>
|
2013-08-28 17:19:02 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-08-28 17:19:02 +00:00
|
|
|
-}
|
|
|
|
|
2019-11-26 19:27:22 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2013-08-28 17:19:02 +00:00
|
|
|
module Logs.Transitions where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2017-08-14 17:55:38 +00:00
|
|
|
import Annex.VectorClock
|
2016-05-27 15:45:13 +00:00
|
|
|
import Logs.Line
|
2013-08-28 17:19:02 +00:00
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
import qualified Data.Set as S
|
2019-01-10 21:13:30 +00:00
|
|
|
import Data.Either
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.ByteString.Builder
|
2019-01-03 17:21:48 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2019-01-10 21:13:30 +00:00
|
|
|
import qualified Data.Attoparsec.ByteString.Lazy as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2017-08-14 17:55:38 +00:00
|
|
|
|
2019-11-26 19:27:22 +00:00
|
|
|
transitionsLog :: RawFilePath
|
2013-08-28 17:19:02 +00:00
|
|
|
transitionsLog = "transitions.log"
|
|
|
|
|
|
|
|
data Transition
|
|
|
|
= ForgetGitHistory
|
|
|
|
| ForgetDeadRemotes
|
|
|
|
deriving (Show, Ord, Eq, Read)
|
|
|
|
|
|
|
|
data TransitionLine = TransitionLine
|
2017-08-14 17:55:38 +00:00
|
|
|
{ transitionStarted :: VectorClock
|
2019-01-10 21:13:30 +00:00
|
|
|
-- New transitions that we don't know about yet are preserved.
|
|
|
|
, transition :: Either ByteString Transition
|
2017-08-14 18:43:56 +00:00
|
|
|
} deriving (Ord, Eq)
|
2013-08-28 17:19:02 +00:00
|
|
|
|
|
|
|
type Transitions = S.Set TransitionLine
|
|
|
|
|
2013-08-28 19:57:42 +00:00
|
|
|
describeTransition :: Transition -> String
|
|
|
|
describeTransition ForgetGitHistory = "forget git history"
|
|
|
|
describeTransition ForgetDeadRemotes = "forget dead remotes"
|
|
|
|
|
2013-08-28 20:38:03 +00:00
|
|
|
noTransitions :: Transitions
|
|
|
|
noTransitions = S.empty
|
|
|
|
|
2017-08-14 17:55:38 +00:00
|
|
|
addTransition :: VectorClock -> Transition -> Transitions -> Transitions
|
2019-01-10 21:13:30 +00:00
|
|
|
addTransition c t = S.insert $ TransitionLine c (Right t)
|
2013-08-28 17:19:02 +00:00
|
|
|
|
2019-01-10 21:13:30 +00:00
|
|
|
buildTransitions :: Transitions -> Builder
|
|
|
|
buildTransitions = mconcat . map genline . S.elems
|
2013-08-28 19:57:42 +00:00
|
|
|
where
|
2019-01-10 21:13:30 +00:00
|
|
|
genline tl = buildt (transition tl) <> charUtf8 ' '
|
|
|
|
<> buildVectorClock (transitionStarted tl) <> charUtf8 '\n'
|
|
|
|
buildt (Left b) = byteString b
|
|
|
|
buildt (Right t) = byteString (encodeBS (show t))
|
|
|
|
|
|
|
|
parseTransitions :: L.ByteString -> Transitions
|
|
|
|
parseTransitions = fromMaybe S.empty . A.maybeResult . A.parse
|
|
|
|
(S.fromList <$> parseLogLines transitionLineParser)
|
|
|
|
|
|
|
|
parseTransitionsStrictly :: String -> L.ByteString -> Transitions
|
|
|
|
parseTransitionsStrictly source b =
|
|
|
|
let ts = parseTransitions b
|
|
|
|
in if S.null $ S.filter (isLeft . transition) ts
|
|
|
|
then ts
|
|
|
|
else giveup $ "unknown transitions listed in " ++ source ++ "; upgrade git-annex!"
|
2013-08-28 19:57:42 +00:00
|
|
|
|
2013-08-28 17:19:02 +00:00
|
|
|
showTransitionLine :: TransitionLine -> String
|
2017-08-14 18:43:56 +00:00
|
|
|
showTransitionLine (TransitionLine c t) = unwords [show t, formatVectorClock c]
|
2013-08-28 17:19:02 +00:00
|
|
|
|
2019-01-10 21:13:30 +00:00
|
|
|
transitionLineParser :: A.Parser TransitionLine
|
|
|
|
transitionLineParser = do
|
|
|
|
t <- (parsetransition <$> A.takeByteString)
|
|
|
|
_ <- A8.char ' '
|
|
|
|
c <- vectorClockParser
|
|
|
|
return $ TransitionLine c t
|
2013-08-28 17:19:02 +00:00
|
|
|
where
|
2019-01-10 21:13:30 +00:00
|
|
|
parsetransition b = case readish (decodeBS b) of
|
|
|
|
Just t -> Right t
|
|
|
|
Nothing -> Left b
|
2013-08-28 17:19:02 +00:00
|
|
|
|
2013-08-28 19:57:42 +00:00
|
|
|
combineTransitions :: [Transitions] -> Transitions
|
|
|
|
combineTransitions = S.unions
|
|
|
|
|
2019-01-10 21:13:30 +00:00
|
|
|
{- Unknown types of transitions are omitted. -}
|
|
|
|
knownTransitionList :: Transitions -> [Transition]
|
|
|
|
knownTransitionList = nub . rights . map transition . S.elems
|
2013-08-28 17:19:02 +00:00
|
|
|
|
|
|
|
{- Typically ran with Annex.Branch.change, but we can't import Annex.Branch
|
|
|
|
- here since it depends on this module. -}
|
2019-11-26 19:27:22 +00:00
|
|
|
recordTransitions :: (RawFilePath -> (L.ByteString -> Builder) -> Annex ()) -> Transitions -> Annex ()
|
2013-09-26 03:19:01 +00:00
|
|
|
recordTransitions changer t = changer transitionsLog $
|
2019-01-10 21:13:30 +00:00
|
|
|
buildTransitions . S.union t . parseTransitionsStrictly "local"
|