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
|
|
|
|
|
2011-11-08 19:34:10 +00:00
|
|
|
seekHelper :: ([FilePath] -> Git.Repo -> IO [FilePath]) -> [FilePath] -> Annex [FilePath]
|
|
|
|
seekHelper a params = do
|
|
|
|
g <- gitRepo
|
2011-11-11 05:52:58 +00:00
|
|
|
liftIO $ 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
|
|
|
|
withBackendFilesInGit a params = do
|
2011-11-08 19:34:10 +00:00
|
|
|
files <- seekHelper LsFiles.inRepo params
|
2011-10-30 03:48:46 +00:00
|
|
|
prepBackendPairs a files
|
|
|
|
|
|
|
|
withFilesMissing :: (String -> CommandStart) -> CommandSeek
|
|
|
|
withFilesMissing a params = prepFiltered a $ liftIO $ filterM missing params
|
|
|
|
where
|
|
|
|
missing = liftM not . doesFileExist
|
|
|
|
|
|
|
|
withFilesNotInGit :: (BackendFile -> CommandStart) -> CommandSeek
|
|
|
|
withFilesNotInGit a params = do
|
|
|
|
force <- Annex.getState Annex.force
|
2011-11-08 19:34:10 +00:00
|
|
|
newfiles <- seekHelper (LsFiles.notInRepo force) params
|
2011-10-30 03:48:46 +00:00
|
|
|
prepBackendPairs a newfiles
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
prepStart (proc matcher) fs
|
|
|
|
where
|
|
|
|
proc matcher v = do
|
|
|
|
let f = d v
|
|
|
|
ok <- matcher f
|
|
|
|
if ok then a v else return Nothing
|
|
|
|
|
|
|
|
{- Generates a list of CommandStart actions that will be run to perform a
|
|
|
|
- command, using a list (ie of files) coming from an action. The list
|
|
|
|
- will be produced and consumed lazily. -}
|
|
|
|
prepStart :: (b -> CommandStart) -> Annex [b] -> Annex [CommandStart]
|
2011-11-11 05:52:58 +00:00
|
|
|
prepStart a = liftM (map a)
|
2011-10-30 03:48:46 +00:00
|
|
|
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
|
|
|
notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
|