2010-11-11 22:54:52 +00:00
|
|
|
{- git-annex commands
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command where
|
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
|
|
|
import System.Directory
|
|
|
|
import System.Posix.Files
|
|
|
|
import Control.Monad (filterM)
|
2010-12-08 18:07:49 +00:00
|
|
|
import System.Path.WildMatch
|
2010-12-08 18:48:10 +00:00
|
|
|
import Text.Regex.PCRE.Light.Char8
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
import Types
|
2010-11-04 17:28:49 +00:00
|
|
|
import qualified Backend
|
2010-11-08 19:15:21 +00:00
|
|
|
import Messages
|
2010-11-04 17:28:49 +00:00
|
|
|
import qualified Annex
|
2010-11-11 22:54:52 +00:00
|
|
|
import qualified GitRepo as Git
|
|
|
|
import Locations
|
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
|
|
|
|
{- Some helper functions are used to build up CommandSeek and CommandStart
|
2010-11-02 23:04:24 +00:00
|
|
|
- functions. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandSeekStrings = CommandStartString -> CommandSeek
|
|
|
|
type CommandStartString = String -> CommandStart
|
2010-11-28 18:19:43 +00:00
|
|
|
type BackendFile = (FilePath, Maybe Backend)
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandSeekBackendFiles = CommandStartBackendFile -> CommandSeek
|
|
|
|
type CommandStartBackendFile = BackendFile -> CommandStart
|
2010-11-28 19:28:20 +00:00
|
|
|
type AttrFile = (FilePath, String)
|
2010-12-30 18:19:16 +00:00
|
|
|
type CommandSeekAttrFiles = CommandStartAttrFile -> CommandSeek
|
|
|
|
type CommandStartAttrFile = AttrFile -> CommandStart
|
|
|
|
type CommandSeekNothing = CommandStart -> CommandSeek
|
|
|
|
type CommandStartNothing = CommandStart
|
|
|
|
|
|
|
|
data Command = Command {
|
|
|
|
cmdname :: String,
|
|
|
|
cmdparams :: String,
|
|
|
|
cmdseek :: [CommandSeek],
|
|
|
|
cmddesc :: String
|
2010-11-04 17:28:49 +00:00
|
|
|
}
|
|
|
|
|
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. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
prepCmd :: Command -> [String] -> Annex [Annex Bool]
|
|
|
|
prepCmd Command { cmdseek = seek } params = do
|
2010-12-08 18:07:49 +00:00
|
|
|
lists <- mapM (\s -> s params) seek
|
2010-12-30 18:19:16 +00:00
|
|
|
return $ map doCommand $ foldl (++) [] lists
|
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
|
|
|
|
doCommand start = do
|
2010-11-04 17:28:49 +00:00
|
|
|
s <- start
|
2010-11-22 19:46:57 +00:00
|
|
|
case s of
|
2010-11-04 17:28:49 +00:00
|
|
|
Nothing -> return True
|
|
|
|
Just perform -> do
|
|
|
|
p <- perform
|
2010-11-22 19:46:57 +00:00
|
|
|
case p of
|
2010-11-04 17:28:49 +00:00
|
|
|
Nothing -> do
|
|
|
|
showEndFail
|
|
|
|
return False
|
|
|
|
Just cleanup -> do
|
|
|
|
c <- cleanup
|
2010-11-22 19:46:57 +00:00
|
|
|
if c
|
2010-11-04 17:28:49 +00:00
|
|
|
then do
|
|
|
|
showEndOk
|
|
|
|
return True
|
|
|
|
else do
|
|
|
|
showEndFail
|
|
|
|
return False
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a)
|
|
|
|
notAnnexed file a = do
|
|
|
|
r <- Backend.lookupFile file
|
2010-11-22 19:46:57 +00:00
|
|
|
case r of
|
2010-11-02 23:04:24 +00:00
|
|
|
Just _ -> return Nothing
|
|
|
|
Nothing -> a
|
|
|
|
|
|
|
|
isAnnexed :: FilePath -> ((Key, Backend) -> Annex (Maybe a)) -> Annex (Maybe a)
|
|
|
|
isAnnexed file a = do
|
|
|
|
r <- Backend.lookupFile file
|
2010-11-22 19:46:57 +00:00
|
|
|
case r of
|
2010-11-02 23:04:24 +00:00
|
|
|
Just v -> a v
|
|
|
|
Nothing -> return Nothing
|
2010-11-11 22:54:52 +00:00
|
|
|
|
|
|
|
{- These functions find appropriate files or other things based on a
|
|
|
|
user's parameters, and run a specified action on them. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesInGit :: CommandSeekStrings
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesInGit a params = do
|
|
|
|
repo <- Annex.gitRepo
|
2010-12-24 01:53:32 +00:00
|
|
|
files <- liftIO $ Git.inRepo repo params
|
|
|
|
files' <- filterFiles files
|
2010-12-08 18:07:49 +00:00
|
|
|
return $ map a files'
|
2010-12-30 18:19:16 +00:00
|
|
|
withAttrFilesInGit :: String -> CommandSeekAttrFiles
|
2010-11-28 19:28:20 +00:00
|
|
|
withAttrFilesInGit attr a params = do
|
|
|
|
repo <- Annex.gitRepo
|
2010-12-24 01:53:32 +00:00
|
|
|
files <- liftIO $ Git.inRepo repo params
|
|
|
|
files' <- filterFiles files
|
2010-12-08 18:07:49 +00:00
|
|
|
pairs <- liftIO $ Git.checkAttr repo attr files'
|
2010-11-28 19:28:20 +00:00
|
|
|
return $ map a pairs
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesMissing :: CommandSeekStrings
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesMissing a params = do
|
|
|
|
files <- liftIO $ filterM missing params
|
2010-12-08 18:07:49 +00:00
|
|
|
files' <- filterFiles files
|
|
|
|
return $ map a files'
|
2010-11-11 22:54:52 +00:00
|
|
|
where
|
|
|
|
missing f = do
|
|
|
|
e <- doesFileExist f
|
|
|
|
return $ not e
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesNotInGit :: CommandSeekBackendFiles
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesNotInGit a params = do
|
|
|
|
repo <- Annex.gitRepo
|
2010-12-24 01:53:32 +00:00
|
|
|
newfiles <- liftIO $ Git.notInRepo repo params
|
|
|
|
newfiles' <- filterFiles newfiles
|
2010-12-08 18:07:49 +00:00
|
|
|
backendPairs a newfiles'
|
2010-12-30 18:19:16 +00:00
|
|
|
withString :: CommandSeekStrings
|
2010-11-13 20:03:25 +00:00
|
|
|
withString a params = return [a $ unwords params]
|
2010-12-30 18:19:16 +00:00
|
|
|
withStrings :: CommandSeekStrings
|
2010-11-15 22:04:19 +00:00
|
|
|
withStrings a params = return $ map a params
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesToBeCommitted :: CommandSeekStrings
|
2010-11-11 22:54:52 +00:00
|
|
|
withFilesToBeCommitted a params = do
|
|
|
|
repo <- Annex.gitRepo
|
2010-12-24 01:53:32 +00:00
|
|
|
tocommit <- liftIO $ Git.stagedFiles repo params
|
|
|
|
tocommit' <- filterFiles tocommit
|
2010-12-08 18:07:49 +00:00
|
|
|
return $ map a tocommit'
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesUnlocked :: CommandSeekBackendFiles
|
2010-11-28 18:19:43 +00:00
|
|
|
withFilesUnlocked = withFilesUnlocked' Git.typeChangedFiles
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesUnlockedToBeCommitted :: CommandSeekBackendFiles
|
2010-11-28 18:19:43 +00:00
|
|
|
withFilesUnlockedToBeCommitted = withFilesUnlocked' Git.typeChangedStagedFiles
|
2010-12-30 18:19:16 +00:00
|
|
|
withFilesUnlocked' :: (Git.Repo -> [FilePath] -> IO [FilePath]) -> CommandSeekBackendFiles
|
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
|
2010-11-11 22:54:52 +00:00
|
|
|
repo <- Annex.gitRepo
|
2010-12-24 01:53:32 +00:00
|
|
|
typechangedfiles <- liftIO $ 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
|
2010-12-08 18:07:49 +00:00
|
|
|
unlockedfiles' <- filterFiles unlockedfiles
|
|
|
|
backendPairs a unlockedfiles'
|
2010-12-30 18:19:16 +00:00
|
|
|
withKeys :: CommandSeekStrings
|
2010-11-11 22:54:52 +00:00
|
|
|
withKeys a params = return $ map a params
|
2010-12-30 18:19:16 +00:00
|
|
|
withTempFile :: CommandSeekStrings
|
2010-11-11 22:54:52 +00:00
|
|
|
withTempFile a params = return $ map a params
|
2010-12-30 18:19:16 +00:00
|
|
|
withNothing :: CommandSeekNothing
|
2010-11-13 20:15:45 +00:00
|
|
|
withNothing a [] = return [a]
|
|
|
|
withNothing _ _ = return []
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
backendPairs :: CommandSeekBackendFiles
|
2010-11-28 18:19:43 +00:00
|
|
|
backendPairs a files = do
|
|
|
|
pairs <- Backend.chooseBackends files
|
|
|
|
return $ map a pairs
|
|
|
|
|
2010-11-13 18:59:27 +00:00
|
|
|
{- Default to acting on all files matching the seek action if
|
|
|
|
- none are specified. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
withAll :: (a -> CommandSeek) -> a -> CommandSeek
|
2010-11-22 19:46:57 +00:00
|
|
|
withAll w a [] = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
w a [Git.workTree g]
|
|
|
|
withAll w a p = w a p
|
2010-11-13 18:59:27 +00:00
|
|
|
|
2010-11-14 18:58:42 +00:00
|
|
|
{- Provides a default parameter to act on if none is specified. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
withDefault :: String-> (a -> CommandSeek) -> (a -> CommandSeek)
|
2010-11-22 19:46:57 +00:00
|
|
|
withDefault d w a [] = w a [d]
|
|
|
|
withDefault _ w a p = w a p
|
2010-11-14 16:35:05 +00:00
|
|
|
|
2010-12-08 18:07:49 +00:00
|
|
|
{- Filter out files from the state directory, and those matching the
|
|
|
|
- exclude glob pattern, if it was specified. -}
|
|
|
|
filterFiles :: [FilePath] -> Annex [FilePath]
|
|
|
|
filterFiles l = do
|
|
|
|
let l' = filter notState l
|
|
|
|
exclude <- Annex.flagGet "exclude"
|
|
|
|
if null exclude
|
|
|
|
then return l'
|
|
|
|
else do
|
2010-12-08 18:48:10 +00:00
|
|
|
let regexp = compile ("^" ++ wildToRegex exclude) []
|
2010-12-08 18:07:49 +00:00
|
|
|
return $ filter (notExcluded regexp) l'
|
|
|
|
where
|
|
|
|
notState f = stateLoc /= take stateLocLen f
|
|
|
|
stateLocLen = length stateLoc
|
2010-12-08 18:48:10 +00:00
|
|
|
notExcluded r f = case match r f [] of
|
2010-12-08 18:07:49 +00:00
|
|
|
Nothing -> True
|
|
|
|
Just _ -> False
|
2010-11-28 19:28:20 +00:00
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
{- filter out symlinks -}
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
|
|
|
notSymlink f = do
|
|
|
|
s <- liftIO $ getSymbolicLinkStatus f
|
|
|
|
return $ not $ isSymbolicLink s
|
2010-12-30 18:19:16 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
{- Descriptions of params used in usage messages. -}
|
|
|
|
paramRepeating :: String -> String
|
|
|
|
paramRepeating s = s ++ " ..."
|
|
|
|
paramOptional :: String -> String
|
|
|
|
paramOptional s = "[" ++ s ++ "]"
|
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
|
|
|
|
paramDesc = "DESCRIPTION"
|
|
|
|
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"
|
2010-12-30 18:19:16 +00:00
|
|
|
paramNothing :: String
|
|
|
|
paramNothing = ""
|