fromkey: Made idempotent

If the worktree file already exists, and is annexed and uses the same
key, avoid failing, nothing needs to be done.

Had to add lookupFileNotHidden to handle the case where an adjust --hide-missing
is in use, and the worktree file was hidden due to the object content
being missing. lookupFile would return the key of the hidden file,
but it makes sense that after fromkey succeeds, the worktree must
contain the file it was supposed to set up.
This commit is contained in:
Joey Hess 2019-02-05 13:13:09 -04:00
parent 995628d94a
commit 7b46b43c48
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 35 additions and 12 deletions

View file

@ -1,6 +1,6 @@
{- git-annex worktree files {- git-annex worktree files
- -
- Copyright 2013-2018 Joey Hess <id@joeyh.name> - Copyright 2013-2019 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -36,13 +36,28 @@ import qualified Database.Keys.SQL
- pointer to a key in the original branch. - pointer to a key in the original branch.
-} -}
lookupFile :: FilePath -> Annex (Maybe Key) lookupFile :: FilePath -> Annex (Maybe Key)
lookupFile file = isAnnexLink file >>= \case lookupFile = lookupFile' catkeyfile
Just key -> return (Just key) where
Nothing -> ifM (versionSupportsUnlockedPointers <||> isDirect) catkeyfile file =
( ifM (liftIO $ doesFileExist file) ifM (liftIO $ doesFileExist file)
( catKeyFile file ( catKeyFile file
, catKeyFileHidden file =<< getCurrentBranch , catKeyFileHidden file =<< getCurrentBranch
) )
lookupFileNotHidden :: FilePath -> Annex (Maybe Key)
lookupFileNotHidden = lookupFile' catkeyfile
where
catkeyfile file =
ifM (liftIO $ doesFileExist file)
( catKeyFile file
, return Nothing
)
lookupFile' :: (FilePath -> Annex (Maybe Key)) -> FilePath -> Annex (Maybe Key)
lookupFile' catkeyfile file = isAnnexLink file >>= \case
Just key -> return (Just key)
Nothing -> ifM (versionSupportsUnlockedPointers <||> isDirect)
( catkeyfile file
, return Nothing , return Nothing
) )

View file

@ -11,6 +11,7 @@ git-annex (7.20190130) UNRELEASED; urgency=medium
* Display progress bar when getting files from export remotes. * Display progress bar when getting files from export remotes.
* Fix race in cleanup of othertmp directory that could result in a failure * Fix race in cleanup of othertmp directory that could result in a failure
attempting to access it. attempting to access it.
* fromkey: Made idempotent.
-- Joey Hess <id@joeyh.name> Wed, 30 Jan 2019 12:30:22 -0400 -- Joey Hess <id@joeyh.name> Wed, 30 Jan 2019 12:30:22 -0400

View file

@ -1,6 +1,6 @@
{- git-annex command {- git-annex command
- -
- Copyright 2010-2018 Joey Hess <id@joeyh.name> - Copyright 2010-2019 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -12,6 +12,7 @@ module Command.FromKey where
import Command import Command
import qualified Annex.Queue import qualified Annex.Queue
import Annex.Content import Annex.Content
import Annex.WorkTree
import qualified Annex import qualified Annex
import qualified Backend.URL import qualified Backend.URL
@ -89,9 +90,13 @@ perform key file = do
next $ return ok next $ return ok
perform' :: Key -> FilePath -> Annex Bool perform' :: Key -> FilePath -> Annex Bool
perform' key file = do perform' key file = lookupFileNotHidden file >>= \case
link <- calcRepo $ gitAnnexLink file key Nothing -> do
liftIO $ createDirectoryIfMissing True (parentDir file) link <- calcRepo $ gitAnnexLink file key
liftIO $ createSymbolicLink link file liftIO $ createDirectoryIfMissing True (parentDir file)
Annex.Queue.addCommand "add" [Param "--"] [file] liftIO $ createSymbolicLink link file
return True Annex.Queue.addCommand "add" [Param "--"] [file]
return True
Just k
| k == key -> return True
| otherwise -> giveup $ file ++ " already exists with different content"

View file

@ -1 +1,3 @@
Currently, git-annex-fromkey errors if the target file exists. It would help if, when the target file is the same symlink that would be created by the command (i.e. to the same key), this wasn't considered an error. Together with [[todo/batch_command_result_status]] this would make the command a more robust building block for higher-level operations. Currently, git-annex-fromkey errors if the target file exists. It would help if, when the target file is the same symlink that would be created by the command (i.e. to the same key), this wasn't considered an error. Together with [[todo/batch_command_result_status]] this would make the command a more robust building block for higher-level operations.
> idempotency is good, [[done]] --[[Joey]]