2011-12-14 19:56:11 +00:00
|
|
|
{- running git commands
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2010-2013 Joey Hess <id@joeyh.name>
|
2011-12-14 19:56:11 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-10-17 23:35:57 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2011-12-14 19:56:11 +00:00
|
|
|
module Git.Command where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
import Git.Types
|
2012-09-15 21:25:05 +00:00
|
|
|
import qualified Utility.CoProcess as CoProcess
|
2011-12-14 19:56:11 +00:00
|
|
|
|
|
|
|
{- Constructs a git command line operating on the specified repo. -}
|
|
|
|
gitCommandLine :: [CommandParam] -> Repo -> [CommandParam]
|
2015-01-06 19:31:24 +00:00
|
|
|
gitCommandLine params r@(Repo { location = l@(Local { } ) }) =
|
2016-04-08 18:24:00 +00:00
|
|
|
setdir ++ settree ++ gitGlobalOpts r ++ params
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2016-04-08 18:24:00 +00:00
|
|
|
setdir
|
|
|
|
| gitEnvOverridesGitDir r = []
|
|
|
|
| otherwise = [Param $ "--git-dir=" ++ gitdir l]
|
2012-12-13 04:24:19 +00:00
|
|
|
settree = case worktree l of
|
|
|
|
Nothing -> []
|
2014-02-12 18:32:47 +00:00
|
|
|
Just t -> [Param $ "--work-tree=" ++ t]
|
2011-12-14 19:56:11 +00:00
|
|
|
gitCommandLine _ repo = assertLocal repo $ error "internal"
|
|
|
|
|
|
|
|
{- Runs git in the specified repo. -}
|
2013-03-03 17:39:07 +00:00
|
|
|
runBool :: [CommandParam] -> Repo -> IO Bool
|
|
|
|
runBool params repo = assertLocal repo $
|
2014-01-22 21:11:41 +00:00
|
|
|
boolSystemEnv "git" (gitCommandLine params repo) (gitEnv repo)
|
|
|
|
|
2011-12-14 19:56:11 +00:00
|
|
|
{- Runs git in the specified repo, throwing an error if it fails. -}
|
2013-03-03 17:39:07 +00:00
|
|
|
run :: [CommandParam] -> Repo -> IO ()
|
|
|
|
run params repo = assertLocal repo $
|
|
|
|
unlessM (runBool params repo) $
|
|
|
|
error $ "git " ++ show params ++ " failed"
|
2011-12-14 19:56:11 +00:00
|
|
|
|
2013-03-01 20:21:29 +00:00
|
|
|
{- Runs git and forces it to be quiet, throwing an error if it fails. -}
|
2013-03-03 17:39:07 +00:00
|
|
|
runQuiet :: [CommandParam] -> Repo -> IO ()
|
|
|
|
runQuiet params repo = withQuietOutput createProcessSuccess $
|
|
|
|
(proc "git" $ toCommand $ gitCommandLine (params) repo)
|
2013-03-01 20:21:29 +00:00
|
|
|
{ env = gitEnv repo }
|
|
|
|
|
2013-03-03 17:39:07 +00:00
|
|
|
{- Runs a git command and returns its output, lazily.
|
2011-12-14 19:56:11 +00:00
|
|
|
-
|
2012-10-04 22:47:31 +00:00
|
|
|
- Also returns an action that should be used when the output is all
|
|
|
|
- read (or no more is needed), that will wait on the command, and
|
|
|
|
- return True if it succeeded. Failure to wait will result in zombies.
|
2011-12-14 19:56:11 +00:00
|
|
|
-}
|
2012-10-04 22:47:31 +00:00
|
|
|
pipeReadLazy :: [CommandParam] -> Repo -> IO (String, IO Bool)
|
|
|
|
pipeReadLazy params repo = assertLocal repo $ do
|
2012-10-04 23:41:58 +00:00
|
|
|
(_, Just h, _, pid) <- createProcess p { std_out = CreatePipe }
|
2012-10-04 22:47:31 +00:00
|
|
|
c <- hGetContents h
|
|
|
|
return (c, checkSuccessProcess pid)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p = gitCreateProcess params repo
|
2012-10-04 22:04:09 +00:00
|
|
|
|
2013-03-03 17:39:07 +00:00
|
|
|
{- Runs a git command, and returns its output, strictly.
|
2012-10-04 22:04:09 +00:00
|
|
|
-
|
|
|
|
- Nonzero exit status is ignored.
|
|
|
|
-}
|
|
|
|
pipeReadStrict :: [CommandParam] -> Repo -> IO String
|
|
|
|
pipeReadStrict params repo = assertLocal repo $
|
|
|
|
withHandle StdoutHandle (createProcessChecked ignoreFailureProcess) p $ \h -> do
|
|
|
|
output <- hGetContentsStrict h
|
|
|
|
hClose h
|
|
|
|
return output
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
p = gitCreateProcess params repo
|
2011-12-14 19:56:11 +00:00
|
|
|
|
2013-10-20 21:50:51 +00:00
|
|
|
{- Runs a git command, feeding it an input, and returning its output,
|
2012-07-17 18:40:05 +00:00
|
|
|
- which is expected to be fairly small, since it's all read into memory
|
|
|
|
- strictly. -}
|
2013-10-20 21:50:51 +00:00
|
|
|
pipeWriteRead :: [CommandParam] -> Maybe (Handle -> IO ()) -> Repo -> IO String
|
|
|
|
pipeWriteRead params writer repo = assertLocal repo $
|
2012-08-25 00:50:39 +00:00
|
|
|
writeReadProcessEnv "git" (toCommand $ gitCommandLine params repo)
|
2013-10-20 21:50:51 +00:00
|
|
|
(gitEnv repo) writer (Just adjusthandle)
|
2013-06-18 18:58:38 +00:00
|
|
|
where
|
2016-12-24 18:46:31 +00:00
|
|
|
adjusthandle h = hSetNewlineMode h noNewlineTranslation
|
2012-08-25 00:50:39 +00:00
|
|
|
|
2013-03-03 17:39:07 +00:00
|
|
|
{- Runs a git command, feeding it input on a handle with an action. -}
|
2012-08-25 00:50:39 +00:00
|
|
|
pipeWrite :: [CommandParam] -> Repo -> (Handle -> IO ()) -> IO ()
|
2012-10-04 22:04:09 +00:00
|
|
|
pipeWrite params repo = withHandle StdinHandle createProcessSuccess $
|
|
|
|
gitCreateProcess params repo
|
|
|
|
|
2011-12-14 19:56:11 +00:00
|
|
|
{- Reads null terminated output of a git command (as enabled by the -z
|
|
|
|
- parameter), and splits it. -}
|
2012-10-04 22:47:31 +00:00
|
|
|
pipeNullSplit :: [CommandParam] -> Repo -> IO ([String], IO Bool)
|
|
|
|
pipeNullSplit params repo = do
|
|
|
|
(s, cleanup) <- pipeReadLazy params repo
|
2017-01-31 22:40:42 +00:00
|
|
|
return (filter (not . null) $ splitc sep s, cleanup)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2017-01-31 22:40:42 +00:00
|
|
|
sep = '\0'
|
2011-12-14 19:56:11 +00:00
|
|
|
|
2013-03-19 02:09:51 +00:00
|
|
|
pipeNullSplitStrict :: [CommandParam] -> Repo -> IO [String]
|
|
|
|
pipeNullSplitStrict params repo = do
|
|
|
|
s <- pipeReadStrict params repo
|
2017-01-31 22:40:42 +00:00
|
|
|
return $ filter (not . null) $ splitc sep s
|
2013-03-19 02:09:51 +00:00
|
|
|
where
|
2017-01-31 22:40:42 +00:00
|
|
|
sep = '\0'
|
2012-10-04 23:56:32 +00:00
|
|
|
|
2012-10-04 22:47:31 +00:00
|
|
|
pipeNullSplitZombie :: [CommandParam] -> Repo -> IO [String]
|
2012-10-04 23:56:32 +00:00
|
|
|
pipeNullSplitZombie params repo = leaveZombie <$> pipeNullSplit params repo
|
|
|
|
|
|
|
|
{- Doesn't run the cleanup action. A zombie results. -}
|
|
|
|
leaveZombie :: (a, IO Bool) -> a
|
|
|
|
leaveZombie = fst
|
2012-10-04 22:47:31 +00:00
|
|
|
|
2012-09-15 21:25:05 +00:00
|
|
|
{- Runs a git command as a coprocess. -}
|
2013-05-31 16:20:17 +00:00
|
|
|
gitCoProcessStart :: Bool -> [CommandParam] -> Repo -> IO CoProcess.CoProcessHandle
|
2014-01-02 01:42:25 +00:00
|
|
|
gitCoProcessStart restartable params repo = CoProcess.start numrestarts "git"
|
2013-05-31 16:20:17 +00:00
|
|
|
(toCommand $ gitCommandLine params repo)
|
|
|
|
(gitEnv repo)
|
2014-01-02 01:42:25 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
{- If a long-running git command like cat-file --batch
|
2014-01-02 01:42:25 +00:00
|
|
|
- crashes, it will likely start up again ok. If it keeps crashing
|
|
|
|
- 10 times, something is badly wrong. -}
|
|
|
|
numrestarts = if restartable then 10 else 0
|
2012-10-04 22:47:31 +00:00
|
|
|
|
|
|
|
gitCreateProcess :: [CommandParam] -> Repo -> CreateProcess
|
|
|
|
gitCreateProcess params repo =
|
|
|
|
(proc "git" $ toCommand $ gitCommandLine params repo)
|
|
|
|
{ env = gitEnv repo }
|