2011-01-16 20:05:05 +00:00
|
|
|
|
{- git-annex command line parsing and dispatch
|
2010-11-02 23:04:24 +00:00
|
|
|
|
-
|
2015-07-08 16:33:27 +00:00
|
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
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 #-}
|
|
|
|
|
|
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
|
|
|
|
|
2015-07-08 23:38:56 +00:00
|
|
|
|
import qualified Options.Applicative as O
|
2015-07-09 15:49:52 +00:00
|
|
|
|
import qualified Options.Applicative.Help as H
|
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)
|
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. -}
|
2015-07-10 17:18:46 +00:00
|
|
|
|
dispatch :: Bool -> CmdParams -> [Command] -> [GlobalOption] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO ()
|
2015-07-10 04:55:53 +00:00
|
|
|
|
dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progdesc = do
|
2011-03-12 19:30:17 +00:00
|
|
|
|
setupConsole
|
2015-07-08 16:33:27 +00:00
|
|
|
|
go =<< (E.try getgitrepo :: IO (Either E.SomeException Git.Repo))
|
|
|
|
|
where
|
|
|
|
|
go (Right g) = do
|
|
|
|
|
state <- Annex.new g
|
|
|
|
|
Annex.eval state $ do
|
|
|
|
|
checkEnvironment
|
|
|
|
|
forM_ fields $ uncurry Annex.setField
|
2015-07-10 06:18:08 +00:00
|
|
|
|
(cmd, seek, globalconfig) <- parsewith cmdparser
|
2015-07-08 23:38:56 +00:00
|
|
|
|
(\a -> inRepo $ a . Just)
|
2015-07-08 16:33:27 +00:00
|
|
|
|
when (cmdnomessages cmd) $
|
|
|
|
|
Annex.setOutput QuietOutput
|
2015-07-10 06:03:03 +00:00
|
|
|
|
getParsed globalconfig
|
2015-07-08 16:33:27 +00:00
|
|
|
|
whenM (annexDebug <$> Annex.getGitConfig) $
|
|
|
|
|
liftIO enableDebugOutput
|
|
|
|
|
startup
|
|
|
|
|
performCommandAction cmd seek $
|
|
|
|
|
shutdown $ cmdnocommit cmd
|
2015-07-08 19:39:05 +00:00
|
|
|
|
go (Left norepo) = do
|
2015-07-10 06:18:08 +00:00
|
|
|
|
(_, a, _globalconfig) <- parsewith
|
2015-07-08 23:38:56 +00:00
|
|
|
|
(fromMaybe (throw norepo) . cmdnorepo)
|
|
|
|
|
(\a -> a =<< Git.Config.global)
|
2015-07-08 19:39:05 +00:00
|
|
|
|
a
|
2015-07-08 16:33:27 +00:00
|
|
|
|
|
2015-07-08 23:38:56 +00:00
|
|
|
|
parsewith getparser ingitrepo =
|
2015-07-10 06:03:03 +00:00
|
|
|
|
case parseCmd progname progdesc globaloptions allargs allcmds getparser of
|
2015-07-08 23:38:56 +00:00
|
|
|
|
O.Failure _ -> do
|
|
|
|
|
-- parse failed, so fall back to
|
|
|
|
|
-- fuzzy matching, or to showing usage
|
|
|
|
|
when fuzzy $
|
|
|
|
|
ingitrepo autocorrect
|
2015-07-10 06:03:03 +00:00
|
|
|
|
liftIO (O.handleParseResult (parseCmd progname progdesc globaloptions correctedargs allcmds getparser))
|
2015-07-08 23:38:56 +00:00
|
|
|
|
res -> liftIO (O.handleParseResult res)
|
|
|
|
|
where
|
2015-07-09 15:49:52 +00:00
|
|
|
|
autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds
|
|
|
|
|
(fuzzy, cmds, inputcmdname, args) = findCmd fuzzyok allargs allcmds
|
2015-07-08 23:38:56 +00:00
|
|
|
|
name
|
|
|
|
|
| fuzzy = case cmds of
|
2015-07-09 15:49:52 +00:00
|
|
|
|
(c:_) -> Just (cmdname c)
|
2015-07-08 23:38:56 +00:00
|
|
|
|
_ -> inputcmdname
|
|
|
|
|
| otherwise = inputcmdname
|
2015-07-09 15:49:52 +00:00
|
|
|
|
correctedargs = case name of
|
|
|
|
|
Nothing -> allargs
|
|
|
|
|
Just n -> n:args
|
2015-07-08 16:33:27 +00:00
|
|
|
|
|
2015-07-08 19:39:05 +00:00
|
|
|
|
{- Parses command line, selecting one of the commands from the list. -}
|
2015-07-10 17:18:46 +00:00
|
|
|
|
parseCmd :: String -> String -> [GlobalOption] -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter)
|
2015-07-10 06:03:03 +00:00
|
|
|
|
parseCmd progname progdesc globaloptions allargs allcmds getparser =
|
|
|
|
|
O.execParserPure (O.prefs O.idm) pinfo allargs
|
2015-07-08 16:33:27 +00:00
|
|
|
|
where
|
2015-07-10 06:18:08 +00:00
|
|
|
|
pinfo = O.info (O.helper <*> subcmds) (O.progDescDoc (Just intro))
|
2015-07-09 05:53:15 +00:00
|
|
|
|
subcmds = O.hsubparser $ mconcat $ map mkcommand allcmds
|
2015-07-09 15:49:52 +00:00
|
|
|
|
mkcommand c = O.command (cmdname c) $ O.info (mkparser c) $ O.fullDesc
|
|
|
|
|
<> O.header (synopsis (progname ++ " " ++ cmdname c) (cmddesc c))
|
|
|
|
|
<> O.footer ("For details, run: " ++ progname ++ " help " ++ cmdname c)
|
2015-07-10 06:18:08 +00:00
|
|
|
|
mkparser c = (,,)
|
2015-07-08 16:33:27 +00:00
|
|
|
|
<$> pure c
|
2015-07-08 19:39:05 +00:00
|
|
|
|
<*> getparser c
|
2015-07-10 17:18:46 +00:00
|
|
|
|
<*> combineGlobalOptions globaloptions
|
2015-07-09 15:49:52 +00:00
|
|
|
|
synopsis n d = n ++ " - " ++ d
|
|
|
|
|
intro = mconcat $ concatMap (\l -> [H.text l, H.line])
|
|
|
|
|
(synopsis progname progdesc : commandList allcmds)
|
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. -}
|
2015-07-09 15:49:52 +00:00
|
|
|
|
findCmd :: Bool -> CmdParams -> [Command] -> (Bool, [Command], Maybe String, CmdParams)
|
|
|
|
|
findCmd fuzzyok argv cmds
|
|
|
|
|
| not (null exactcmds) = ret (False, exactcmds)
|
|
|
|
|
| fuzzyok && not (null inexactcmds) = ret (True, inexactcmds)
|
|
|
|
|
| otherwise = ret (False, [])
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
2015-07-09 15:49:52 +00:00
|
|
|
|
ret (fuzzy, matches) = (fuzzy, matches, name, args)
|
2012-11-11 04:51:07 +00:00
|
|
|
|
(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
|
|
|
|
|
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
|