2019-02-21 21:32:59 +00:00
|
|
|
{- git-annex import from remotes
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2019-02-27 17:15:02 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-02-26 19:25:28 +00:00
|
|
|
|
2019-02-23 19:47:55 +00:00
|
|
|
module Annex.Import (
|
2019-02-26 17:11:25 +00:00
|
|
|
RemoteTrackingBranch(..),
|
|
|
|
mkRemoteTrackingBranch,
|
2019-02-23 19:47:55 +00:00
|
|
|
ImportTreeConfig(..),
|
|
|
|
ImportCommitConfig(..),
|
|
|
|
buildImportCommit,
|
2019-02-26 19:25:28 +00:00
|
|
|
buildImportTrees,
|
|
|
|
downloadImport
|
2019-02-23 19:47:55 +00:00
|
|
|
) where
|
2019-02-21 21:32:59 +00:00
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Types.Import
|
2019-02-26 17:11:25 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2019-02-21 21:32:59 +00:00
|
|
|
import Git.Types
|
|
|
|
import Git.Tree
|
2019-02-22 16:41:17 +00:00
|
|
|
import Git.Sha
|
2019-02-21 21:32:59 +00:00
|
|
|
import Git.FilePath
|
2019-02-22 16:41:17 +00:00
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.Branch
|
|
|
|
import qualified Annex
|
2019-02-21 21:32:59 +00:00
|
|
|
import Annex.Link
|
2019-02-22 16:41:17 +00:00
|
|
|
import Annex.LockFile
|
2019-02-27 17:15:02 +00:00
|
|
|
import Annex.Content
|
|
|
|
import Backend
|
|
|
|
import Types.Key
|
|
|
|
import Types.KeySource
|
2019-02-26 19:25:28 +00:00
|
|
|
import Utility.Metered
|
2019-02-27 17:15:02 +00:00
|
|
|
import Utility.DataUnits
|
2019-02-22 16:41:17 +00:00
|
|
|
import Logs.Export
|
2019-02-26 19:25:28 +00:00
|
|
|
import Logs.ContentIdentifier
|
|
|
|
import qualified Database.Export as Export
|
|
|
|
import qualified Database.ContentIdentifier as CID
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import qualified Data.Map.Strict as M
|
2019-02-21 21:32:59 +00:00
|
|
|
|
2019-02-26 17:11:25 +00:00
|
|
|
newtype RemoteTrackingBranch = RemoteTrackingBranch
|
|
|
|
{ fromRemoteTrackingBranch :: Ref }
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
{- Makes a remote tracking branch corresponding to a local branch.
|
|
|
|
- Note that the local branch does not have to exist yet. -}
|
|
|
|
mkRemoteTrackingBranch :: Remote -> Ref -> RemoteTrackingBranch
|
|
|
|
mkRemoteTrackingBranch remote ref = RemoteTrackingBranch $
|
|
|
|
Git.Ref.underBase ("refs/remotes/" ++ Remote.name remote) ref
|
|
|
|
|
2019-02-23 19:47:55 +00:00
|
|
|
{- Configures how to build an import tree. -}
|
|
|
|
data ImportTreeConfig
|
|
|
|
= ImportTree
|
|
|
|
-- ^ Import the tree as-is from the remote.
|
|
|
|
| ImportSubTree TopFilePath Sha
|
|
|
|
-- ^ Import a tree from the remote and graft it into a subdirectory
|
|
|
|
-- of the existing tree whose Sha is provided, replacing anything
|
|
|
|
-- that was there before.
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
{- Configures how to build an import commit. -}
|
|
|
|
data ImportCommitConfig = ImportCommitConfig
|
2019-02-26 17:11:25 +00:00
|
|
|
{ importCommitParent :: Maybe Sha
|
|
|
|
-- ^ Commit to use as a parent of the import commit.
|
2019-02-23 19:47:55 +00:00
|
|
|
, importCommitMode :: Git.Branch.CommitMode
|
|
|
|
, importCommitMessage :: String
|
|
|
|
}
|
|
|
|
|
|
|
|
{- Builds a commit for an import from a special remote.
|
2019-02-21 21:32:59 +00:00
|
|
|
-
|
|
|
|
- When a remote provided a history of versions of files,
|
|
|
|
- builds a corresponding tree of git commits.
|
|
|
|
-
|
2019-02-23 19:47:55 +00:00
|
|
|
- When there are no changes to commit (ie, the imported tree is the same
|
|
|
|
- as the tree in the importCommitParent), returns Nothing.
|
|
|
|
-
|
2019-02-21 21:32:59 +00:00
|
|
|
- After importing from a remote, exporting the same thing back to the
|
2019-02-22 16:41:17 +00:00
|
|
|
- remote should be a no-op. So, the export log and database are
|
|
|
|
- updated to reflect the imported tree.
|
2019-02-21 21:32:59 +00:00
|
|
|
-
|
2019-02-23 19:47:55 +00:00
|
|
|
- This does not download any content from a remote. But since it needs the
|
2019-02-22 16:41:17 +00:00
|
|
|
- Key of imported files to be known, its caller will have to first download
|
2019-02-21 21:32:59 +00:00
|
|
|
- new files in order to generate keys for them.
|
|
|
|
-}
|
|
|
|
buildImportCommit
|
2019-02-22 16:41:17 +00:00
|
|
|
:: Remote
|
2019-02-23 19:47:55 +00:00
|
|
|
-> ImportTreeConfig
|
|
|
|
-> ImportCommitConfig
|
2019-02-21 21:32:59 +00:00
|
|
|
-> ImportableContents Key
|
2019-02-26 17:11:25 +00:00
|
|
|
-> Annex (Maybe Ref)
|
2019-02-23 19:47:55 +00:00
|
|
|
buildImportCommit remote importtreeconfig importcommitconfig importable =
|
2019-02-26 17:11:25 +00:00
|
|
|
case importCommitParent importcommitconfig of
|
2019-02-23 19:47:55 +00:00
|
|
|
Nothing -> go emptyTree Nothing
|
2019-02-26 17:11:25 +00:00
|
|
|
Just basecommit -> inRepo (Git.Ref.tree basecommit) >>= \case
|
|
|
|
Nothing -> go emptyTree Nothing
|
|
|
|
Just origtree -> go origtree (Just basecommit)
|
2019-02-21 21:32:59 +00:00
|
|
|
where
|
2019-02-23 19:47:55 +00:00
|
|
|
basetree = case importtreeconfig of
|
|
|
|
ImportTree -> emptyTree
|
|
|
|
ImportSubTree _ sha -> sha
|
|
|
|
subdir = case importtreeconfig of
|
|
|
|
ImportTree -> Nothing
|
|
|
|
ImportSubTree dir _ -> Just dir
|
|
|
|
|
|
|
|
go origtree basecommit = do
|
|
|
|
imported@(History finaltree _) <-
|
|
|
|
buildImportTrees basetree subdir importable
|
|
|
|
mkcommits origtree basecommit imported >>= \case
|
2019-02-26 17:11:25 +00:00
|
|
|
Nothing -> return Nothing
|
2019-02-23 19:47:55 +00:00
|
|
|
Just finalcommit -> do
|
|
|
|
updateexportdb finaltree
|
|
|
|
updateexportlog finaltree
|
2019-02-26 17:11:25 +00:00
|
|
|
return (Just finalcommit)
|
2019-02-23 19:47:55 +00:00
|
|
|
|
|
|
|
mkcommits origtree basecommit (History importedtree hs) = do
|
|
|
|
parents <- catMaybes <$> mapM (mkcommits origtree basecommit) hs
|
|
|
|
if importedtree == origtree && null parents
|
|
|
|
then return Nothing -- no changes to commit
|
2019-02-22 16:41:17 +00:00
|
|
|
else do
|
2019-02-22 16:44:22 +00:00
|
|
|
let commitparents = if null parents
|
2019-02-23 19:47:55 +00:00
|
|
|
then catMaybes [basecommit]
|
2019-02-22 16:44:22 +00:00
|
|
|
else parents
|
2019-02-23 19:47:55 +00:00
|
|
|
commit <- inRepo $ Git.Branch.commitTree
|
|
|
|
(importCommitMode importcommitconfig)
|
|
|
|
(importCommitMessage importcommitconfig)
|
|
|
|
commitparents
|
|
|
|
importedtree
|
2019-02-22 16:41:17 +00:00
|
|
|
return (Just commit)
|
2019-02-23 19:47:55 +00:00
|
|
|
|
2019-02-22 16:41:17 +00:00
|
|
|
updateexportdb importedtree =
|
2019-02-26 17:11:25 +00:00
|
|
|
withExclusiveLock (gitAnnexExportLock (Remote.uuid remote)) $ do
|
2019-02-26 19:25:28 +00:00
|
|
|
db <- Export.openDb (Remote.uuid remote)
|
2019-02-22 16:41:17 +00:00
|
|
|
prevtree <- liftIO $ fromMaybe emptyTree
|
2019-02-26 19:25:28 +00:00
|
|
|
<$> Export.getExportTreeCurrent db
|
2019-02-22 16:41:17 +00:00
|
|
|
when (importedtree /= prevtree) $ do
|
2019-02-26 19:25:28 +00:00
|
|
|
Export.updateExportTree db prevtree importedtree
|
|
|
|
liftIO $ Export.recordExportTreeCurrent db importedtree
|
2019-02-22 16:41:17 +00:00
|
|
|
-- TODO: addExportedLocation etc
|
2019-02-26 19:25:28 +00:00
|
|
|
Export.closeDb db
|
2019-02-23 19:47:55 +00:00
|
|
|
|
2019-02-22 16:41:17 +00:00
|
|
|
updateexportlog importedtree = do
|
2019-02-26 17:11:25 +00:00
|
|
|
old <- getExport (Remote.uuid remote)
|
|
|
|
recordExport (Remote.uuid remote) $ ExportChange
|
2019-02-22 16:41:17 +00:00
|
|
|
{ oldTreeish = exportedTreeishes old
|
|
|
|
, newTreeish = importedtree
|
|
|
|
}
|
2019-02-21 21:32:59 +00:00
|
|
|
|
|
|
|
data History t = History t [History t]
|
2019-02-22 16:41:17 +00:00
|
|
|
deriving (Show)
|
2019-02-21 21:32:59 +00:00
|
|
|
|
2019-02-22 16:41:17 +00:00
|
|
|
{- Builds a history of git trees reflecting the ImportableContents.
|
|
|
|
-
|
|
|
|
- When a subdir is provided, imported tree is grafted into the basetree at
|
|
|
|
- that location, replacing any object that was there.
|
|
|
|
-}
|
2019-02-21 21:32:59 +00:00
|
|
|
buildImportTrees
|
2019-02-22 16:41:17 +00:00
|
|
|
:: Ref
|
|
|
|
-> Maybe TopFilePath
|
2019-02-21 21:32:59 +00:00
|
|
|
-> ImportableContents Key
|
|
|
|
-> Annex (History Sha)
|
2019-02-22 16:41:17 +00:00
|
|
|
buildImportTrees basetree msubdir importable = History
|
|
|
|
<$> (go (importableContents importable) =<< Annex.gitRepo)
|
|
|
|
<*> mapM (buildImportTrees basetree msubdir) (importableHistory importable)
|
2019-02-21 21:32:59 +00:00
|
|
|
where
|
2019-02-22 16:41:17 +00:00
|
|
|
go ls repo = withMkTreeHandle repo $ \hdl -> do
|
|
|
|
importtree <- liftIO . recordTree' hdl
|
|
|
|
. treeItemsToTree
|
|
|
|
=<< mapM mktreeitem ls
|
|
|
|
case msubdir of
|
|
|
|
Nothing -> return importtree
|
|
|
|
Just subdir -> liftIO $
|
|
|
|
graftTree' importtree subdir basetree repo hdl
|
2019-02-21 21:32:59 +00:00
|
|
|
mktreeitem (loc, k) = do
|
|
|
|
let lf = fromImportLocation loc
|
2019-02-22 16:41:17 +00:00
|
|
|
let treepath = asTopFilePath lf
|
|
|
|
let topf = asTopFilePath $
|
|
|
|
maybe lf (\sd -> getTopFilePath sd </> lf) msubdir
|
2019-02-21 21:32:59 +00:00
|
|
|
relf <- fromRepo $ fromTopFilePath topf
|
|
|
|
symlink <- calcRepo $ gitAnnexLink relf k
|
|
|
|
linksha <- hashSymlink symlink
|
2019-02-22 16:41:17 +00:00
|
|
|
return $ TreeItem treepath (fromTreeItemType TreeSymlink) linksha
|
2019-02-26 19:25:28 +00:00
|
|
|
|
|
|
|
{- Downloads all new ContentIdentifiers. Supports concurrency when enabled.
|
|
|
|
-
|
|
|
|
- If any download fails, the whole thing fails, but it will resume where
|
|
|
|
- it left off.
|
|
|
|
-}
|
2019-02-27 17:15:02 +00:00
|
|
|
downloadImport :: Remote -> ImportTreeConfig -> ImportableContents (ContentIdentifier, ByteSize) -> Annex (Maybe (ImportableContents Key))
|
|
|
|
downloadImport remote importtreeconfig importablecontents = do
|
2019-02-26 19:25:28 +00:00
|
|
|
-- This map is used to remember content identifiers that
|
|
|
|
-- were just downloaded, before they have necessarily been
|
|
|
|
-- stored in the database. This way, if the same content
|
|
|
|
-- identifier appears multiple times in the
|
|
|
|
-- importablecontents (eg when it has a history),
|
|
|
|
-- they will only be downloaded once.
|
|
|
|
cidmap <- liftIO $ newTVarIO M.empty
|
|
|
|
bracket CID.openDb CID.closeDb (go cidmap importablecontents)
|
|
|
|
-- TODO really support concurrency; avoid donwloading the same
|
|
|
|
-- ContentIdentifier twice.
|
|
|
|
where
|
|
|
|
go cidmap (ImportableContents l h) db = do
|
|
|
|
l' <- mapM (download cidmap db) l
|
|
|
|
if any isNothing l'
|
|
|
|
then return Nothing
|
|
|
|
else do
|
|
|
|
h' <- mapM (\ic -> go cidmap ic db) h
|
|
|
|
if any isNothing h'
|
|
|
|
then return Nothing
|
|
|
|
else return $ Just $
|
|
|
|
ImportableContents
|
|
|
|
(catMaybes l')
|
|
|
|
(catMaybes h')
|
|
|
|
|
2019-02-27 17:15:02 +00:00
|
|
|
download cidmap db (loc, (cid, sz)) = getcidkey cidmap db cid >>= \case
|
2019-02-26 19:25:28 +00:00
|
|
|
(k:_) -> return $ Just (loc, k)
|
2019-02-27 17:15:02 +00:00
|
|
|
[] -> checkDiskSpaceToGet tmpkey Nothing $
|
|
|
|
withTmp tmpkey $ \tmpfile ->
|
|
|
|
Remote.retrieveExportWithContentIdentifier ia loc cid tmpfile (ingestkey loc tmpfile) p >>= \case
|
|
|
|
Just k -> do
|
|
|
|
recordcidkey cidmap db cid k
|
|
|
|
return $ Just (loc, k)
|
|
|
|
Nothing -> return Nothing
|
|
|
|
where
|
|
|
|
-- TODO progress bar
|
|
|
|
p = nullMeterUpdate
|
|
|
|
ia = Remote.importActions remote
|
|
|
|
tmpkey = importKey cid sz
|
2019-02-26 19:25:28 +00:00
|
|
|
|
2019-02-27 17:15:02 +00:00
|
|
|
ingestkey loc tmpfile = do
|
|
|
|
f <- fromRepo $ fromTopFilePath $ locworktreefilename loc
|
|
|
|
backend <- chooseBackend f
|
|
|
|
let ks = KeySource
|
|
|
|
{ keyFilename = f
|
|
|
|
, contentLocation = tmpfile
|
|
|
|
, inodeCache = Nothing
|
|
|
|
}
|
|
|
|
genKey ks backend >>= \case
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just (k, _) ->
|
|
|
|
tryNonAsync (moveAnnex k tmpfile) >>= \case
|
|
|
|
Right True -> return (Just k)
|
|
|
|
_ -> return Nothing
|
|
|
|
|
|
|
|
locworktreefilename loc = asTopFilePath $ case importtreeconfig of
|
|
|
|
ImportTree -> fromImportLocation loc
|
|
|
|
ImportSubTree subdir _ ->
|
|
|
|
getTopFilePath subdir </> fromImportLocation loc
|
2019-02-26 19:25:28 +00:00
|
|
|
|
|
|
|
getcidkey cidmap db cid = liftIO $
|
|
|
|
CID.getContentIdentifierKeys db (Remote.uuid remote) cid >>= \case
|
2019-02-27 17:15:02 +00:00
|
|
|
[] -> atomically $
|
|
|
|
maybeToList . M.lookup cid <$> readTVar cidmap
|
2019-02-26 19:25:28 +00:00
|
|
|
l -> return l
|
|
|
|
|
|
|
|
recordcidkey cidmap db cid k = do
|
|
|
|
liftIO $ atomically $ modifyTVar' cidmap $
|
|
|
|
M.insert cid k
|
|
|
|
liftIO $ CID.recordContentIdentifier db (Remote.uuid remote) cid k
|
|
|
|
recordContentIdentifier (Remote.uuid remote) cid k
|
2019-02-27 17:15:02 +00:00
|
|
|
|
|
|
|
{- Temporary key used for import of a ContentIdentifier while downloading
|
|
|
|
- content, before generating its real key. -}
|
|
|
|
importKey :: ContentIdentifier -> Integer -> Key
|
|
|
|
importKey (ContentIdentifier cid) size = stubKey
|
|
|
|
{ keyName = cid
|
|
|
|
, keyVariety = OtherKey "CID"
|
|
|
|
, keySize = Just size
|
|
|
|
}
|