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.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-08-28 17:19:02 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Transitions where
|
|
|
|
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time
|
|
|
|
import System.Locale
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
|
|
|
|
transitionsLog :: FilePath
|
|
|
|
transitionsLog = "transitions.log"
|
|
|
|
|
|
|
|
data Transition
|
|
|
|
= ForgetGitHistory
|
|
|
|
| ForgetDeadRemotes
|
|
|
|
deriving (Show, Ord, Eq, Read)
|
|
|
|
|
|
|
|
data TransitionLine = TransitionLine
|
|
|
|
{ transitionStarted :: POSIXTime
|
|
|
|
, transition :: Transition
|
|
|
|
} deriving (Show, Ord, Eq)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-08-28 17:19:02 +00:00
|
|
|
addTransition :: POSIXTime -> Transition -> Transitions -> Transitions
|
|
|
|
addTransition ts t = S.insert $ TransitionLine ts t
|
|
|
|
|
|
|
|
showTransitions :: Transitions -> String
|
|
|
|
showTransitions = unlines . map showTransitionLine . S.elems
|
|
|
|
|
|
|
|
{- If the log contains new transitions we don't support, returns Nothing. -}
|
|
|
|
parseTransitions :: String -> Maybe Transitions
|
|
|
|
parseTransitions = check . map parseTransitionLine . lines
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
check l
|
2013-08-28 17:19:02 +00:00
|
|
|
| all isJust l = Just $ S.fromList $ catMaybes l
|
|
|
|
| otherwise = Nothing
|
|
|
|
|
2013-08-28 19:57:42 +00:00
|
|
|
parseTransitionsStrictly :: String -> String -> Transitions
|
|
|
|
parseTransitionsStrictly source = fromMaybe badsource . parseTransitions
|
|
|
|
where
|
|
|
|
badsource = error $ "unknown transitions listed in " ++ source ++ "; upgrade git-annex!"
|
|
|
|
|
2013-08-28 17:19:02 +00:00
|
|
|
showTransitionLine :: TransitionLine -> String
|
|
|
|
showTransitionLine (TransitionLine ts t) = unwords [show t, show ts]
|
|
|
|
|
|
|
|
parseTransitionLine :: String -> Maybe TransitionLine
|
|
|
|
parseTransitionLine s = TransitionLine <$> pdate ds <*> readish ts
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
ws = words s
|
|
|
|
ts = Prelude.head ws
|
2013-08-28 17:19:02 +00:00
|
|
|
ds = unwords $ Prelude.tail ws
|
2013-09-27 23:58:48 +00:00
|
|
|
pdate = utcTimeToPOSIXSeconds <$$> parseTime defaultTimeLocale "%s%Qs"
|
2013-08-28 17:19:02 +00:00
|
|
|
|
2013-08-28 19:57:42 +00:00
|
|
|
combineTransitions :: [Transitions] -> Transitions
|
|
|
|
combineTransitions = S.unions
|
|
|
|
|
|
|
|
transitionList :: Transitions -> [Transition]
|
|
|
|
transitionList = 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. -}
|
2013-08-28 20:38:03 +00:00
|
|
|
recordTransitions :: (FilePath -> (String -> String) -> Annex ()) -> Transitions -> Annex ()
|
2013-09-26 03:19:01 +00:00
|
|
|
recordTransitions changer t = changer transitionsLog $
|
|
|
|
showTransitions . S.union t . parseTransitionsStrictly "local"
|