2017-08-31 19:41:48 +00:00
|
|
|
{- git-annex export log
|
|
|
|
-
|
|
|
|
- Copyright 2017 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Export where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Git
|
2017-09-06 17:39:33 +00:00
|
|
|
import Git.Sha
|
2017-08-31 22:06:49 +00:00
|
|
|
import Git.FilePath
|
2017-08-31 19:41:48 +00:00
|
|
|
import Logs
|
2017-09-12 21:45:52 +00:00
|
|
|
import Logs.MapLog
|
2017-08-31 19:41:48 +00:00
|
|
|
import Annex.UUID
|
|
|
|
|
2017-09-06 17:39:33 +00:00
|
|
|
data Exported = Exported
|
|
|
|
{ exportedTreeish :: Git.Ref
|
|
|
|
, incompleteExportedTreeish :: [Git.Ref]
|
|
|
|
}
|
2017-09-12 20:59:04 +00:00
|
|
|
deriving (Eq, Show)
|
2017-09-06 17:39:33 +00:00
|
|
|
|
2017-09-12 21:45:52 +00:00
|
|
|
data ExportParticipants = ExportParticipants
|
|
|
|
{ exportFrom :: UUID
|
|
|
|
, exportTo :: UUID
|
|
|
|
}
|
|
|
|
deriving (Eq, Ord)
|
|
|
|
|
|
|
|
data ExportChange = ExportChange
|
|
|
|
{ oldTreeish :: [Git.Ref]
|
|
|
|
, newTreeish :: Git.Ref
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:39:33 +00:00
|
|
|
-- | Get what's been exported to a special remote.
|
2017-08-31 19:41:48 +00:00
|
|
|
--
|
|
|
|
-- If the list contains multiple items, there was an export conflict,
|
|
|
|
-- and different trees were exported to the same special remote.
|
2017-09-06 17:39:33 +00:00
|
|
|
getExport :: UUID -> Annex [Exported]
|
2017-09-12 21:45:52 +00:00
|
|
|
getExport remoteuuid = nub . mapMaybe get . M.toList . simpleMap
|
|
|
|
. parseExportLog
|
2017-08-31 19:41:48 +00:00
|
|
|
<$> Annex.Branch.get exportLog
|
|
|
|
where
|
2017-09-12 21:45:52 +00:00
|
|
|
get (ep, exported)
|
|
|
|
| exportTo ep == remoteuuid = Just exported
|
2017-08-31 19:41:48 +00:00
|
|
|
| otherwise = Nothing
|
|
|
|
|
|
|
|
-- | Record a change in what's exported to a special remote.
|
|
|
|
--
|
2017-09-06 17:39:33 +00:00
|
|
|
-- This is called before an export begins uploading new files to the
|
|
|
|
-- remote, but after it's cleaned up any files that need to be deleted
|
|
|
|
-- from the old treeish.
|
|
|
|
--
|
2017-08-31 19:41:48 +00:00
|
|
|
-- Any entries in the log for the oldTreeish will be updated to the
|
|
|
|
-- newTreeish. This way, when multiple repositories are exporting to
|
|
|
|
-- the same special remote, there's no conflict as long as they move
|
|
|
|
-- forward in lock-step.
|
2017-08-31 22:06:49 +00:00
|
|
|
--
|
|
|
|
-- Also, the newTreeish is grafted into the git-annex branch. This is done
|
|
|
|
-- to ensure that it's available later.
|
2017-08-31 19:41:48 +00:00
|
|
|
recordExport :: UUID -> ExportChange -> Annex ()
|
|
|
|
recordExport remoteuuid ec = do
|
|
|
|
c <- liftIO currentVectorClock
|
|
|
|
u <- getUUID
|
2017-09-12 21:45:52 +00:00
|
|
|
let ep = ExportParticipants { exportFrom = u, exportTo = remoteuuid }
|
|
|
|
let exported = Exported (newTreeish ec) []
|
2017-08-31 19:41:48 +00:00
|
|
|
Annex.Branch.change exportLog $
|
2017-09-12 21:45:52 +00:00
|
|
|
showExportLog
|
|
|
|
. changeMapLog c ep exported
|
2017-08-31 19:41:48 +00:00
|
|
|
. M.mapWithKey (updateothers c u)
|
2017-09-12 21:45:52 +00:00
|
|
|
. parseExportLog
|
2017-08-31 19:41:48 +00:00
|
|
|
where
|
2017-09-12 21:45:52 +00:00
|
|
|
updateothers c u ep le@(LogEntry _ exported@(Exported { exportedTreeish = t }))
|
|
|
|
| u == exportFrom ep || remoteuuid /= exportTo ep || t `notElem` oldTreeish ec = le
|
|
|
|
| otherwise = LogEntry c (exported { exportedTreeish = newTreeish ec })
|
2017-09-06 17:39:33 +00:00
|
|
|
|
|
|
|
-- | Record the beginning of an export, to allow cleaning up from
|
|
|
|
-- interrupted exports.
|
|
|
|
--
|
|
|
|
-- This is called before any changes are made to the remote.
|
|
|
|
recordExportBeginning :: UUID -> Git.Ref -> Annex ()
|
|
|
|
recordExportBeginning remoteuuid newtree = do
|
|
|
|
c <- liftIO currentVectorClock
|
|
|
|
u <- getUUID
|
2017-09-12 21:45:52 +00:00
|
|
|
let ep = ExportParticipants { exportFrom = u, exportTo = remoteuuid }
|
|
|
|
old <- fromMaybe (Exported emptyTree [])
|
|
|
|
. M.lookup ep . simpleMap
|
|
|
|
. parseExportLog
|
2017-09-06 17:39:33 +00:00
|
|
|
<$> Annex.Branch.get exportLog
|
2017-09-12 18:19:26 +00:00
|
|
|
let new = old { incompleteExportedTreeish = nub (newtree:incompleteExportedTreeish old) }
|
2017-09-06 17:39:33 +00:00
|
|
|
Annex.Branch.change exportLog $
|
2017-09-12 21:45:52 +00:00
|
|
|
showExportLog
|
|
|
|
. changeMapLog c ep new
|
|
|
|
. parseExportLog
|
2017-09-12 22:30:36 +00:00
|
|
|
Annex.Branch.graftTreeish newtree (asTopFilePath "export.tree")
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2017-09-12 21:45:52 +00:00
|
|
|
parseExportLog :: String -> MapLog ExportParticipants Exported
|
|
|
|
parseExportLog = parseMapLog parseExportParticipants parseExported
|
|
|
|
|
|
|
|
showExportLog :: MapLog ExportParticipants Exported -> String
|
|
|
|
showExportLog = showMapLog formatExportParticipants formatExported
|
|
|
|
|
|
|
|
formatExportParticipants :: ExportParticipants -> String
|
|
|
|
formatExportParticipants ep =
|
|
|
|
fromUUID (exportFrom ep) ++ ':' : fromUUID (exportTo ep)
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2017-09-12 21:45:52 +00:00
|
|
|
parseExportParticipants :: String -> Maybe ExportParticipants
|
|
|
|
parseExportParticipants s = case separate (== ':') s of
|
|
|
|
("",_) -> Nothing
|
|
|
|
(_,"") -> Nothing
|
|
|
|
(f,t) -> Just $ ExportParticipants
|
|
|
|
{ exportFrom = toUUID f
|
|
|
|
, exportTo = toUUID t
|
|
|
|
}
|
|
|
|
formatExported :: Exported -> String
|
|
|
|
formatExported exported = unwords $ map Git.fromRef $
|
|
|
|
exportedTreeish exported : incompleteExportedTreeish exported
|
2017-08-31 19:41:48 +00:00
|
|
|
|
2017-09-12 21:45:52 +00:00
|
|
|
parseExported :: String -> Maybe Exported
|
|
|
|
parseExported s = case words s of
|
|
|
|
(et:it) -> Just $ Exported (Git.Ref et) (map Git.Ref it)
|
2017-08-31 19:41:48 +00:00
|
|
|
_ -> Nothing
|