2011-09-15 16:57:02 -04:00
|
|
|
{- git-annex command infrastructure
|
2010-11-02 19:04:24 -04:00
|
|
|
-
|
2011-09-19 01:37:04 -04:00
|
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
2010-11-02 19:04:24 -04:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-10-29 23:48:46 -04:00
|
|
|
module Command (
|
|
|
|
command,
|
2011-11-16 00:49:09 -04:00
|
|
|
noRepo,
|
2012-09-15 20:46:38 -04:00
|
|
|
noCommit,
|
2013-07-30 20:24:27 -04:00
|
|
|
noMessages,
|
2012-01-05 22:48:59 -04:00
|
|
|
withOptions,
|
2011-10-29 23:48:46 -04:00
|
|
|
next,
|
|
|
|
stop,
|
2011-12-09 12:23:45 -04:00
|
|
|
stopUnless,
|
2011-10-29 23:48:46 -04:00
|
|
|
prepCommand,
|
|
|
|
doCommand,
|
2011-11-10 23:35:08 -04:00
|
|
|
whenAnnexed,
|
2011-12-07 16:53:53 -04:00
|
|
|
ifAnnexed,
|
2011-10-29 23:48:46 -04:00
|
|
|
isBareRepo,
|
2012-02-13 23:42:44 -04:00
|
|
|
numCopies,
|
2012-12-06 13:22:16 -04:00
|
|
|
numCopiesCheck,
|
2012-10-08 17:14:01 -04:00
|
|
|
checkAuto,
|
2011-11-11 01:52:58 -04:00
|
|
|
module ReExported
|
2011-10-29 23:48:46 -04:00
|
|
|
) where
|
2010-11-02 19:04:24 -04:00
|
|
|
|
2011-10-05 16:02:51 -04:00
|
|
|
import Common.Annex
|
2010-11-04 13:28:49 -04:00
|
|
|
import qualified Backend
|
|
|
|
import qualified Annex
|
2011-06-30 13:16:57 -04:00
|
|
|
import qualified Git
|
2012-01-10 13:11:16 -04:00
|
|
|
import qualified Remote
|
2011-11-11 01:52:58 -04:00
|
|
|
import Types.Command as ReExported
|
2012-01-05 22:48:59 -04:00
|
|
|
import Types.Option as ReExported
|
2011-11-11 01:52:58 -04:00
|
|
|
import Seek as ReExported
|
|
|
|
import Checks as ReExported
|
2012-01-05 22:48:59 -04:00
|
|
|
import Usage as ReExported
|
2011-10-15 16:21:08 -04:00
|
|
|
import Logs.Trust
|
2011-09-15 13:30:04 -04:00
|
|
|
import Config
|
2012-02-13 23:42:44 -04:00
|
|
|
import Annex.CheckAttr
|
2010-11-02 19:04:24 -04:00
|
|
|
|
2011-11-16 00:49:09 -04:00
|
|
|
{- Generates a normal command -}
|
2013-03-24 18:28:21 -04:00
|
|
|
command :: String -> String -> [CommandSeek] -> CommandSection -> String -> Command
|
2013-07-30 20:24:27 -04:00
|
|
|
command = Command [] Nothing commonChecks False False
|
2012-02-14 12:40:40 -04:00
|
|
|
|
2012-09-15 20:46:38 -04:00
|
|
|
{- Indicates that a command doesn't need to commit any changes to
|
|
|
|
- the git-annex branch. -}
|
|
|
|
noCommit :: Command -> Command
|
|
|
|
noCommit c = c { cmdnocommit = True }
|
2011-11-16 00:49:09 -04:00
|
|
|
|
2013-07-30 20:24:27 -04:00
|
|
|
{- Indicates that a command should not output anything other than what
|
|
|
|
- it directly sends to stdout. (--json can override this). -}
|
|
|
|
noMessages :: Command -> Command
|
|
|
|
noMessages c = c { cmdnomessages = True }
|
|
|
|
|
2011-11-16 00:49:09 -04:00
|
|
|
{- Adds a fallback action to a command, that will be run if it's used
|
|
|
|
- outside a git repository. -}
|
|
|
|
noRepo :: IO () -> Command -> Command
|
|
|
|
noRepo a c = c { cmdnorepo = Just a }
|
2010-11-04 13:28:49 -04:00
|
|
|
|
2012-01-05 22:48:59 -04:00
|
|
|
{- Adds options to a command. -}
|
|
|
|
withOptions :: [Option] -> Command -> Command
|
|
|
|
withOptions o c = c { cmdoptions = o }
|
|
|
|
|
2011-05-15 02:02:46 -04: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
|
|
|
|
|
2011-12-09 12:23:45 -04:00
|
|
|
{- Stops unless a condition is met. -}
|
|
|
|
stopUnless :: Annex Bool -> Annex (Maybe a) -> Annex (Maybe a)
|
2012-03-14 17:43:34 -04:00
|
|
|
stopUnless c a = ifM c ( a , stop )
|
2011-12-09 12:23:45 -04:00
|
|
|
|
2011-10-30 20:04:15 -04:00
|
|
|
{- Prepares to run a command via the check and seek stages, returning a
|
|
|
|
- list of actions to perform to run the command. -}
|
|
|
|
prepCommand :: Command -> [String] -> Annex [CommandCleanup]
|
|
|
|
prepCommand Command { cmdseek = seek, cmdcheck = c } params = do
|
2011-11-11 01:52:58 -04:00
|
|
|
mapM_ runCheck c
|
2011-10-30 20:04:15 -04:00
|
|
|
map doCommand . concat <$> mapM (\s -> s params) seek
|
2010-11-04 13:28:49 -04:00
|
|
|
|
2010-12-30 14:19:16 -04:00
|
|
|
{- Runs a command through the start, perform and cleanup stages -}
|
|
|
|
doCommand :: CommandStart -> CommandCleanup
|
2011-05-15 12:25:58 -04:00
|
|
|
doCommand = start
|
2012-11-11 00:51:07 -04:00
|
|
|
where
|
|
|
|
start = stage $ maybe skip perform
|
|
|
|
perform = stage $ maybe failure cleanup
|
|
|
|
cleanup = stage $ status
|
|
|
|
stage = (=<<)
|
|
|
|
skip = return True
|
|
|
|
failure = showEndFail >> return False
|
|
|
|
status r = showEndResult r >> return r
|
2010-11-04 13:28:49 -04:00
|
|
|
|
2011-11-10 23:35:08 -04:00
|
|
|
{- Modifies an action to only act on files that are already annexed,
|
|
|
|
- and passes the key and backend on to it. -}
|
2011-12-31 04:11:39 -04:00
|
|
|
whenAnnexed :: (FilePath -> (Key, Backend) -> Annex (Maybe a)) -> FilePath -> Annex (Maybe a)
|
2011-12-07 16:53:53 -04:00
|
|
|
whenAnnexed a file = ifAnnexed file (a file) (return Nothing)
|
2011-11-10 23:35:08 -04:00
|
|
|
|
2011-12-31 04:11:39 -04:00
|
|
|
ifAnnexed :: FilePath -> ((Key, Backend) -> Annex a) -> Annex a -> Annex a
|
2011-12-07 16:53:53 -04:00
|
|
|
ifAnnexed file yes no = maybe no yes =<< Backend.lookupFile file
|
2011-10-29 17:49:37 -04:00
|
|
|
|
2011-10-29 23:48:46 -04:00
|
|
|
isBareRepo :: Annex Bool
|
2011-11-08 15:34:10 -04:00
|
|
|
isBareRepo = fromRepo Git.repoIsLocalBare
|
2011-01-26 00:17:38 -04:00
|
|
|
|
2012-02-13 23:42:44 -04:00
|
|
|
numCopies :: FilePath -> Annex (Maybe Int)
|
2013-07-09 12:05:56 -04:00
|
|
|
numCopies file = do
|
|
|
|
forced <- Annex.getState Annex.forcenumcopies
|
|
|
|
case forced of
|
|
|
|
Just n -> return $ Just n
|
|
|
|
Nothing -> readish <$> checkAttr "annex.numcopies" file
|
2012-02-13 23:42:44 -04:00
|
|
|
|
2012-12-06 13:22:16 -04:00
|
|
|
numCopiesCheck :: FilePath -> Key -> (Int -> Int -> Bool) -> Annex Bool
|
|
|
|
numCopiesCheck file key vs = do
|
|
|
|
numcopiesattr <- numCopies file
|
|
|
|
needed <- getNumCopies numcopiesattr
|
|
|
|
have <- trustExclude UnTrusted =<< Remote.keyLocations key
|
|
|
|
return $ length have `vs` needed
|
|
|
|
|
2012-10-08 17:14:01 -04:00
|
|
|
checkAuto :: Annex Bool -> Annex Bool
|
|
|
|
checkAuto checker = ifM (Annex.getState Annex.auto)
|
|
|
|
( checker , return True )
|