2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Move where
|
|
|
|
|
2011-05-16 17:27:19 +00:00
|
|
|
import Control.Monad (when)
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
import Command
|
2010-11-11 22:54:52 +00:00
|
|
|
import qualified Command.Drop
|
2010-11-02 23:04:24 +00:00
|
|
|
import qualified Annex
|
|
|
|
import LocationLog
|
|
|
|
import Types
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2011-03-27 21:24:20 +00:00
|
|
|
import qualified Remote
|
2010-11-02 23:04:24 +00:00
|
|
|
import UUID
|
2010-11-08 19:15:21 +00:00
|
|
|
import Messages
|
2011-03-16 01:34:13 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "move" paramPath seek
|
2010-12-30 19:06:26 +00:00
|
|
|
"move content of files to/from another repository"]
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2010-11-27 21:02:53 +00:00
|
|
|
seek = [withFilesInGit $ start True]
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2010-11-27 21:02:53 +00:00
|
|
|
{- Move (or copy) a file either --to or --from a repository.
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- This only operates on the cached file content; it does not involve
|
|
|
|
- moving data in the key-value backend. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
start :: Bool -> CommandStartString
|
2010-11-27 21:02:53 +00:00
|
|
|
start move file = do
|
2011-01-26 04:17:38 +00:00
|
|
|
to <- Annex.getState Annex.toremote
|
|
|
|
from <- Annex.getState Annex.fromremote
|
|
|
|
case (from, to) of
|
|
|
|
(Nothing, Nothing) -> error "specify either --from or --to"
|
|
|
|
(Nothing, Just name) -> do
|
2011-03-27 21:24:20 +00:00
|
|
|
dest <- Remote.byName name
|
2011-01-26 04:17:38 +00:00
|
|
|
toStart dest move file
|
|
|
|
(Just name, Nothing) -> do
|
2011-03-27 21:24:20 +00:00
|
|
|
src <- Remote.byName name
|
2011-01-26 04:17:38 +00:00
|
|
|
fromStart src move file
|
2010-11-02 23:04:24 +00:00
|
|
|
(_ , _) -> error "only one of --from or --to can be specified"
|
|
|
|
|
2010-11-27 21:02:53 +00:00
|
|
|
showAction :: Bool -> FilePath -> Annex ()
|
|
|
|
showAction True file = showStart "move" file
|
|
|
|
showAction False file = showStart "copy" file
|
|
|
|
|
2011-03-03 19:22:53 +00:00
|
|
|
{- Used to log a change in a remote's having a key. The change is logged
|
|
|
|
- in the local repo, not on the remote. The process of transferring the
|
|
|
|
- key to the remote, or removing the key from it *may* log the change
|
|
|
|
- on the remote, but this cannot be relied on. For example, it's not done
|
|
|
|
- for bare repos. -}
|
2011-03-27 21:24:20 +00:00
|
|
|
remoteHasKey :: Remote.Remote Annex -> Key -> Bool -> Annex ()
|
2010-11-27 21:02:53 +00:00
|
|
|
remoteHasKey remote key present = do
|
2011-03-27 21:24:20 +00:00
|
|
|
let remoteuuid = Remote.uuid remote
|
2011-04-25 18:54:24 +00:00
|
|
|
logStatusFor remoteuuid key status
|
2010-11-27 21:02:53 +00:00
|
|
|
where
|
|
|
|
status = if present then ValuePresent else ValueMissing
|
|
|
|
|
2011-03-27 21:24:20 +00:00
|
|
|
{- Moves (or copies) the content of an annexed file to a remote.
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
2011-03-27 21:24:20 +00:00
|
|
|
- If the remote already has the content, it is still removed from
|
|
|
|
- the current repository.
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Note that unlike drop, this does not honor annex.numcopies.
|
|
|
|
- A file's content can be moved even if there are insufficient copies to
|
|
|
|
- allow it to be dropped.
|
|
|
|
-}
|
2011-03-27 21:24:20 +00:00
|
|
|
toStart :: Remote.Remote Annex -> Bool -> CommandStartString
|
2011-01-26 04:17:38 +00:00
|
|
|
toStart dest move file = isAnnexed file $ \(key, _) -> do
|
2011-01-26 19:59:10 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-03-27 21:24:20 +00:00
|
|
|
u <- getUUID g
|
2010-11-02 23:04:24 +00:00
|
|
|
ishere <- inAnnex key
|
2011-03-27 21:24:20 +00:00
|
|
|
if not ishere || u == Remote.uuid dest
|
2011-05-15 06:02:46 +00:00
|
|
|
then stop -- not here, so nothing to do
|
2010-11-02 23:04:24 +00:00
|
|
|
else do
|
2010-11-27 21:02:53 +00:00
|
|
|
showAction move file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ toPerform dest move key
|
2011-03-27 21:24:20 +00:00
|
|
|
toPerform :: Remote.Remote Annex -> Bool -> Key -> CommandPerform
|
2011-01-26 04:17:38 +00:00
|
|
|
toPerform dest move key = do
|
2011-03-27 22:34:30 +00:00
|
|
|
-- Checking the remote is expensive, so not done in the start step.
|
|
|
|
-- In fast mode, location tracking is assumed to be correct,
|
|
|
|
-- and an explicit check is not done, when copying. When moving,
|
|
|
|
-- it has to be done, to avoid inaverdent data loss.
|
|
|
|
fast <- Annex.getState Annex.fast
|
2011-05-16 17:27:19 +00:00
|
|
|
let fastcheck = fast && not move && not (Remote.hasKeyCheap dest)
|
|
|
|
isthere <- if fastcheck
|
2011-04-01 16:34:06 +00:00
|
|
|
then do
|
2011-06-01 23:10:38 +00:00
|
|
|
(remotes, _) <- Remote.keyPossibilities key
|
2011-04-01 16:34:06 +00:00
|
|
|
return $ Right $ dest `elem` remotes
|
2011-03-27 22:34:30 +00:00
|
|
|
else Remote.hasKey dest key
|
2010-11-02 23:04:24 +00:00
|
|
|
case isthere of
|
|
|
|
Left err -> do
|
|
|
|
showNote $ show err
|
2011-05-15 06:02:46 +00:00
|
|
|
stop
|
2010-11-02 23:04:24 +00:00
|
|
|
Right False -> do
|
2011-03-27 21:24:20 +00:00
|
|
|
showNote $ "to " ++ Remote.name dest ++ "..."
|
|
|
|
ok <- Remote.storeKey dest key
|
2010-11-22 21:51:55 +00:00
|
|
|
if ok
|
2011-05-15 06:02:46 +00:00
|
|
|
then next $ toCleanup dest move key
|
2011-05-16 17:27:19 +00:00
|
|
|
else do
|
|
|
|
when fastcheck $
|
|
|
|
warning "This could have failed because --fast is enabled."
|
|
|
|
stop
|
2011-05-15 06:02:46 +00:00
|
|
|
Right True -> next $ toCleanup dest move key
|
2011-03-27 21:24:20 +00:00
|
|
|
toCleanup :: Remote.Remote Annex -> Bool -> Key -> CommandCleanup
|
2011-01-26 04:17:38 +00:00
|
|
|
toCleanup dest move key = do
|
|
|
|
remoteHasKey dest key True
|
2010-12-31 23:09:17 +00:00
|
|
|
if move
|
|
|
|
then Command.Drop.cleanup key
|
|
|
|
else return True
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-03-27 21:24:20 +00:00
|
|
|
{- Moves (or copies) the content of an annexed file from a remote
|
|
|
|
- to the current repository.
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- If the current repository already has the content, it is still removed
|
2011-03-27 21:24:20 +00:00
|
|
|
- from the remote.
|
2010-11-02 23:04:24 +00:00
|
|
|
-}
|
2011-03-27 21:24:20 +00:00
|
|
|
fromStart :: Remote.Remote Annex -> Bool -> CommandStartString
|
2011-01-26 04:17:38 +00:00
|
|
|
fromStart src move file = isAnnexed file $ \(key, _) -> do
|
2011-01-26 19:59:10 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-03-27 21:24:20 +00:00
|
|
|
u <- getUUID g
|
2011-06-01 23:10:38 +00:00
|
|
|
(remotes, _) <- Remote.keyPossibilities key
|
2011-03-27 21:24:20 +00:00
|
|
|
if (u == Remote.uuid src) || (null $ filter (== src) remotes)
|
2011-05-15 06:02:46 +00:00
|
|
|
then stop
|
2010-11-02 23:04:24 +00:00
|
|
|
else do
|
2010-11-27 21:02:53 +00:00
|
|
|
showAction move file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ fromPerform src move key
|
2011-03-27 21:24:20 +00:00
|
|
|
fromPerform :: Remote.Remote Annex -> Bool -> Key -> CommandPerform
|
2011-01-26 04:17:38 +00:00
|
|
|
fromPerform src move key = do
|
2010-11-02 23:04:24 +00:00
|
|
|
ishere <- inAnnex key
|
2010-11-22 21:51:55 +00:00
|
|
|
if ishere
|
2011-05-15 06:02:46 +00:00
|
|
|
then next $ fromCleanup src move key
|
2010-11-02 23:04:24 +00:00
|
|
|
else do
|
2011-03-27 21:24:20 +00:00
|
|
|
showNote $ "from " ++ Remote.name src ++ "..."
|
|
|
|
ok <- getViaTmp key $ Remote.retrieveKeyFile src key
|
2010-11-22 21:51:55 +00:00
|
|
|
if ok
|
2011-05-15 06:02:46 +00:00
|
|
|
then next $ fromCleanup src move key
|
|
|
|
else stop -- fail
|
2011-03-27 21:24:20 +00:00
|
|
|
fromCleanup :: Remote.Remote Annex -> Bool -> Key -> CommandCleanup
|
2011-01-26 04:17:38 +00:00
|
|
|
fromCleanup src True key = do
|
2011-03-27 21:24:20 +00:00
|
|
|
ok <- Remote.removeKey src key
|
2011-01-26 04:17:38 +00:00
|
|
|
-- better safe than sorry: assume the src dropped the key
|
2010-12-31 23:09:17 +00:00
|
|
|
-- even if it seemed to fail; the failure could have occurred
|
|
|
|
-- after it really dropped it
|
2011-01-26 04:17:38 +00:00
|
|
|
remoteHasKey src key False
|
2010-11-02 23:04:24 +00:00
|
|
|
return ok
|
2011-01-26 04:17:38 +00:00
|
|
|
fromCleanup _ False _ = return True
|