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
|
2011-03-03 20:22:53 +00:00
|
|
|
import Control.Monad (filterM, liftM, when)
|
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
|
2011-01-27 20:31:29 +00:00
|
|
|
import Data.List
|
2011-05-15 06:49:43 +00:00
|
|
|
import Data.Maybe
|
2011-05-28 20:09:11 +00:00
|
|
|
import Data.String.Utils
|
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
|
2011-02-01 00:14:08 +00:00
|
|
|
import Utility
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
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
|
2011-05-16 16:25:54 +00:00
|
|
|
type CommandSeekWords = CommandStartWords -> CommandSeek
|
|
|
|
type CommandStartWords = [String] -> CommandStart
|
2011-03-16 02:42:34 +00:00
|
|
|
type CommandSeekKeys = CommandStartKey -> CommandSeek
|
|
|
|
type CommandStartKey = Key -> CommandStart
|
2011-01-26 01:02:34 +00:00
|
|
|
type BackendFile = (FilePath, Maybe (Backend Annex))
|
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 {
|
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]
|
|
|
|
prepCommand Command { cmdseek = seek } params = do
|
2010-12-08 18:07:49 +00:00
|
|
|
lists <- mapM (\s -> s params) seek
|
2011-01-09 22:23:58 +00:00
|
|
|
return $ map doCommand $ concat 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
|
2011-05-15 16:25:58 +00:00
|
|
|
doCommand = start
|
|
|
|
where
|
|
|
|
start = stage $ maybe (return True) perform
|
|
|
|
perform = stage $ maybe (showEndFail >> return False) cleanup
|
|
|
|
cleanup = stage $ \r -> showEndResult r >> return r
|
|
|
|
stage a b = b >>= a
|
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
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
when (Git.repoIsLocalBare g) $ do
|
|
|
|
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
|
|
|
|
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
|
2011-02-01 00:14:08 +00:00
|
|
|
files <- liftIO $ runPreserveOrder (Git.inRepo repo) params
|
2011-02-19 21:00:40 +00:00
|
|
|
liftM (map a) $ filterFiles 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
|
2011-02-01 00:14:08 +00:00
|
|
|
files <- liftIO $ runPreserveOrder (Git.inRepo repo) params
|
2011-03-23 00:31:22 +00:00
|
|
|
liftM (map a) $ liftIO $ Git.checkAttr repo attr files
|
2011-01-08 19:54:14 +00:00
|
|
|
withBackendFilesInGit :: CommandSeekBackendFiles
|
|
|
|
withBackendFilesInGit a params = do
|
|
|
|
repo <- Annex.gitRepo
|
2011-02-01 00:14:08 +00:00
|
|
|
files <- liftIO $ runPreserveOrder (Git.inRepo repo) params
|
2011-01-08 19:54:14 +00:00
|
|
|
files' <- filterFiles files
|
|
|
|
backendPairs a files'
|
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
|
2011-02-19 21:00:40 +00:00
|
|
|
liftM (map a) $ filterFiles 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
|
2011-02-01 00:14:08 +00:00
|
|
|
newfiles <- liftIO $ runPreserveOrder (Git.notInRepo repo) params
|
2010-12-24 01:53:32 +00:00
|
|
|
newfiles' <- filterFiles newfiles
|
2010-12-08 18:07:49 +00:00
|
|
|
backendPairs a newfiles'
|
2011-05-16 16:25:54 +00:00
|
|
|
withWords :: CommandSeekWords
|
|
|
|
withWords a params = return [a 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
|
2011-02-02 01:58:47 +00:00
|
|
|
tocommit <- liftIO $ runPreserveOrder (Git.stagedFilesNotDeleted repo) params
|
2011-02-19 21:00:40 +00:00
|
|
|
liftM (map a) $ filterFiles 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
|
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
|
2010-12-08 18:07:49 +00:00
|
|
|
unlockedfiles' <- filterFiles unlockedfiles
|
|
|
|
backendPairs a unlockedfiles'
|
2011-03-16 02:42:34 +00:00
|
|
|
withKeys :: CommandSeekKeys
|
2011-03-16 02:46:47 +00:00
|
|
|
withKeys a params = return $ map a $ map parse params
|
|
|
|
where
|
2011-05-15 06:49:43 +00:00
|
|
|
parse p = maybe (error "bad key") id $ readKey p
|
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]
|
2011-05-29 00:01:45 +00:00
|
|
|
withNothing _ _ = error "This command takes no parameters."
|
2010-11-11 22:54:52 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
backendPairs :: CommandSeekBackendFiles
|
2011-02-19 21:00:40 +00:00
|
|
|
backendPairs a files = liftM (map a) $ Backend.chooseBackends files
|
2010-11-28 18:19:43 +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
|
2011-01-26 04:17:38 +00:00
|
|
|
exclude <- Annex.getState Annex.exclude
|
2010-12-08 18:07:49 +00:00
|
|
|
if null exclude
|
|
|
|
then return l'
|
2011-01-30 04:08:17 +00:00
|
|
|
else return $ filter (notExcluded $ wildsRegex exclude) l'
|
2010-12-08 18:07:49 +00:00
|
|
|
where
|
2011-01-27 21:58:30 +00:00
|
|
|
notState f = not $ stateDir `isPrefixOf` f
|
2011-05-28 00:20:20 +00:00
|
|
|
notExcluded r f = isNothing $ match r f []
|
2011-01-30 03:47:10 +00:00
|
|
|
|
2011-01-30 04:08:17 +00:00
|
|
|
wildsRegex :: [String] -> Regex
|
|
|
|
wildsRegex ws = compile regex []
|
2011-05-28 20:09:11 +00:00
|
|
|
where
|
|
|
|
regex = "^(" ++ alternatives ++ ")"
|
|
|
|
alternatives = join "|" $ map wildToRegex ws
|
2010-11-28 19:28:20 +00:00
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
{- filter out symlinks -}
|
|
|
|
notSymlink :: FilePath -> IO Bool
|
2011-02-19 21:00:40 +00:00
|
|
|
notSymlink f = liftM (not . isSymbolicLink) $ liftIO $ 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. -}
|
|
|
|
paramRepeating :: String -> String
|
|
|
|
paramRepeating s = s ++ " ..."
|
|
|
|
paramOptional :: String -> String
|
|
|
|
paramOptional s = "[" ++ s ++ "]"
|
2011-03-03 21:21:00 +00:00
|
|
|
paramPair :: String -> String -> String
|
|
|
|
paramPair a b = a ++ " " ++ b
|
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"
|
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-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-02-01 00:06:34 +00:00
|
|
|
{- Given an original list of files, and an expanded list derived from it,
|
|
|
|
- ensures that the original list's ordering is preserved.
|
|
|
|
-
|
|
|
|
- The input list may contain a directory, like "dir" or "dir/". Any
|
|
|
|
- items in the expanded list that are contained in that directory will
|
|
|
|
- appear at the same position as it did in the input list.
|
|
|
|
-}
|
|
|
|
preserveOrder :: [FilePath] -> [FilePath] -> [FilePath]
|
|
|
|
-- optimisation, only one item in original list, so no reordering needed
|
|
|
|
preserveOrder [_] new = new
|
|
|
|
preserveOrder orig new = collect orig new
|
|
|
|
where
|
|
|
|
collect [] n = n
|
|
|
|
collect [_] n = n -- optimisation
|
|
|
|
collect (l:ls) n = found ++ collect ls rest
|
|
|
|
where (found, rest)=partition (l `dirContains`) n
|
|
|
|
|
2011-02-01 00:14:08 +00:00
|
|
|
{- Runs an action that takes a list of FilePaths, and ensures that
|
|
|
|
- its return list preserves order.
|
|
|
|
-
|
|
|
|
- This assumes that it's cheaper to call preserveOrder on the result,
|
2011-02-19 21:00:40 +00:00
|
|
|
- than it would be to run the action separately with each param. In the case
|
2011-02-01 00:14:08 +00:00
|
|
|
- of git file list commands, that assumption tends to hold.
|
|
|
|
-}
|
|
|
|
runPreserveOrder :: ([FilePath] -> IO [FilePath]) -> [FilePath] -> IO [FilePath]
|
2011-02-19 21:00:40 +00:00
|
|
|
runPreserveOrder a files = liftM (preserveOrder files) (a files)
|