2011-10-30 03:48:46 +00:00
|
|
|
{- git-annex command seeking
|
|
|
|
-
|
|
|
|
- These functions find appropriate files or other things based on
|
|
|
|
- the values a user passes to a command, and prepare actions operating
|
|
|
|
- on them.
|
|
|
|
-
|
|
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Seek where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Command
|
|
|
|
import Types.Key
|
|
|
|
import Backend
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Git
|
|
|
|
import qualified Git.LsFiles as LsFiles
|
2011-12-13 19:22:43 +00:00
|
|
|
import qualified Git.CheckAttr
|
2011-10-30 03:48:46 +00:00
|
|
|
import qualified Limit
|
2012-01-06 14:14:37 +00:00
|
|
|
import qualified Option
|
2011-10-30 03:48:46 +00:00
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
seekHelper :: ([FilePath] -> Git.Repo -> IO [FilePath]) -> [FilePath] -> Annex [FilePath]
|
2012-01-10 19:36:54 +00:00
|
|
|
seekHelper a params = inRepo $ \g -> runPreserveOrder (`a` g) params
|
2011-11-08 19:34:10 +00:00
|
|
|
|
2011-10-30 03:48:46 +00:00
|
|
|
withFilesInGit :: (FilePath -> CommandStart) -> CommandSeek
|
2011-11-08 19:34:10 +00:00
|
|
|
withFilesInGit a params = prepFiltered a $ seekHelper LsFiles.inRepo params
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withAttrFilesInGit :: String -> ((FilePath, String) -> CommandStart) -> CommandSeek
|
|
|
|
withAttrFilesInGit attr a params = do
|
2011-11-08 19:34:10 +00:00
|
|
|
files <- seekHelper LsFiles.inRepo params
|
2011-12-13 19:22:43 +00:00
|
|
|
prepFilteredGen a fst $ inRepo $ Git.CheckAttr.lookup attr files
|
2011-10-30 03:48:46 +00:00
|
|
|
|
2011-11-11 03:35:08 +00:00
|
|
|
withNumCopies :: (Maybe Int -> FilePath -> CommandStart) -> CommandSeek
|
2011-10-30 03:48:46 +00:00
|
|
|
withNumCopies a params = withAttrFilesInGit "annex.numcopies" go params
|
|
|
|
where
|
2011-11-11 03:35:08 +00:00
|
|
|
go (file, v) = a (readMaybe v) file
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withBackendFilesInGit :: (BackendFile -> CommandStart) -> CommandSeek
|
2012-01-10 19:36:54 +00:00
|
|
|
withBackendFilesInGit a params =
|
|
|
|
prepBackendPairs a =<< seekHelper LsFiles.inRepo params
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withFilesNotInGit :: (BackendFile -> CommandStart) -> CommandSeek
|
|
|
|
withFilesNotInGit a params = do
|
2012-01-03 04:09:09 +00:00
|
|
|
{- dotfiles are not acted on unless explicitly listed -}
|
|
|
|
files <- filter (not . dotfile) <$> seek ps
|
|
|
|
dotfiles <- if null dotps then return [] else seek dotps
|
|
|
|
prepBackendPairs a $ preserveOrder params (files++dotfiles)
|
|
|
|
where
|
|
|
|
(dotps, ps) = partition dotfile params
|
|
|
|
seek l = do
|
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
g <- gitRepo
|
|
|
|
liftIO $ (\p -> LsFiles.notInRepo force p g) l
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withWords :: ([String] -> CommandStart) -> CommandSeek
|
|
|
|
withWords a params = return [a params]
|
|
|
|
|
|
|
|
withStrings :: (String -> CommandStart) -> CommandSeek
|
|
|
|
withStrings a params = return $ map a params
|
|
|
|
|
|
|
|
withFilesToBeCommitted :: (String -> CommandStart) -> CommandSeek
|
2011-11-08 19:34:10 +00:00
|
|
|
withFilesToBeCommitted a params = prepFiltered a $
|
|
|
|
seekHelper LsFiles.stagedNotDeleted params
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withFilesUnlocked :: (BackendFile -> CommandStart) -> CommandSeek
|
|
|
|
withFilesUnlocked = withFilesUnlocked' LsFiles.typeChanged
|
|
|
|
|
|
|
|
withFilesUnlockedToBeCommitted :: (BackendFile -> CommandStart) -> CommandSeek
|
|
|
|
withFilesUnlockedToBeCommitted = withFilesUnlocked' LsFiles.typeChangedStaged
|
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
withFilesUnlocked' :: ([FilePath] -> Git.Repo -> IO [FilePath]) -> (BackendFile -> CommandStart) -> CommandSeek
|
2011-10-30 03:48:46 +00:00
|
|
|
withFilesUnlocked' typechanged a params = do
|
|
|
|
-- unlocked files have changed type from a symlink to a regular file
|
2011-11-11 05:52:58 +00:00
|
|
|
top <- fromRepo Git.workTree
|
2011-11-08 19:34:10 +00:00
|
|
|
typechangedfiles <- seekHelper typechanged params
|
2011-10-30 03:48:46 +00:00
|
|
|
unlockedfiles <- liftIO $ filterM notSymlink $
|
2011-11-08 19:34:10 +00:00
|
|
|
map (\f -> top ++ "/" ++ f) typechangedfiles
|
2011-10-30 03:48:46 +00:00
|
|
|
prepBackendPairs a unlockedfiles
|
|
|
|
|
|
|
|
withKeys :: (Key -> CommandStart) -> CommandSeek
|
|
|
|
withKeys a params = return $ map (a . parse) params
|
|
|
|
where
|
|
|
|
parse p = fromMaybe (error "bad key") $ readKey p
|
|
|
|
|
2012-01-07 01:27:42 +00:00
|
|
|
withValue :: Annex v -> (v -> CommandSeek) -> CommandSeek
|
|
|
|
withValue v a params = do
|
|
|
|
r <- v
|
|
|
|
a r params
|
|
|
|
|
2012-01-06 14:14:37 +00:00
|
|
|
{- Modifies a seek action using the value of a field option, which is fed into
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
- a conversion function, and then is passed into the seek action.
|
|
|
|
- This ensures that the conversion function only runs once.
|
|
|
|
-}
|
2012-01-06 14:14:37 +00:00
|
|
|
withField :: Option -> (Maybe String -> Annex a) -> (a -> CommandSeek) -> CommandSeek
|
2012-01-07 01:27:42 +00:00
|
|
|
withField option converter = withValue $
|
|
|
|
converter =<< Annex.getField (Option.name option)
|
more command-specific options
Made --from and --to command-specific options.
Added generic storage for values of command-specific options,
which allows removing some of the special case fields in AnnexState.
(Also added generic storage for command-specific flags, although there are
not yet any.)
Note that this storage uses a Map, so repeatedly looking up the same value
is slightly more expensive than looking up an AnnexState field. But, the
value can be looked up once in the seek stage, transformed as necessary,
and passed in a closure to the start stage, and this avoids that overhead.
Still, I'm hesitant to use this for things like force or fast flags.
It's probably best to reserve it for flags that are only used by a few
commands, or options like --from and --to that it's important only be
allowed to be used with commands that implement them, to avoid user
confusion.
2012-01-06 07:06:25 +00:00
|
|
|
|
2012-01-07 22:13:12 +00:00
|
|
|
withFlag :: Option -> (Bool -> CommandSeek) -> CommandSeek
|
|
|
|
withFlag option = withValue $ Annex.getFlag (Option.name option)
|
|
|
|
|
2011-10-30 03:48:46 +00:00
|
|
|
withNothing :: CommandStart -> CommandSeek
|
|
|
|
withNothing a [] = return [a]
|
|
|
|
withNothing _ _ = error "This command takes no parameters."
|
|
|
|
|
|
|
|
|
|
|
|
prepFiltered :: (FilePath -> CommandStart) -> Annex [FilePath] -> Annex [CommandStart]
|
|
|
|
prepFiltered a = prepFilteredGen a id
|
|
|
|
|
|
|
|
prepBackendPairs :: (BackendFile -> CommandStart) -> CommandSeek
|
|
|
|
prepBackendPairs a fs = prepFilteredGen a snd (chooseBackends fs)
|
|
|
|
|
|
|
|
prepFilteredGen :: (b -> CommandStart) -> (b -> FilePath) -> Annex [b] -> Annex [CommandStart]
|
|
|
|
prepFilteredGen a d fs = do
|
|
|
|
matcher <- Limit.getMatcher
|
2012-01-10 19:36:54 +00:00
|
|
|
map (proc matcher) <$> fs
|
2011-10-30 03:48:46 +00:00
|
|
|
where
|
|
|
|
proc matcher v = do
|
|
|
|
let f = d v
|
|
|
|
ok <- matcher f
|
|
|
|
if ok then a v else return Nothing
|
|
|
|
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
|
|
|
notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
|