2011-12-14 15:56:11 -04:00
|
|
|
{- running git commands
|
|
|
|
-
|
2020-06-04 12:13:26 -04:00
|
|
|
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
|
2011-12-14 15:56:11 -04:00
|
|
|
-
|
2019-03-13 15:48:14 -04:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-12-14 15:56:11 -04:00
|
|
|
-}
|
|
|
|
|
2013-10-17 19:35:57 -04:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2011-12-14 15:56:11 -04:00
|
|
|
module Git.Command where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
import Git.Types
|
2012-09-15 17:25:05 -04:00
|
|
|
import qualified Utility.CoProcess as CoProcess
|
2011-12-14 15:56:11 -04:00
|
|
|
|
2019-11-25 16:18:19 -04:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.ByteString as S
|
|
|
|
|
2011-12-14 15:56:11 -04:00
|
|
|
{- Constructs a git command line operating on the specified repo. -}
|
|
|
|
gitCommandLine :: [CommandParam] -> Repo -> [CommandParam]
|
2015-01-06 15:31:24 -04:00
|
|
|
gitCommandLine params r@(Repo { location = l@(Local { } ) }) =
|
2016-04-08 14:24:00 -04:00
|
|
|
setdir ++ settree ++ gitGlobalOpts r ++ params
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2016-04-08 14:24:00 -04:00
|
|
|
setdir
|
|
|
|
| gitEnvOverridesGitDir r = []
|
2019-12-09 13:49:05 -04:00
|
|
|
| otherwise = [Param $ "--git-dir=" ++ fromRawFilePath (gitdir l)]
|
2012-12-13 00:24:19 -04:00
|
|
|
settree = case worktree l of
|
|
|
|
Nothing -> []
|
2019-12-09 13:49:05 -04:00
|
|
|
Just t -> [Param $ "--work-tree=" ++ fromRawFilePath t]
|
2011-12-14 15:56:11 -04:00
|
|
|
gitCommandLine _ repo = assertLocal repo $ error "internal"
|
|
|
|
|
|
|
|
{- Runs git in the specified repo. -}
|
2013-03-03 13:39:07 -04:00
|
|
|
runBool :: [CommandParam] -> Repo -> IO Bool
|
|
|
|
runBool params repo = assertLocal repo $
|
2014-01-22 17:11:41 -04:00
|
|
|
boolSystemEnv "git" (gitCommandLine params repo) (gitEnv repo)
|
|
|
|
|
2011-12-14 15:56:11 -04:00
|
|
|
{- Runs git in the specified repo, throwing an error if it fails. -}
|
2013-03-03 13:39:07 -04:00
|
|
|
run :: [CommandParam] -> Repo -> IO ()
|
|
|
|
run params repo = assertLocal repo $
|
|
|
|
unlessM (runBool params repo) $
|
2021-12-09 14:36:54 -04:00
|
|
|
giveup $ "git " ++ show params ++ " failed"
|
2011-12-14 15:56:11 -04:00
|
|
|
|
2013-03-01 16:21:29 -04:00
|
|
|
{- Runs git and forces it to be quiet, throwing an error if it fails. -}
|
2013-03-03 13:39:07 -04:00
|
|
|
runQuiet :: [CommandParam] -> Repo -> IO ()
|
2020-06-04 15:36:34 -04:00
|
|
|
runQuiet params repo = withNullHandle $ \nullh ->
|
|
|
|
let p = (proc "git" $ toCommand $ gitCommandLine (params) repo)
|
|
|
|
{ env = gitEnv repo
|
|
|
|
, std_out = UseHandle nullh
|
|
|
|
, std_err = UseHandle nullh
|
|
|
|
}
|
|
|
|
in withCreateProcess p $ \_ _ _ -> forceSuccessProcess p
|
2013-03-01 16:21:29 -04:00
|
|
|
|
2013-03-03 13:39:07 -04:00
|
|
|
{- Runs a git command and returns its output, lazily.
|
2011-12-14 15:56:11 -04:00
|
|
|
-
|
2012-10-04 18:47:31 -04:00
|
|
|
- Also returns an action that should be used when the output is all
|
2019-04-24 14:29:46 -04:00
|
|
|
- read, that will wait on the command, and
|
2020-09-25 11:38:42 -04:00
|
|
|
- return True if it succeeded.
|
2011-12-14 15:56:11 -04:00
|
|
|
-}
|
2019-11-25 16:18:19 -04:00
|
|
|
pipeReadLazy :: [CommandParam] -> Repo -> IO (L.ByteString, IO Bool)
|
2012-10-04 18:47:31 -04:00
|
|
|
pipeReadLazy params repo = assertLocal repo $ do
|
2012-10-04 19:41:58 -04:00
|
|
|
(_, Just h, _, pid) <- createProcess p { std_out = CreatePipe }
|
2019-11-25 16:18:19 -04:00
|
|
|
c <- L.hGetContents h
|
2012-10-04 18:47:31 -04:00
|
|
|
return (c, checkSuccessProcess pid)
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
|
|
|
p = gitCreateProcess params repo
|
2012-10-04 18:04:09 -04:00
|
|
|
|
2013-03-03 13:39:07 -04:00
|
|
|
{- Runs a git command, and returns its output, strictly.
|
2012-10-04 18:04:09 -04:00
|
|
|
-
|
|
|
|
- Nonzero exit status is ignored.
|
|
|
|
-}
|
2019-11-25 16:18:19 -04:00
|
|
|
pipeReadStrict :: [CommandParam] -> Repo -> IO S.ByteString
|
2021-05-24 11:33:23 -04:00
|
|
|
pipeReadStrict = pipeReadStrict' id
|
2018-09-20 12:49:14 -04:00
|
|
|
|
2021-05-24 11:33:23 -04:00
|
|
|
pipeReadStrict' :: (CreateProcess -> CreateProcess) -> [CommandParam] -> Repo -> IO S.ByteString
|
|
|
|
pipeReadStrict' fp params repo = assertLocal repo $ withCreateProcess p go
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2021-05-24 11:33:23 -04:00
|
|
|
p = fp (gitCreateProcess params repo) { std_out = CreatePipe }
|
2020-06-04 12:13:26 -04:00
|
|
|
|
|
|
|
go _ (Just outh) _ pid = do
|
2021-05-24 11:33:23 -04:00
|
|
|
output <- S.hGetContents outh
|
2020-06-04 12:13:26 -04:00
|
|
|
hClose outh
|
|
|
|
void $ waitForProcess pid
|
|
|
|
return output
|
|
|
|
go _ _ _ _ = error "internal"
|
2011-12-14 15:56:11 -04:00
|
|
|
|
2013-10-20 17:50:51 -04:00
|
|
|
{- Runs a git command, feeding it an input, and returning its output,
|
2012-07-17 14:40:05 -04:00
|
|
|
- which is expected to be fairly small, since it's all read into memory
|
|
|
|
- strictly. -}
|
2020-04-06 17:14:49 -04:00
|
|
|
pipeWriteRead :: [CommandParam] -> Maybe (Handle -> IO ()) -> Repo -> IO S.ByteString
|
2013-10-20 17:50:51 -04:00
|
|
|
pipeWriteRead params writer repo = assertLocal repo $
|
2012-08-24 20:50:39 -04:00
|
|
|
writeReadProcessEnv "git" (toCommand $ gitCommandLine params repo)
|
2020-04-06 17:14:49 -04:00
|
|
|
(gitEnv repo) writer'
|
2013-06-18 14:58:38 -04:00
|
|
|
where
|
2020-04-06 17:14:49 -04:00
|
|
|
writer' = case writer of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just a -> Just $ \h -> do
|
|
|
|
adjusthandle h
|
|
|
|
a h
|
2016-12-24 14:46:31 -04:00
|
|
|
adjusthandle h = hSetNewlineMode h noNewlineTranslation
|
2012-08-24 20:50:39 -04:00
|
|
|
|
2013-03-03 13:39:07 -04:00
|
|
|
{- Runs a git command, feeding it input on a handle with an action. -}
|
2012-08-24 20:50:39 -04:00
|
|
|
pipeWrite :: [CommandParam] -> Repo -> (Handle -> IO ()) -> IO ()
|
2020-06-05 17:27:49 -04:00
|
|
|
pipeWrite params repo feeder = assertLocal repo $
|
2020-06-04 15:36:34 -04:00
|
|
|
let p = (gitCreateProcess params repo)
|
|
|
|
{ std_in = CreatePipe }
|
|
|
|
in withCreateProcess p (go p)
|
|
|
|
where
|
2020-06-05 17:27:49 -04:00
|
|
|
go p (Just hin) _ _ pid = do
|
|
|
|
feeder hin
|
|
|
|
hClose hin
|
2020-06-04 15:36:34 -04:00
|
|
|
forceSuccessProcess p pid
|
|
|
|
go _ _ _ _ _ = error "internal"
|
2012-10-04 18:04:09 -04:00
|
|
|
|
2011-12-14 15:56:11 -04:00
|
|
|
{- Reads null terminated output of a git command (as enabled by the -z
|
|
|
|
- parameter), and splits it. -}
|
2019-11-25 16:18:19 -04:00
|
|
|
pipeNullSplit :: [CommandParam] -> Repo -> IO ([L.ByteString], IO Bool)
|
2012-10-04 18:47:31 -04:00
|
|
|
pipeNullSplit params repo = do
|
|
|
|
(s, cleanup) <- pipeReadLazy params repo
|
2019-11-25 16:18:19 -04:00
|
|
|
return (filter (not . L.null) $ L.split 0 s, cleanup)
|
2011-12-14 15:56:11 -04:00
|
|
|
|
2019-11-27 14:09:11 -04:00
|
|
|
{- Reads lazily, but copies each part to a strict ByteString for
|
2019-11-26 15:27:22 -04:00
|
|
|
- convenience.
|
|
|
|
-}
|
2019-11-25 16:18:19 -04:00
|
|
|
pipeNullSplit' :: [CommandParam] -> Repo -> IO ([S.ByteString], IO Bool)
|
|
|
|
pipeNullSplit' params repo = do
|
|
|
|
(s, cleanup) <- pipeNullSplit params repo
|
|
|
|
return (map L.toStrict s, cleanup)
|
|
|
|
|
|
|
|
pipeNullSplitStrict :: [CommandParam] -> Repo -> IO [S.ByteString]
|
2013-03-18 22:09:51 -04:00
|
|
|
pipeNullSplitStrict params repo = do
|
|
|
|
s <- pipeReadStrict params repo
|
2019-11-25 16:18:19 -04:00
|
|
|
return $ filter (not . S.null) $ S.split 0 s
|
2012-10-04 19:56:32 -04:00
|
|
|
|
2012-09-15 17:25:05 -04:00
|
|
|
{- Runs a git command as a coprocess. -}
|
2013-05-31 12:20:17 -04:00
|
|
|
gitCoProcessStart :: Bool -> [CommandParam] -> Repo -> IO CoProcess.CoProcessHandle
|
2014-01-01 21:42:25 -04:00
|
|
|
gitCoProcessStart restartable params repo = CoProcess.start numrestarts "git"
|
2013-05-31 12:20:17 -04:00
|
|
|
(toCommand $ gitCommandLine params repo)
|
|
|
|
(gitEnv repo)
|
2014-01-01 21:42:25 -04:00
|
|
|
where
|
2014-10-09 14:53:13 -04:00
|
|
|
{- If a long-running git command like cat-file --batch
|
2014-01-01 21:42:25 -04: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 18:47:31 -04:00
|
|
|
|
|
|
|
gitCreateProcess :: [CommandParam] -> Repo -> CreateProcess
|
|
|
|
gitCreateProcess params repo =
|
|
|
|
(proc "git" $ toCommand $ gitCommandLine params repo)
|
|
|
|
{ env = gitEnv repo }
|