2012-05-31 23:47:18 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <id@joeyh.name>
|
2012-05-31 23:47:18 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Import where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Command.Add
|
2013-08-11 18:31:54 +00:00
|
|
|
import Utility.CopyFile
|
2013-08-20 15:00:52 +00:00
|
|
|
import Backend
|
|
|
|
import Remote
|
|
|
|
import Types.KeySource
|
2015-03-31 19:36:02 +00:00
|
|
|
import Types.Key
|
2015-04-29 17:46:12 +00:00
|
|
|
import Annex.CheckIgnore
|
2015-04-30 18:03:24 +00:00
|
|
|
import Annex.NumCopies
|
|
|
|
import Types.TrustLevel
|
|
|
|
import Logs.Trust
|
2012-05-31 23:47:18 +00:00
|
|
|
|
2014-10-14 18:20:10 +00:00
|
|
|
cmd :: [Command]
|
|
|
|
cmd = [withOptions opts $ notBareRepo $ command "import" paramPaths seek
|
2013-03-24 22:28:21 +00:00
|
|
|
SectionCommon "move and add files from outside git working copy"]
|
2012-05-31 23:47:18 +00:00
|
|
|
|
2013-08-11 18:31:54 +00:00
|
|
|
opts :: [Option]
|
2015-02-08 19:04:58 +00:00
|
|
|
opts = duplicateModeOptions ++ fileMatchingOptions
|
2013-08-11 18:31:54 +00:00
|
|
|
|
2015-02-08 19:04:58 +00:00
|
|
|
data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
|
|
|
|
deriving (Eq, Enum, Bounded)
|
2013-08-11 18:31:54 +00:00
|
|
|
|
2015-02-08 19:04:58 +00:00
|
|
|
associatedOption :: DuplicateMode -> Maybe Option
|
|
|
|
associatedOption Default = Nothing
|
|
|
|
associatedOption Duplicate = Just $
|
|
|
|
flagOption [] "duplicate" "do not delete source files"
|
|
|
|
associatedOption DeDuplicate = Just $
|
|
|
|
flagOption [] "deduplicate" "delete source files whose content was imported before"
|
|
|
|
associatedOption CleanDuplicates = Just $
|
|
|
|
flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
|
|
|
|
associatedOption SkipDuplicates = Just $
|
|
|
|
flagOption [] "skip-duplicates" "import only new files"
|
2013-12-04 17:13:30 +00:00
|
|
|
|
2015-02-08 19:04:58 +00:00
|
|
|
duplicateModeOptions :: [Option]
|
|
|
|
duplicateModeOptions = mapMaybe associatedOption [minBound..maxBound]
|
2013-08-11 18:31:54 +00:00
|
|
|
|
|
|
|
getDuplicateMode :: Annex DuplicateMode
|
2015-02-08 19:04:58 +00:00
|
|
|
getDuplicateMode = go . catMaybes <$> mapM getflag [minBound..maxBound]
|
2013-08-11 18:31:54 +00:00
|
|
|
where
|
2015-02-08 19:04:58 +00:00
|
|
|
getflag m = case associatedOption m of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just o -> ifM (Annex.getFlag (optionName o))
|
|
|
|
( return (Just m)
|
|
|
|
, return Nothing
|
|
|
|
)
|
|
|
|
go [] = Default
|
|
|
|
go [m] = m
|
|
|
|
go ms = error $ "cannot combine " ++
|
|
|
|
unwords (map (optionParam . fromJust . associatedOption) ms)
|
2013-08-11 18:31:54 +00:00
|
|
|
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek ps = do
|
|
|
|
mode <- getDuplicateMode
|
|
|
|
withPathContents (start mode) ps
|
2012-05-31 23:47:18 +00:00
|
|
|
|
2013-08-11 18:31:54 +00:00
|
|
|
start :: DuplicateMode -> (FilePath, FilePath) -> CommandStart
|
|
|
|
start mode (srcfile, destfile) =
|
2012-05-31 23:47:18 +00:00
|
|
|
ifM (liftIO $ isRegularFile <$> getSymbolicLinkStatus srcfile)
|
|
|
|
( do
|
2015-02-08 18:43:42 +00:00
|
|
|
ma <- pickaction
|
|
|
|
case ma of
|
2013-12-04 17:13:30 +00:00
|
|
|
Nothing -> stop
|
|
|
|
Just a -> do
|
|
|
|
showStart "import" destfile
|
|
|
|
next a
|
2012-05-31 23:47:18 +00:00
|
|
|
, stop
|
|
|
|
)
|
2013-08-20 15:00:52 +00:00
|
|
|
where
|
2015-03-31 19:36:02 +00:00
|
|
|
deletedup k = do
|
|
|
|
showNote $ "duplicate of " ++ key2file k
|
2015-04-30 18:03:24 +00:00
|
|
|
ifM (verifiedExisting k destfile)
|
|
|
|
( do
|
|
|
|
liftIO $ removeFile srcfile
|
|
|
|
next $ return True
|
|
|
|
, do
|
2015-04-30 18:10:28 +00:00
|
|
|
warning "Could not verify that the content is still present in the annex; not removing from the import location."
|
2015-04-30 18:03:24 +00:00
|
|
|
stop
|
|
|
|
)
|
2013-12-04 17:13:30 +00:00
|
|
|
importfile = do
|
2015-04-29 17:46:12 +00:00
|
|
|
ignored <- not <$> Annex.getState Annex.force <&&> checkIgnored destfile
|
|
|
|
if ignored
|
2015-04-29 17:56:41 +00:00
|
|
|
then do
|
|
|
|
warning $ "not importing " ++ destfile ++ " which is .gitignored (use --force to override)"
|
|
|
|
stop
|
2015-04-29 17:46:12 +00:00
|
|
|
else do
|
2015-04-29 17:56:41 +00:00
|
|
|
existing <- liftIO (catchMaybeIO $ getSymbolicLinkStatus destfile)
|
|
|
|
case existing of
|
|
|
|
Nothing -> importfilechecked
|
|
|
|
(Just s)
|
|
|
|
| isDirectory s -> notoverwriting "(is a directory)"
|
|
|
|
| otherwise -> ifM (Annex.getState Annex.force)
|
|
|
|
( do
|
|
|
|
liftIO $ nukeFile destfile
|
|
|
|
importfilechecked
|
|
|
|
, notoverwriting "(use --force to override, or a duplication option such as --deduplicate to clean up)"
|
|
|
|
)
|
|
|
|
importfilechecked = do
|
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir destfile)
|
|
|
|
liftIO $ if mode == Duplicate || mode == SkipDuplicates
|
|
|
|
then void $ copyFileExternal CopyAllMetaData srcfile destfile
|
|
|
|
else moveFile srcfile destfile
|
|
|
|
Command.Add.perform destfile
|
|
|
|
notoverwriting why = do
|
|
|
|
warning $ "not overwriting existing " ++ destfile ++ " " ++ why
|
|
|
|
stop
|
2015-02-08 18:43:42 +00:00
|
|
|
checkdup dupa notdupa = do
|
|
|
|
backend <- chooseBackend destfile
|
|
|
|
let ks = KeySource srcfile srcfile Nothing
|
|
|
|
v <- genKey ks backend
|
2015-03-31 19:36:02 +00:00
|
|
|
case v of
|
|
|
|
Just (k, _) -> ifM (not . null <$> keyLocations k)
|
|
|
|
( return (maybe Nothing (\a -> Just (a k)) dupa)
|
|
|
|
, return notdupa
|
|
|
|
)
|
|
|
|
_ -> return notdupa
|
2015-02-08 18:43:42 +00:00
|
|
|
pickaction = case mode of
|
|
|
|
DeDuplicate -> checkdup (Just deletedup) (Just importfile)
|
|
|
|
CleanDuplicates -> checkdup (Just deletedup) Nothing
|
|
|
|
SkipDuplicates -> checkdup Nothing (Just importfile)
|
|
|
|
_ -> return (Just importfile)
|
2015-04-30 18:03:24 +00:00
|
|
|
|
|
|
|
verifiedExisting :: Key -> FilePath -> Annex Bool
|
|
|
|
verifiedExisting key destfile = do
|
|
|
|
-- Look up the numcopies setting for the file that it would be
|
|
|
|
-- imported to, if it were imported.
|
|
|
|
need <- getFileNumCopies destfile
|
|
|
|
|
|
|
|
(remotes, trusteduuids) <- knownCopies key
|
|
|
|
untrusteduuids <- trustGet UnTrusted
|
|
|
|
let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids++untrusteduuids)
|
|
|
|
verifyEnoughCopies [] key need trusteduuids [] tocheck
|