2011-01-16 20:05:05 +00:00
|
|
|
|
{- git-annex command line parsing and dispatch
|
2010-11-02 23:04:24 +00:00
|
|
|
|
-
|
2012-04-12 19:34:41 +00:00
|
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2010-11-02 23:04:24 +00:00
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
2013-05-10 21:29:59 +00:00
|
|
|
|
{-# LANGUAGE CPP #-}
|
2014-08-19 16:55:01 +00:00
|
|
|
|
{-# LANGUAGE BangPatterns #-}
|
2013-05-10 21:29:59 +00:00
|
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
|
module CmdLine (
|
2010-12-30 20:52:24 +00:00
|
|
|
|
dispatch,
|
2010-12-31 00:08:22 +00:00
|
|
|
|
usage,
|
2011-01-16 20:05:05 +00:00
|
|
|
|
shutdown
|
2010-12-30 19:06:26 +00:00
|
|
|
|
) where
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2011-11-16 04:49:09 +00:00
|
|
|
|
import qualified Control.Exception as E
|
2012-02-25 22:02:49 +00:00
|
|
|
|
import qualified Data.Map as M
|
2011-11-16 04:49:09 +00:00
|
|
|
|
import Control.Exception (throw)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import System.Console.GetOpt
|
2013-08-04 17:07:55 +00:00
|
|
|
|
#ifndef mingw32_HOST_OS
|
2012-10-02 03:01:29 +00:00
|
|
|
|
import System.Posix.Signals
|
2013-05-10 21:29:59 +00:00
|
|
|
|
#endif
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
|
import Common.Annex
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import qualified Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
|
import qualified Git
|
2012-04-12 19:34:41 +00:00
|
|
|
|
import qualified Git.AutoCorrect
|
2014-08-23 23:51:33 +00:00
|
|
|
|
import qualified Git.Config
|
2011-10-04 04:40:47 +00:00
|
|
|
|
import Annex.Content
|
2013-04-22 19:36:34 +00:00
|
|
|
|
import Annex.Environment
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import Command
|
2013-07-31 00:24:27 +00:00
|
|
|
|
import Types.Messages
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2010-12-30 20:52:24 +00:00
|
|
|
|
{- Runs the passed command line. -}
|
2013-11-30 19:18:40 +00:00
|
|
|
|
dispatch :: Bool -> CmdParams -> [Command] -> [Option] -> [(String, String)] -> String -> IO Git.Repo -> IO ()
|
2012-07-02 04:53:00 +00:00
|
|
|
|
dispatch fuzzyok allargs allcmds commonoptions fields header getgitrepo = do
|
2011-03-12 19:30:17 +00:00
|
|
|
|
setupConsole
|
2014-08-19 16:55:01 +00:00
|
|
|
|
case getOptCmd args cmd commonoptions of
|
|
|
|
|
Right (flags, params) -> go flags params
|
|
|
|
|
=<< (E.try getgitrepo :: IO (Either E.SomeException Git.Repo))
|
|
|
|
|
Left parseerr -> error parseerr
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
2014-08-19 16:55:01 +00:00
|
|
|
|
go flags params (Right g) = do
|
|
|
|
|
state <- Annex.new g
|
|
|
|
|
Annex.eval state $ do
|
|
|
|
|
checkEnvironment
|
2014-08-23 23:51:33 +00:00
|
|
|
|
when fuzzy $
|
|
|
|
|
inRepo $ autocorrect . Just
|
2014-08-19 16:55:01 +00:00
|
|
|
|
forM_ fields $ uncurry Annex.setField
|
|
|
|
|
when (cmdnomessages cmd) $
|
|
|
|
|
Annex.setOutput QuietOutput
|
|
|
|
|
sequence_ flags
|
|
|
|
|
whenM (annexDebug <$> Annex.getGitConfig) $
|
|
|
|
|
liftIO enableDebugOutput
|
|
|
|
|
startup
|
|
|
|
|
performCommandAction cmd params
|
|
|
|
|
shutdown $ cmdnocommit cmd
|
2014-08-23 23:51:33 +00:00
|
|
|
|
go _flags params (Left e) = do
|
|
|
|
|
when fuzzy $
|
|
|
|
|
autocorrect =<< Git.Config.global
|
2014-08-19 16:55:01 +00:00
|
|
|
|
maybe (throw e) (\a -> a params) (cmdnorepo cmd)
|
2013-03-25 16:41:57 +00:00
|
|
|
|
err msg = msg ++ "\n\n" ++ usage header allcmds
|
2012-11-11 04:51:07 +00:00
|
|
|
|
cmd = Prelude.head cmds
|
|
|
|
|
(fuzzy, cmds, name, args) = findCmd fuzzyok allargs allcmds err
|
2014-08-23 23:51:33 +00:00
|
|
|
|
autocorrect = Git.AutoCorrect.prepare name cmdname cmds
|
2010-12-30 19:44:15 +00:00
|
|
|
|
|
2012-04-12 19:34:41 +00:00
|
|
|
|
{- Parses command line params far enough to find the Command to run, and
|
|
|
|
|
- returns the remaining params.
|
|
|
|
|
- Does fuzzy matching if necessary, which may result in multiple Commands. -}
|
2013-11-30 19:18:40 +00:00
|
|
|
|
findCmd :: Bool -> CmdParams -> [Command] -> (String -> String) -> (Bool, [Command], String, CmdParams)
|
2012-04-12 19:34:41 +00:00
|
|
|
|
findCmd fuzzyok argv cmds err
|
|
|
|
|
| isNothing name = error $ err "missing command"
|
2012-04-14 20:01:22 +00:00
|
|
|
|
| not (null exactcmds) = (False, exactcmds, fromJust name, args)
|
|
|
|
|
| fuzzyok && not (null inexactcmds) = (True, inexactcmds, fromJust name, args)
|
2012-04-12 19:34:41 +00:00
|
|
|
|
| otherwise = error $ err $ "unknown command " ++ fromJust name
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
(name, args) = findname argv []
|
|
|
|
|
findname [] c = (Nothing, reverse c)
|
|
|
|
|
findname (a:as) c
|
|
|
|
|
| "-" `isPrefixOf` a = findname as (a:c)
|
|
|
|
|
| otherwise = (Just a, reverse c ++ as)
|
|
|
|
|
exactcmds = filter (\c -> name == Just (cmdname c)) cmds
|
|
|
|
|
inexactcmds = case name of
|
|
|
|
|
Nothing -> []
|
|
|
|
|
Just n -> Git.AutoCorrect.fuzzymatches n cmdname cmds
|
2012-04-12 19:34:41 +00:00
|
|
|
|
|
|
|
|
|
{- Parses command line options, and returns actions to run to configure flags
|
|
|
|
|
- and the remaining parameters for the command. -}
|
2014-08-19 16:55:01 +00:00
|
|
|
|
getOptCmd :: CmdParams -> Command -> [Option] -> Either String ([Annex ()], CmdParams)
|
2013-03-27 17:51:24 +00:00
|
|
|
|
getOptCmd argv cmd commonoptions = check $
|
2012-04-12 19:34:41 +00:00
|
|
|
|
getOpt Permute (commonoptions ++ cmdoptions cmd) argv
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
2014-08-19 16:55:01 +00:00
|
|
|
|
check (flags, rest, []) = Right (flags, rest)
|
|
|
|
|
check (_, _, errs) = Left $ unlines
|
2013-03-27 17:51:24 +00:00
|
|
|
|
[ concat errs
|
|
|
|
|
, commandUsage cmd
|
|
|
|
|
]
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
|
|
{- Actions to perform each time ran. -}
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
startup :: Annex ()
|
|
|
|
|
startup =
|
2013-08-04 17:07:55 +00:00
|
|
|
|
#ifndef mingw32_HOST_OS
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
liftIO $ void $ installHandler sigINT Default Nothing
|
|
|
|
|
#else
|
|
|
|
|
return ()
|
2013-05-11 20:03:00 +00:00
|
|
|
|
#endif
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
|
|
{- Cleanup actions. -}
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
|
shutdown :: Bool -> Annex ()
|
2012-09-16 00:46:38 +00:00
|
|
|
|
shutdown nocommit = do
|
|
|
|
|
saveState nocommit
|
2012-02-25 22:02:49 +00:00
|
|
|
|
sequence_ =<< M.elems <$> Annex.getState Annex.cleanup
|
2012-10-17 04:39:45 +00:00
|
|
|
|
liftIO reapZombies -- zombies from long-running git processes
|