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.
|
|
|
|
-
|
2012-05-31 23:47:18 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2011-10-30 03:48:46 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Seek where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Command
|
|
|
|
import Types.Key
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Git
|
2012-10-04 23:56:32 +00:00
|
|
|
import qualified Git.Command
|
2011-10-30 03:48:46 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
|
|
|
import qualified Limit
|
2012-01-06 14:14:37 +00:00
|
|
|
import qualified Option
|
2013-01-06 20:56:55 +00:00
|
|
|
import Config
|
2011-10-30 03:48:46 +00:00
|
|
|
|
2012-10-04 23:56:32 +00:00
|
|
|
seekHelper :: ([FilePath] -> Git.Repo -> IO ([FilePath], IO Bool)) -> [FilePath] -> Annex [FilePath]
|
2012-11-25 21:54:08 +00:00
|
|
|
seekHelper a params = do
|
|
|
|
ll <- inRepo $ \g ->
|
|
|
|
runSegmentPaths (\fs -> Git.Command.leaveZombie <$> a fs g) params
|
|
|
|
{- Show warnings only for files/directories that do not exist. -}
|
|
|
|
forM_ (map fst $ filter (null . snd) $ zip params ll) $ \p ->
|
2013-01-26 04:14:03 +00:00
|
|
|
unlessM (isJust <$> (liftIO $ catchMaybeIO $ getSymbolicLinkStatus p)) $
|
2012-11-25 21:54:08 +00:00
|
|
|
fileNotFound p
|
|
|
|
return $ concat ll
|
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
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
withFilesNotInGit :: (FilePath -> CommandStart) -> CommandSeek
|
2011-10-30 03:48:46 +00:00
|
|
|
withFilesNotInGit a params = do
|
2012-01-03 04:09:09 +00:00
|
|
|
{- dotfiles are not acted on unless explicitly listed -}
|
2012-04-08 16:25:54 +00:00
|
|
|
files <- filter (not . dotfile) <$>
|
|
|
|
seekunless (null ps && not (null params)) ps
|
|
|
|
dotfiles <- seekunless (null dotps) dotps
|
2012-11-25 21:54:08 +00:00
|
|
|
prepFiltered a $ return $ concat $ segmentPaths params (files++dotfiles)
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
(dotps, ps) = partition dotfile params
|
|
|
|
seekunless True _ = return []
|
|
|
|
seekunless _ l = do
|
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
g <- gitRepo
|
|
|
|
liftIO $ Git.Command.leaveZombie <$> LsFiles.notInRepo force l g
|
2011-10-30 03:48:46 +00:00
|
|
|
|
2012-05-31 23:47:18 +00:00
|
|
|
withPathContents :: ((FilePath, FilePath) -> CommandStart) -> CommandSeek
|
|
|
|
withPathContents a params = map a . concat <$> liftIO (mapM get params)
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
get p = ifM (isDirectory <$> getFileStatus p)
|
|
|
|
( map (\f -> (f, makeRelative p f)) <$> dirContentsRecursive p
|
|
|
|
, return [(p, takeFileName p)]
|
|
|
|
)
|
2012-05-31 23:47:18 +00:00
|
|
|
|
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
|
|
|
|
|
2012-02-16 20:36:35 +00:00
|
|
|
withPairs :: ((String, String) -> CommandStart) -> CommandSeek
|
|
|
|
withPairs a params = return $ map a $ pairs [] params
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
pairs c [] = reverse c
|
|
|
|
pairs c (x:y:xs) = pairs ((x,y):c) xs
|
|
|
|
pairs _ _ = error "expected pairs"
|
2012-02-16 20:36:35 +00:00
|
|
|
|
2011-10-30 03:48:46 +00:00
|
|
|
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
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
withFilesUnlocked :: (FilePath -> CommandStart) -> CommandSeek
|
2011-10-30 03:48:46 +00:00
|
|
|
withFilesUnlocked = withFilesUnlocked' LsFiles.typeChanged
|
|
|
|
|
2012-02-14 03:42:44 +00:00
|
|
|
withFilesUnlockedToBeCommitted :: (FilePath -> CommandStart) -> CommandSeek
|
2011-10-30 03:48:46 +00:00
|
|
|
withFilesUnlockedToBeCommitted = withFilesUnlocked' LsFiles.typeChangedStaged
|
|
|
|
|
2012-10-04 23:56:32 +00:00
|
|
|
withFilesUnlocked' :: ([FilePath] -> Git.Repo -> IO ([FilePath], IO Bool)) -> (FilePath -> 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-08 19:34:10 +00:00
|
|
|
typechangedfiles <- seekHelper typechanged params
|
2012-02-14 04:22:42 +00:00
|
|
|
let unlockedfiles = liftIO $ filterM notSymlink typechangedfiles
|
2012-02-14 03:42:44 +00:00
|
|
|
prepFiltered a unlockedfiles
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
withKeys :: (Key -> CommandStart) -> CommandSeek
|
|
|
|
withKeys a params = return $ map (a . parse) params
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
parse p = fromMaybe (error "bad key") $ file2key p
|
2011-10-30 03:48:46 +00:00
|
|
|
|
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 $
|
2012-06-14 04:01:48 +00:00
|
|
|
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]
|
2012-02-14 05:11:02 +00:00
|
|
|
prepFiltered a fs = do
|
2011-10-30 03:48:46 +00:00
|
|
|
matcher <- Limit.getMatcher
|
2012-07-19 04:43:36 +00:00
|
|
|
map (process matcher) <$> fs
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
process matcher f = ifM (matcher $ Annex.FileInfo f f)
|
|
|
|
( a f , return Nothing )
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
|
|
|
notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
|
2013-01-06 20:56:55 +00:00
|
|
|
|
|
|
|
whenNotDirect :: CommandSeek -> CommandSeek
|
|
|
|
whenNotDirect a params = ifM isDirect ( return [] , a params )
|
2013-02-06 16:40:59 +00:00
|
|
|
|
|
|
|
whenDirect :: CommandSeek -> CommandSeek
|
|
|
|
whenDirect a params = ifM isDirect ( a params, return [] )
|