2012-05-31 23:47:18 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2013-08-11 18:31:54 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <joey@kitenet.net>
|
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
|
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]
|
|
|
|
opts =
|
|
|
|
[ duplicateOption
|
|
|
|
, deduplicateOption
|
|
|
|
, cleanDuplicatesOption
|
2013-12-04 17:13:30 +00:00
|
|
|
, skipDuplicatesOption
|
2013-08-11 18:31:54 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
duplicateOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
duplicateOption = flagOption [] "duplicate" "do not delete source files"
|
2013-08-11 18:31:54 +00:00
|
|
|
|
|
|
|
deduplicateOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
deduplicateOption = flagOption [] "deduplicate" "delete source files whose content was imported before"
|
2013-08-11 18:31:54 +00:00
|
|
|
|
|
|
|
cleanDuplicatesOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
cleanDuplicatesOption = flagOption [] "clean-duplicates" "delete duplicate source files (import nothing)"
|
2013-08-11 18:31:54 +00:00
|
|
|
|
2013-12-04 17:13:30 +00:00
|
|
|
skipDuplicatesOption :: Option
|
2014-01-26 20:25:55 +00:00
|
|
|
skipDuplicatesOption = flagOption [] "skip-duplicates" "import only new files"
|
2013-12-04 17:13:30 +00:00
|
|
|
|
|
|
|
data DuplicateMode = Default | Duplicate | DeDuplicate | CleanDuplicates | SkipDuplicates
|
2013-08-11 18:31:54 +00:00
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
getDuplicateMode :: Annex DuplicateMode
|
|
|
|
getDuplicateMode = gen
|
|
|
|
<$> getflag duplicateOption
|
|
|
|
<*> getflag deduplicateOption
|
|
|
|
<*> getflag cleanDuplicatesOption
|
2013-12-04 17:13:30 +00:00
|
|
|
<*> getflag skipDuplicatesOption
|
2013-08-11 18:31:54 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
getflag = Annex.getFlag . optionName
|
|
|
|
gen False False False False = Default
|
2013-12-04 17:13:30 +00:00
|
|
|
gen True False False False = Duplicate
|
|
|
|
gen False True False False = DeDuplicate
|
|
|
|
gen False False True False = CleanDuplicates
|
|
|
|
gen False False False True = SkipDuplicates
|
|
|
|
gen _ _ _ _ = error "bad combination of --duplicate, --deduplicate, --clean-duplicates, --skip-duplicates"
|
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
|
2013-12-04 17:13:30 +00:00
|
|
|
isdup <- do
|
|
|
|
backend <- chooseBackend destfile
|
|
|
|
let ks = KeySource srcfile srcfile Nothing
|
|
|
|
v <- genKey ks backend
|
|
|
|
case v of
|
|
|
|
Just (k, _) -> not . null <$> keyLocations k
|
|
|
|
_ -> return False
|
|
|
|
case pickaction isdup of
|
|
|
|
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
|
|
|
|
deletedup = do
|
|
|
|
showNote "duplicate"
|
|
|
|
liftIO $ removeFile srcfile
|
|
|
|
next $ return True
|
2013-12-04 17:13:30 +00:00
|
|
|
importfile = do
|
2013-12-09 17:37:18 +00:00
|
|
|
handleexisting =<< liftIO (catchMaybeIO $ getSymbolicLinkStatus destfile)
|
2015-01-09 17:11:56 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir destfile)
|
2013-12-04 17:13:30 +00:00
|
|
|
liftIO $ if mode == Duplicate || mode == SkipDuplicates
|
2014-08-27 00:06:43 +00:00
|
|
|
then void $ copyFileExternal CopyAllMetaData srcfile destfile
|
2013-08-20 15:00:52 +00:00
|
|
|
else moveFile srcfile destfile
|
|
|
|
Command.Add.perform destfile
|
2013-12-09 17:37:18 +00:00
|
|
|
handleexisting Nothing = noop
|
|
|
|
handleexisting (Just s)
|
|
|
|
| isDirectory s = notoverwriting "(is a directory)"
|
2014-10-09 19:35:19 +00:00
|
|
|
| otherwise = ifM (Annex.getState Annex.force)
|
2013-12-09 17:37:18 +00:00
|
|
|
( liftIO $ nukeFile destfile
|
|
|
|
, notoverwriting "(use --force to override)"
|
|
|
|
)
|
|
|
|
notoverwriting why = error $ "not overwriting existing " ++ destfile ++ " " ++ why
|
2013-12-04 17:13:30 +00:00
|
|
|
pickaction isdup = case mode of
|
|
|
|
DeDuplicate
|
|
|
|
| isdup -> Just deletedup
|
|
|
|
| otherwise -> Just importfile
|
|
|
|
CleanDuplicates
|
|
|
|
| isdup -> Just deletedup
|
|
|
|
| otherwise -> Nothing
|
|
|
|
SkipDuplicates
|
|
|
|
| isdup -> Nothing
|
|
|
|
| otherwise -> Just importfile
|
|
|
|
_ -> Just importfile
|
|
|
|
|