2011-09-15 20:57:02 +00:00
|
|
|
{- git-annex command infrastructure
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
2011-09-19 05:37:04 +00:00
|
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command where
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-04 17:28:49 +00:00
|
|
|
import qualified Backend
|
|
|
|
import qualified Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.LsFiles as LsFiles
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
2011-09-15 17:30:04 +00:00
|
|
|
import Trust
|
|
|
|
import LocationLog
|
|
|
|
import Config
|
2011-09-15 20:57:02 +00:00
|
|
|
import Backend
|
2011-09-18 21:47:49 +00:00
|
|
|
import Limit
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
{- A command runs in four stages.
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
2010-12-30 18:19:16 +00:00
|
|
|
- 0. The seek stage takes the parameters passed to the command,
|
2010-11-02 23:04:24 +00:00
|
|
|
- looks through the repo to find the ones that are relevant
|
2010-12-30 18:19:16 +00:00
|
|
|
- to that command (ie, new files to add), and generates
|
2010-11-02 23:04:24 +00:00
|
|
|
- a list of start stage actions. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandSeek = [String] -> Annex [CommandStart]
|
2010-11-02 23:04:24 +00:00
|
|
|
{- 1. The start stage is run before anything is printed about the
|
2010-12-30 18:19:16 +00:00
|
|
|
- command, is passed some input, and can early abort it
|
2010-11-02 23:04:24 +00:00
|
|
|
- if the input does not make sense. It should run quickly and
|
|
|
|
- should not modify Annex state. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandStart = Annex (Maybe CommandPerform)
|
|
|
|
{- 2. The perform stage is run after a message is printed about the command
|
2010-11-02 23:04:24 +00:00
|
|
|
- being run, and it should be where the bulk of the work happens. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandPerform = Annex (Maybe CommandCleanup)
|
2010-11-02 23:04:24 +00:00
|
|
|
{- 3. The cleanup stage is run only if the perform stage succeeds, and it
|
2010-12-30 18:19:16 +00:00
|
|
|
- returns the overall success/fail of the command. -}
|
|
|
|
type CommandCleanup = Annex Bool
|
2011-09-15 20:50:49 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
data Command = Command {
|
2011-05-15 06:12:17 +00:00
|
|
|
cmdusesrepo :: Bool,
|
2010-12-30 18:19:16 +00:00
|
|
|
cmdname :: String,
|
|
|
|
cmdparams :: String,
|
|
|
|
cmdseek :: [CommandSeek],
|
2011-05-15 06:12:17 +00:00
|
|
|
cmddesc :: String
|
2010-11-04 17:28:49 +00:00
|
|
|
}
|
|
|
|
|
2011-05-15 06:02:46 +00:00
|
|
|
{- Most commands operate on files in a git repo. -}
|
2011-03-19 22:58:49 +00:00
|
|
|
repoCommand :: String -> String -> [CommandSeek] -> String -> Command
|
2011-05-15 06:12:17 +00:00
|
|
|
repoCommand = Command True
|
2011-03-19 22:58:49 +00:00
|
|
|
|
2011-05-15 06:02:46 +00:00
|
|
|
{- Others can run anywhere. -}
|
2011-03-19 22:58:49 +00:00
|
|
|
standaloneCommand :: String -> String -> [CommandSeek] -> String -> Command
|
2011-05-15 06:12:17 +00:00
|
|
|
standaloneCommand = Command False
|
2011-03-19 22:58:49 +00:00
|
|
|
|
2011-05-15 06:02:46 +00:00
|
|
|
{- For start and perform stages to indicate what step to run next. -}
|
|
|
|
next :: a -> Annex (Maybe a)
|
|
|
|
next a = return $ Just a
|
|
|
|
|
|
|
|
{- Or to indicate nothing needs to be done. -}
|
|
|
|
stop :: Annex (Maybe a)
|
|
|
|
stop = return Nothing
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
{- Prepares a list of actions to run to perform a command, based on
|
2010-11-04 17:28:49 +00:00
|
|
|
- the parameters passed to it. -}
|
2011-03-19 22:58:49 +00:00
|
|
|
prepCommand :: Command -> [String] -> Annex [Annex Bool]
|
2011-09-19 05:37:04 +00:00
|
|
|
prepCommand Command { cmdseek = seek } params =
|
|
|
|
return . map doCommand . concat =<< mapM (\s -> s params) seek
|
2010-11-04 17:28:49 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
{- Runs a command through the start, perform and cleanup stages -}
|
|
|
|
doCommand :: CommandStart -> CommandCleanup
|
2011-05-15 16:25:58 +00:00
|
|
|
doCommand = start
|
|
|
|
where
|
2011-07-05 18:58:33 +00:00
|
|
|
start = stage $ maybe success perform
|
|
|
|
perform = stage $ maybe failure cleanup
|
2011-05-15 16:25:58 +00:00
|
|
|
cleanup = stage $ \r -> showEndResult r >> return r
|
2011-09-19 05:37:04 +00:00
|
|
|
stage = (=<<)
|
2011-07-05 18:58:33 +00:00
|
|
|
success = return True
|
2011-09-19 05:37:04 +00:00
|
|
|
failure = showEndFail >> return False
|
2010-11-04 17:28:49 +00:00
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a)
|
2011-05-15 06:49:43 +00:00
|
|
|
notAnnexed file a = maybe a (const $ return Nothing) =<< Backend.lookupFile file
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-01-26 01:02:34 +00:00
|
|
|
isAnnexed :: FilePath -> ((Key, Backend Annex) -> Annex (Maybe a)) -> Annex (Maybe a)
|
2011-05-15 06:49:43 +00:00
|
|
|
isAnnexed file a = maybe (return Nothing) a =<< Backend.lookupFile file
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2011-03-03 20:22:53 +00:00
|
|
|
notBareRepo :: Annex a -> Annex a
|
|
|
|
notBareRepo a = do
|
2011-10-04 02:24:57 +00:00
|
|
|
whenM (Git.repoIsLocalBare <$> gitRepo) $
|
2011-03-03 20:22:53 +00:00
|
|
|
error "You cannot run this subcommand in a bare repository."
|
|
|
|
a
|
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
{- These functions find appropriate files or other things based on a
|
2011-09-19 05:37:04 +00:00
|
|
|
user's parameters, and prepare actions operating on them. -}
|
2011-09-19 02:40:31 +00:00
|
|
|
withFilesInGit :: (FilePath -> CommandStart) -> CommandSeek
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesInGit a params = do
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-09-19 02:40:31 +00:00
|
|
|
runFiltered a $ liftIO $ runPreserveOrder (LsFiles.inRepo repo) params
|
2011-09-15 20:24:47 +00:00
|
|
|
withAttrFilesInGit :: String -> ((FilePath, String) -> CommandStart) -> CommandSeek
|
2010-11-28 19:28:20 +00:00
|
|
|
withAttrFilesInGit attr a params = do
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-06-29 15:55:16 +00:00
|
|
|
files <- liftIO $ runPreserveOrder (LsFiles.inRepo repo) params
|
2011-09-19 03:09:40 +00:00
|
|
|
runFilteredGen a fst $ liftIO $ Git.checkAttr repo attr files
|
2011-09-15 20:24:47 +00:00
|
|
|
withNumCopies :: (FilePath -> Maybe Int -> CommandStart) -> CommandSeek
|
|
|
|
withNumCopies a params = withAttrFilesInGit "annex.numcopies" go params
|
|
|
|
where
|
2011-09-19 03:09:40 +00:00
|
|
|
go (file, v) = a file (readMaybe v)
|
2011-09-15 20:50:49 +00:00
|
|
|
withBackendFilesInGit :: (BackendFile -> CommandStart) -> CommandSeek
|
2011-01-08 19:54:14 +00:00
|
|
|
withBackendFilesInGit a params = do
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-06-29 15:55:16 +00:00
|
|
|
files <- liftIO $ runPreserveOrder (LsFiles.inRepo repo) params
|
2011-09-19 02:40:31 +00:00
|
|
|
backendPairs a files
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesMissing :: (String -> CommandStart) -> CommandSeek
|
2011-09-19 02:40:31 +00:00
|
|
|
withFilesMissing a params = runFiltered a $ liftIO $ filterM missing params
|
2010-11-11 22:54:52 +00:00
|
|
|
where
|
2011-09-19 02:40:31 +00:00
|
|
|
missing = liftM not . doesFileExist
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesNotInGit :: (BackendFile -> CommandStart) -> CommandSeek
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesNotInGit a params = do
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-06-29 15:42:00 +00:00
|
|
|
force <- Annex.getState Annex.force
|
2011-06-29 15:55:16 +00:00
|
|
|
newfiles <- liftIO $ runPreserveOrder (LsFiles.notInRepo repo force) params
|
2011-09-19 02:40:31 +00:00
|
|
|
backendPairs a newfiles
|
2011-09-15 20:50:49 +00:00
|
|
|
withWords :: ([String] -> CommandStart) -> CommandSeek
|
2011-05-16 16:25:54 +00:00
|
|
|
withWords a params = return [a params]
|
2011-09-15 20:50:49 +00:00
|
|
|
withStrings :: (String -> CommandStart) -> CommandSeek
|
2010-11-15 22:04:19 +00:00
|
|
|
withStrings a params = return $ map a params
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesToBeCommitted :: (String -> CommandStart) -> CommandSeek
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesToBeCommitted a params = do
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-09-19 02:40:31 +00:00
|
|
|
runFiltered a $
|
|
|
|
liftIO $ runPreserveOrder (LsFiles.stagedNotDeleted repo) params
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesUnlocked :: (BackendFile -> CommandStart) -> CommandSeek
|
2011-06-29 15:55:16 +00:00
|
|
|
withFilesUnlocked = withFilesUnlocked' LsFiles.typeChanged
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesUnlockedToBeCommitted :: (BackendFile -> CommandStart) -> CommandSeek
|
2011-06-29 15:55:16 +00:00
|
|
|
withFilesUnlockedToBeCommitted = withFilesUnlocked' LsFiles.typeChangedStaged
|
2011-09-15 20:50:49 +00:00
|
|
|
withFilesUnlocked' :: (Git.Repo -> [FilePath] -> IO [FilePath]) -> (BackendFile -> CommandStart) -> CommandSeek
|
2010-11-28 18:19:43 +00:00
|
|
|
withFilesUnlocked' typechanged a params = do
|
|
|
|
-- unlocked files have changed type from a symlink to a regular file
|
2011-10-04 02:24:57 +00:00
|
|
|
repo <- gitRepo
|
2011-02-01 00:14:08 +00:00
|
|
|
typechangedfiles <- liftIO $ runPreserveOrder (typechanged repo) params
|
2010-12-11 21:14:54 +00:00
|
|
|
unlockedfiles <- liftIO $ filterM notSymlink $
|
2010-12-24 01:53:32 +00:00
|
|
|
map (\f -> Git.workTree repo ++ "/" ++ f) typechangedfiles
|
2011-09-19 02:40:31 +00:00
|
|
|
backendPairs a unlockedfiles
|
2011-09-15 20:50:49 +00:00
|
|
|
withKeys :: (Key -> CommandStart) -> CommandSeek
|
2011-07-15 07:12:05 +00:00
|
|
|
withKeys a params = return $ map (a . parse) params
|
2011-03-16 02:46:47 +00:00
|
|
|
where
|
2011-07-15 07:12:05 +00:00
|
|
|
parse p = fromMaybe (error "bad key") $ readKey p
|
2011-09-15 20:50:49 +00:00
|
|
|
withNothing :: CommandStart -> CommandSeek
|
2010-11-13 20:15:45 +00:00
|
|
|
withNothing a [] = return [a]
|
2011-05-29 00:01:45 +00:00
|
|
|
withNothing _ _ = error "This command takes no parameters."
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2011-09-19 02:40:31 +00:00
|
|
|
runFiltered :: (FilePath -> Annex (Maybe a)) -> Annex [FilePath] -> Annex [Annex (Maybe a)]
|
2011-09-21 03:24:48 +00:00
|
|
|
runFiltered a = runFilteredGen a id
|
2011-09-19 02:40:31 +00:00
|
|
|
|
2011-09-15 20:50:49 +00:00
|
|
|
backendPairs :: (BackendFile -> CommandStart) -> CommandSeek
|
2011-09-19 03:09:40 +00:00
|
|
|
backendPairs a fs = runFilteredGen a snd (Backend.chooseBackends fs)
|
|
|
|
|
2011-09-19 05:37:04 +00:00
|
|
|
runFilteredGen :: (b -> Annex (Maybe a)) -> (b -> FilePath) -> Annex [b] -> Annex [Annex (Maybe a)]
|
2011-09-19 03:09:40 +00:00
|
|
|
runFilteredGen a d fs = do
|
2011-09-19 02:40:31 +00:00
|
|
|
matcher <- Limit.getMatcher
|
2011-09-19 03:09:40 +00:00
|
|
|
liftM (map $ proc matcher) fs
|
2011-09-19 02:40:31 +00:00
|
|
|
where
|
2011-09-19 03:09:40 +00:00
|
|
|
proc matcher v = do
|
|
|
|
let f = d v
|
2011-09-19 02:40:31 +00:00
|
|
|
ok <- matcher f
|
2011-09-19 03:09:40 +00:00
|
|
|
if ok then a v else stop
|
2010-11-28 18:19:43 +00:00
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
{- filter out symlinks -}
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
2011-08-25 04:28:55 +00:00
|
|
|
notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
|
2010-12-30 18:19:16 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
{- Descriptions of params used in usage messages. -}
|
2011-09-15 18:33:37 +00:00
|
|
|
paramPaths :: String
|
|
|
|
paramPaths = paramOptional $ paramRepeating paramPath -- most often used
|
2010-12-30 18:19:16 +00:00
|
|
|
paramPath :: String
|
2010-12-30 19:06:26 +00:00
|
|
|
paramPath = "PATH"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramKey :: String
|
2010-12-30 19:06:26 +00:00
|
|
|
paramKey = "KEY"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramDesc :: String
|
2011-03-05 21:05:19 +00:00
|
|
|
paramDesc = "DESC"
|
2011-09-15 16:36:27 +00:00
|
|
|
paramUrl :: String
|
|
|
|
paramUrl = "URL"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramNumber :: String
|
2010-12-30 19:06:26 +00:00
|
|
|
paramNumber = "NUMBER"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramRemote :: String
|
2010-12-30 19:06:26 +00:00
|
|
|
paramRemote = "REMOTE"
|
|
|
|
paramGlob :: String
|
|
|
|
paramGlob = "GLOB"
|
|
|
|
paramName :: String
|
|
|
|
paramName = "NAME"
|
2011-03-29 03:22:31 +00:00
|
|
|
paramType :: String
|
|
|
|
paramType = "TYPE"
|
|
|
|
paramKeyValue :: String
|
|
|
|
paramKeyValue = "K=V"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramNothing :: String
|
|
|
|
paramNothing = ""
|
2011-09-15 18:33:37 +00:00
|
|
|
paramRepeating :: String -> String
|
|
|
|
paramRepeating s = s ++ " ..."
|
|
|
|
paramOptional :: String -> String
|
|
|
|
paramOptional s = "[" ++ s ++ "]"
|
|
|
|
paramPair :: String -> String -> String
|
|
|
|
paramPair a b = a ++ " " ++ b
|
2011-01-26 04:17:38 +00:00
|
|
|
|
2011-03-16 02:28:18 +00:00
|
|
|
{- The Key specified by the --key parameter. -}
|
2011-01-26 04:17:38 +00:00
|
|
|
cmdlineKey :: Annex Key
|
|
|
|
cmdlineKey = do
|
|
|
|
k <- Annex.getState Annex.defaultkey
|
2011-03-16 02:28:18 +00:00
|
|
|
case k of
|
|
|
|
Nothing -> nokey
|
|
|
|
Just "" -> nokey
|
2011-05-15 06:49:43 +00:00
|
|
|
Just kstring -> maybe badkey return $ readKey kstring
|
2011-01-26 04:17:38 +00:00
|
|
|
where
|
2011-03-16 02:28:18 +00:00
|
|
|
nokey = error "please specify the key with --key"
|
2011-05-15 06:49:43 +00:00
|
|
|
badkey = error "bad key"
|
2011-01-26 04:17:38 +00:00
|
|
|
|
2011-09-15 17:30:04 +00:00
|
|
|
{- Used for commands that have an auto mode that checks the number of known
|
|
|
|
- copies of a key.
|
|
|
|
-
|
|
|
|
- In auto mode, first checks that the number of known
|
|
|
|
- copies of the key is > or < than the numcopies setting, before running
|
|
|
|
- the action. -}
|
|
|
|
autoCopies :: Key -> (Int -> Int -> Bool) -> Maybe Int -> CommandStart -> CommandStart
|
|
|
|
autoCopies key vs numcopiesattr a = do
|
|
|
|
auto <- Annex.getState Annex.auto
|
|
|
|
if auto
|
|
|
|
then do
|
|
|
|
needed <- getNumCopies numcopiesattr
|
|
|
|
(_, have) <- trustPartition UnTrusted =<< keyLocations key
|
|
|
|
if length have `vs` needed then a else stop
|
|
|
|
else a
|