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.
|
|
|
|
|
-}
|
|
|
|
|
|
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
|
2012-10-02 03:01:29 +00:00
|
|
|
|
import System.Posix.Signals
|
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-10-04 04:40:47 +00:00
|
|
|
|
import qualified Annex.Queue
|
2011-06-30 17:16:57 +00:00
|
|
|
|
import qualified Git
|
2012-04-12 19:34:41 +00:00
|
|
|
|
import qualified Git.AutoCorrect
|
2011-10-04 04:40:47 +00:00
|
|
|
|
import Annex.Content
|
2012-01-20 19:34:52 +00:00
|
|
|
|
import Annex.Ssh
|
2013-04-22 19:36:34 +00:00
|
|
|
|
import Annex.Environment
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import Command
|
|
|
|
|
|
2011-10-31 00:04:15 +00:00
|
|
|
|
type Params = [String]
|
|
|
|
|
type Flags = [Annex ()]
|
|
|
|
|
|
2010-12-30 20:52:24 +00:00
|
|
|
|
{- Runs the passed command line. -}
|
2012-07-02 04:53:00 +00:00
|
|
|
|
dispatch :: Bool -> Params -> [Command] -> [Option] -> [(String, String)] -> String -> IO Git.Repo -> IO ()
|
|
|
|
|
dispatch fuzzyok allargs allcmds commonoptions fields header getgitrepo = do
|
2011-03-12 19:30:17 +00:00
|
|
|
|
setupConsole
|
2011-11-16 04:49:09 +00:00
|
|
|
|
r <- E.try getgitrepo :: IO (Either E.SomeException Git.Repo)
|
|
|
|
|
case r of
|
2011-12-09 05:57:13 +00:00
|
|
|
|
Left e -> fromMaybe (throw e) (cmdnorepo cmd)
|
2011-11-16 04:49:09 +00:00
|
|
|
|
Right g -> do
|
|
|
|
|
state <- Annex.new g
|
|
|
|
|
(actions, state') <- Annex.run state $ do
|
2013-04-22 19:36:34 +00:00
|
|
|
|
checkEnvironment
|
2012-04-12 19:34:41 +00:00
|
|
|
|
checkfuzzy
|
2013-03-29 03:27:45 +00:00
|
|
|
|
forM_ fields $ uncurry Annex.setField
|
2011-11-16 04:49:09 +00:00
|
|
|
|
sequence_ flags
|
|
|
|
|
prepCommand cmd params
|
2012-09-16 00:46:38 +00:00
|
|
|
|
tryRun state' cmd $ [startup] ++ actions ++ [shutdown $ cmdnocommit cmd]
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
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
|
2013-03-27 17:51:24 +00:00
|
|
|
|
(flags, params) = getOptCmd args cmd commonoptions
|
2012-11-11 04:51:07 +00:00
|
|
|
|
checkfuzzy = when fuzzy $
|
|
|
|
|
inRepo $ 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. -}
|
2012-04-14 20:01:22 +00:00
|
|
|
|
findCmd :: Bool -> Params -> [Command] -> (String -> String) -> (Bool, [Command], String, Params)
|
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. -}
|
2013-03-27 17:51:24 +00:00
|
|
|
|
getOptCmd :: Params -> Command -> [Option] -> (Flags, Params)
|
|
|
|
|
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
|
|
|
|
|
check (flags, rest, []) = (flags, rest)
|
2013-03-27 17:51:24 +00:00
|
|
|
|
check (_, _, errs) = error $ unlines
|
|
|
|
|
[ concat errs
|
|
|
|
|
, commandUsage cmd
|
|
|
|
|
]
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
|
|
{- Runs a list of Annex actions. Catches IO errors and continues
|
|
|
|
|
- (but explicitly thrown errors terminate the whole command).
|
|
|
|
|
-}
|
2011-10-31 00:04:15 +00:00
|
|
|
|
tryRun :: Annex.AnnexState -> Command -> [CommandCleanup] -> IO ()
|
2011-07-15 07:12:05 +00:00
|
|
|
|
tryRun = tryRun' 0
|
2011-10-31 00:04:15 +00:00
|
|
|
|
tryRun' :: Integer -> Annex.AnnexState -> Command -> [CommandCleanup] -> IO ()
|
|
|
|
|
tryRun' errnum _ cmd []
|
|
|
|
|
| errnum > 0 = error $ cmdname cmd ++ ": " ++ show errnum ++ " failed"
|
2012-04-22 03:32:33 +00:00
|
|
|
|
| otherwise = noop
|
2012-02-13 20:59:00 +00:00
|
|
|
|
tryRun' errnum state cmd (a:as) = do
|
|
|
|
|
r <- run
|
|
|
|
|
handle $! r
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
run = tryIO $ Annex.run state $ do
|
|
|
|
|
Annex.Queue.flushWhenFull
|
|
|
|
|
a
|
|
|
|
|
handle (Left err) = showerr err >> cont False state
|
|
|
|
|
handle (Right (success, state')) = cont success state'
|
|
|
|
|
cont success s = do
|
|
|
|
|
let errnum' = if success then errnum else errnum + 1
|
|
|
|
|
(tryRun' $! errnum') s cmd as
|
|
|
|
|
showerr err = Annex.eval state $ do
|
|
|
|
|
showErr err
|
|
|
|
|
showEndFail
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
|
|
{- Actions to perform each time ran. -}
|
|
|
|
|
startup :: Annex Bool
|
2012-10-02 03:01:29 +00:00
|
|
|
|
startup = liftIO $ do
|
|
|
|
|
void $ installHandler sigINT Default Nothing
|
|
|
|
|
return True
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
|
|
{- Cleanup actions. -}
|
2012-01-28 19:41:52 +00:00
|
|
|
|
shutdown :: Bool -> Annex Bool
|
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
|
2012-01-20 19:34:52 +00:00
|
|
|
|
sshCleanup -- ssh connection caching
|
2011-01-30 03:32:32 +00:00
|
|
|
|
return True
|