2011-09-28 19:15:42 +00:00
|
|
|
{- git cat-file interface
|
|
|
|
-
|
2020-04-10 18:03:40 +00:00
|
|
|
- Copyright 2011-2020 Joey Hess <id@joeyh.name>
|
2011-09-28 19:15:42 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-09-28 19:15:42 +00:00
|
|
|
-}
|
|
|
|
|
2020-04-07 17:27:11 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2011-09-28 19:15:42 +00:00
|
|
|
module Git.CatFile (
|
|
|
|
CatFileHandle,
|
|
|
|
catFileStart,
|
2013-10-20 21:50:51 +00:00
|
|
|
catFileStart',
|
2011-09-28 19:15:42 +00:00
|
|
|
catFileStop,
|
2011-11-12 21:45:12 +00:00
|
|
|
catFile,
|
2014-03-03 18:57:16 +00:00
|
|
|
catFileDetails,
|
2013-09-19 19:58:35 +00:00
|
|
|
catTree,
|
2016-02-25 18:59:35 +00:00
|
|
|
catCommit,
|
2012-06-10 23:58:34 +00:00
|
|
|
catObject,
|
|
|
|
catObjectDetails,
|
2016-10-05 19:21:36 +00:00
|
|
|
catObjectMetaData,
|
2011-09-28 19:15:42 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import System.IO
|
2012-06-20 17:13:40 +00:00
|
|
|
import qualified Data.ByteString as S
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2020-04-10 18:03:40 +00:00
|
|
|
import qualified Data.ByteString.Char8 as S8
|
|
|
|
import qualified Data.Attoparsec.ByteString as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
2016-02-25 18:59:35 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.String
|
|
|
|
import Data.Char
|
2013-09-19 19:58:35 +00:00
|
|
|
import Numeric
|
|
|
|
import System.Posix.Types
|
2018-09-20 16:49:14 +00:00
|
|
|
import Text.Read
|
2011-09-28 19:15:42 +00:00
|
|
|
|
2011-12-20 18:37:53 +00:00
|
|
|
import Common
|
2011-09-28 19:15:42 +00:00
|
|
|
import Git
|
2011-12-14 19:30:14 +00:00
|
|
|
import Git.Sha
|
2019-12-27 18:58:10 +00:00
|
|
|
import qualified Git.Ref
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2012-06-06 06:31:31 +00:00
|
|
|
import Git.Types
|
2013-05-12 22:18:48 +00:00
|
|
|
import Git.FilePath
|
2018-09-20 16:49:14 +00:00
|
|
|
import Git.HashObject
|
2012-02-20 19:20:36 +00:00
|
|
|
import qualified Utility.CoProcess as CoProcess
|
2017-05-16 03:32:17 +00:00
|
|
|
import Utility.Tuple
|
2011-09-28 19:15:42 +00:00
|
|
|
|
2016-10-05 19:21:36 +00:00
|
|
|
data CatFileHandle = CatFileHandle
|
|
|
|
{ catFileProcess :: CoProcess.CoProcessHandle
|
|
|
|
, checkFileProcess :: CoProcess.CoProcessHandle
|
2018-09-20 16:49:14 +00:00
|
|
|
, gitRepo :: Repo
|
2016-10-05 19:21:36 +00:00
|
|
|
}
|
2011-09-28 19:15:42 +00:00
|
|
|
|
|
|
|
catFileStart :: Repo -> IO CatFileHandle
|
2013-10-20 21:50:51 +00:00
|
|
|
catFileStart = catFileStart' True
|
|
|
|
|
|
|
|
catFileStart' :: Bool -> Repo -> IO CatFileHandle
|
2016-10-05 19:21:36 +00:00
|
|
|
catFileStart' restartable repo = CatFileHandle
|
|
|
|
<$> startp "--batch"
|
|
|
|
<*> startp "--batch-check=%(objectname) %(objecttype) %(objectsize)"
|
2018-09-20 16:49:14 +00:00
|
|
|
<*> pure repo
|
2016-10-05 19:21:36 +00:00
|
|
|
where
|
2016-11-01 18:03:55 +00:00
|
|
|
startp p = gitCoProcessStart restartable
|
2013-08-01 21:30:47 +00:00
|
|
|
[ Param "cat-file"
|
2016-10-05 19:21:36 +00:00
|
|
|
, Param p
|
2013-08-01 21:30:47 +00:00
|
|
|
] repo
|
2011-09-28 19:15:42 +00:00
|
|
|
|
|
|
|
catFileStop :: CatFileHandle -> IO ()
|
2016-10-05 19:21:36 +00:00
|
|
|
catFileStop h = do
|
|
|
|
CoProcess.stop (catFileProcess h)
|
|
|
|
CoProcess.stop (checkFileProcess h)
|
2011-09-28 19:15:42 +00:00
|
|
|
|
2011-11-12 21:45:12 +00:00
|
|
|
{- Reads a file from a specified branch. -}
|
2019-11-25 20:18:19 +00:00
|
|
|
catFile :: CatFileHandle -> Branch -> RawFilePath -> IO L.ByteString
|
2013-05-12 22:18:48 +00:00
|
|
|
catFile h branch file = catObject h $ Ref $
|
2020-04-07 17:27:11 +00:00
|
|
|
fromRef' branch <> ":" <> toInternalGitPath file
|
2011-11-12 21:45:12 +00:00
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
catFileDetails :: CatFileHandle -> Branch -> RawFilePath -> IO (Maybe (L.ByteString, Sha, ObjectType))
|
2014-03-03 18:57:16 +00:00
|
|
|
catFileDetails h branch file = catObjectDetails h $ Ref $
|
2020-04-07 17:27:11 +00:00
|
|
|
fromRef' branch <> ":" <> toInternalGitPath file
|
2014-03-03 18:57:16 +00:00
|
|
|
|
2011-11-12 21:45:12 +00:00
|
|
|
{- Uses a running git cat-file read the content of an object.
|
|
|
|
- Objects that do not exist will have "" returned. -}
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
catObject :: CatFileHandle -> Ref -> IO L.ByteString
|
2013-10-20 21:50:51 +00:00
|
|
|
catObject h object = maybe L.empty fst3 <$> catObjectDetails h object
|
2012-06-10 23:58:34 +00:00
|
|
|
|
2013-10-20 21:50:51 +00:00
|
|
|
catObjectDetails :: CatFileHandle -> Ref -> IO (Maybe (L.ByteString, Sha, ObjectType))
|
2018-09-20 16:49:14 +00:00
|
|
|
catObjectDetails h object = query (catFileProcess h) object newlinefallback $ \from -> do
|
2020-04-10 18:03:40 +00:00
|
|
|
header <- S8.hGetLine from
|
2016-10-05 19:21:36 +00:00
|
|
|
case parseResp object header of
|
2020-04-10 18:03:40 +00:00
|
|
|
Just (ParsedResp sha objtype size) -> do
|
2016-10-05 19:21:36 +00:00
|
|
|
content <- S.hGet from (fromIntegral size)
|
|
|
|
eatchar '\n' from
|
|
|
|
return $ Just (L.fromChunks [content], sha, objtype)
|
|
|
|
Just DNE -> return Nothing
|
|
|
|
Nothing -> error $ "unknown response from git cat-file " ++ show (header, object)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2013-05-11 20:32:34 +00:00
|
|
|
eatchar expected from = do
|
|
|
|
c <- hGetChar from
|
|
|
|
when (c /= expected) $
|
2013-05-11 21:02:35 +00:00
|
|
|
error $ "missing " ++ (show expected) ++ " from git cat-file"
|
2018-09-20 16:49:14 +00:00
|
|
|
|
|
|
|
-- Slow fallback path for filenames containing newlines.
|
|
|
|
newlinefallback = queryObjectType object (gitRepo h) >>= \case
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just objtype -> queryContent object (gitRepo h) >>= \case
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just content -> do
|
|
|
|
-- only the --batch interface allows getting
|
|
|
|
-- the sha, so have to re-hash the object
|
|
|
|
sha <- hashObject' objtype
|
|
|
|
(flip L.hPut content)
|
|
|
|
(gitRepo h)
|
|
|
|
return (Just (content, sha, objtype))
|
2013-08-01 21:30:47 +00:00
|
|
|
|
2016-10-05 19:21:36 +00:00
|
|
|
{- Gets the size and type of an object, without reading its content. -}
|
2019-12-27 18:58:10 +00:00
|
|
|
catObjectMetaData :: CatFileHandle -> Ref -> IO (Maybe (Sha, FileSize, ObjectType))
|
2018-09-20 16:49:14 +00:00
|
|
|
catObjectMetaData h object = query (checkFileProcess h) object newlinefallback $ \from -> do
|
2020-04-10 18:03:40 +00:00
|
|
|
resp <- S8.hGetLine from
|
2016-10-05 19:21:36 +00:00
|
|
|
case parseResp object resp of
|
2020-04-10 18:03:40 +00:00
|
|
|
Just (ParsedResp sha objtype size) ->
|
2019-12-27 18:58:10 +00:00
|
|
|
return $ Just (sha, size, objtype)
|
2016-10-05 19:21:36 +00:00
|
|
|
Just DNE -> return Nothing
|
|
|
|
Nothing -> error $ "unknown response from git cat-file " ++ show (resp, object)
|
2018-09-20 16:49:14 +00:00
|
|
|
where
|
|
|
|
-- Slow fallback path for filenames containing newlines.
|
|
|
|
newlinefallback = do
|
2019-12-27 18:58:10 +00:00
|
|
|
sha <- Git.Ref.sha object (gitRepo h)
|
2018-09-20 16:49:14 +00:00
|
|
|
sz <- querySize object (gitRepo h)
|
|
|
|
objtype <- queryObjectType object (gitRepo h)
|
2019-12-27 18:58:10 +00:00
|
|
|
return $ (,,) <$> sha <*> sz <*> objtype
|
2016-10-05 19:21:36 +00:00
|
|
|
|
2020-04-10 18:03:40 +00:00
|
|
|
data ParsedResp = ParsedResp Sha ObjectType FileSize | DNE
|
|
|
|
deriving (Show)
|
2016-10-05 19:21:36 +00:00
|
|
|
|
2018-09-20 16:49:14 +00:00
|
|
|
query :: CoProcess.CoProcessHandle -> Ref -> IO a -> (Handle -> IO a) -> IO a
|
|
|
|
query hdl object newlinefallback receive
|
|
|
|
-- git cat-file --batch uses a line based protocol, so when the
|
|
|
|
-- filename itself contains a newline, have to fall back to another
|
|
|
|
-- method of getting the information.
|
2020-04-10 18:03:40 +00:00
|
|
|
| '\n' `S8.elem` s = newlinefallback
|
2019-10-08 19:27:05 +00:00
|
|
|
-- git strips carriage return from the end of a line, out of some
|
|
|
|
-- misplaced desire to support windows, so also use the newline
|
|
|
|
-- fallback for those.
|
2020-04-10 18:03:40 +00:00
|
|
|
| "\r" `S8.isSuffixOf` s = newlinefallback
|
2018-09-20 16:49:14 +00:00
|
|
|
| otherwise = CoProcess.query hdl send receive
|
2016-10-05 19:21:36 +00:00
|
|
|
where
|
2020-04-10 18:03:40 +00:00
|
|
|
send to = S8.hPutStrLn to s
|
|
|
|
s = fromRef' object
|
|
|
|
|
|
|
|
parseResp :: Ref -> S.ByteString -> Maybe ParsedResp
|
|
|
|
parseResp object s
|
|
|
|
| " missing" `S.isSuffixOf` s -- less expensive than full check
|
|
|
|
&& s == fromRef' object <> " missing" = Just DNE
|
|
|
|
| otherwise = eitherToMaybe $ A.parseOnly respParser s
|
|
|
|
|
|
|
|
respParser :: A.Parser ParsedResp
|
|
|
|
respParser = ParsedResp
|
|
|
|
<$> (maybe (fail "bad sha") return . extractSha =<< nextword)
|
|
|
|
<* A8.char ' '
|
|
|
|
<*> (maybe (fail "bad object type") return . readObjectType =<< nextword)
|
|
|
|
<* A8.char ' '
|
|
|
|
<*> A8.decimal
|
|
|
|
where
|
|
|
|
nextword = A8.takeTill (== ' ')
|
2016-10-05 19:21:36 +00:00
|
|
|
|
2018-09-20 16:49:14 +00:00
|
|
|
querySingle :: CommandParam -> Ref -> Repo -> (Handle -> IO a) -> IO (Maybe a)
|
|
|
|
querySingle o r repo reader = assertLocal repo $
|
|
|
|
-- In non-batch mode, git cat-file warns on stderr when
|
|
|
|
-- asked for an object that does not exist.
|
|
|
|
-- Squelch that warning to behave the same as batch mode.
|
|
|
|
withNullHandle $ \nullh -> do
|
|
|
|
let p = gitCreateProcess
|
|
|
|
[ Param "cat-file"
|
|
|
|
, o
|
|
|
|
, Param (fromRef r)
|
|
|
|
] repo
|
|
|
|
let p' = p
|
|
|
|
{ std_err = UseHandle nullh
|
|
|
|
, std_in = Inherit
|
|
|
|
, std_out = CreatePipe
|
|
|
|
}
|
|
|
|
pid <- createProcess p'
|
|
|
|
let h = stdoutHandle pid
|
|
|
|
output <- reader h
|
|
|
|
hClose h
|
|
|
|
ifM (checkSuccessProcess (processHandle pid))
|
|
|
|
( return (Just output)
|
|
|
|
, return Nothing
|
|
|
|
)
|
|
|
|
|
2019-12-27 18:58:10 +00:00
|
|
|
querySize :: Ref -> Repo -> IO (Maybe FileSize)
|
2018-09-20 16:49:14 +00:00
|
|
|
querySize r repo = maybe Nothing (readMaybe . takeWhile (/= '\n'))
|
|
|
|
<$> querySingle (Param "-s") r repo hGetContentsStrict
|
|
|
|
|
|
|
|
queryObjectType :: Ref -> Repo -> IO (Maybe ObjectType)
|
2019-11-25 20:18:19 +00:00
|
|
|
queryObjectType r repo = maybe Nothing (readObjectType . encodeBS . takeWhile (/= '\n'))
|
2019-08-07 17:35:42 +00:00
|
|
|
<$> querySingle (Param "-t") r repo hGetContentsStrict
|
2018-09-20 16:49:14 +00:00
|
|
|
|
|
|
|
queryContent :: Ref -> Repo -> IO (Maybe L.ByteString)
|
|
|
|
queryContent r repo = fmap (\b -> L.fromChunks [b])
|
|
|
|
<$> querySingle (Param "-p") r repo S.hGetContents
|
|
|
|
|
2013-09-19 19:58:35 +00:00
|
|
|
{- Gets a list of files and directories in a tree. (Not recursive.) -}
|
|
|
|
catTree :: CatFileHandle -> Ref -> IO [(FilePath, FileMode)]
|
|
|
|
catTree h treeref = go <$> catObjectDetails h treeref
|
|
|
|
where
|
2013-10-20 21:50:51 +00:00
|
|
|
go (Just (b, _, TreeObject)) = parsetree [] b
|
2014-10-09 18:53:13 +00:00
|
|
|
go _ = []
|
2013-09-19 19:58:35 +00:00
|
|
|
|
|
|
|
parsetree c b = case L.break (== 0) b of
|
|
|
|
(modefile, rest)
|
|
|
|
| L.null modefile -> c
|
|
|
|
| otherwise -> parsetree
|
|
|
|
(parsemodefile modefile:c)
|
|
|
|
(dropsha rest)
|
|
|
|
|
|
|
|
-- these 20 bytes after the NUL hold the file's sha
|
|
|
|
dropsha = L.drop 21
|
|
|
|
|
|
|
|
parsemodefile b =
|
2019-01-01 18:54:06 +00:00
|
|
|
let (modestr, file) = separate (== ' ') (decodeBL b)
|
2013-09-19 19:58:35 +00:00
|
|
|
in (file, readmode modestr)
|
2015-04-19 04:38:29 +00:00
|
|
|
readmode = fromMaybe 0 . fmap fst . headMaybe . readOct
|
2016-02-25 18:59:35 +00:00
|
|
|
|
|
|
|
catCommit :: CatFileHandle -> Ref -> IO (Maybe Commit)
|
|
|
|
catCommit h commitref = go <$> catObjectDetails h commitref
|
|
|
|
where
|
2020-04-07 17:27:11 +00:00
|
|
|
go (Just (b, _, CommitObject)) = parseCommit (L.toStrict b)
|
2016-02-25 18:59:35 +00:00
|
|
|
go _ = Nothing
|
|
|
|
|
2020-04-07 17:27:11 +00:00
|
|
|
parseCommit :: S.ByteString -> Maybe Commit
|
2016-02-25 18:59:35 +00:00
|
|
|
parseCommit b = Commit
|
2020-04-07 17:27:11 +00:00
|
|
|
<$> (extractSha =<< field "tree")
|
|
|
|
<*> Just (maybe [] (mapMaybe extractSha) (fields "parent"))
|
2016-02-25 18:59:35 +00:00
|
|
|
<*> (parsemetadata <$> field "author")
|
|
|
|
<*> (parsemetadata <$> field "committer")
|
2020-04-07 17:27:11 +00:00
|
|
|
<*> Just (decodeBS $ S.intercalate (S.singleton nl) message)
|
2016-02-25 18:59:35 +00:00
|
|
|
where
|
2016-03-11 16:47:14 +00:00
|
|
|
field n = headMaybe =<< fields n
|
|
|
|
fields n = M.lookup (fromString n) fieldmap
|
|
|
|
fieldmap = M.fromListWith (++) ((map breakfield) header)
|
2016-02-25 18:59:35 +00:00
|
|
|
breakfield l =
|
2020-04-07 17:27:11 +00:00
|
|
|
let (k, sp_v) = S.break (== sp) l
|
|
|
|
in (k, [S.drop 1 sp_v])
|
|
|
|
(header, message) = separate S.null ls
|
|
|
|
ls = S.split nl b
|
2016-02-25 18:59:35 +00:00
|
|
|
|
|
|
|
-- author and committer lines have the form: "name <email> date"
|
|
|
|
-- The email is always present, even if empty "<>"
|
|
|
|
parsemetadata l = CommitMetaData
|
2020-04-07 17:27:11 +00:00
|
|
|
{ commitName = whenset $ S.init name_sp
|
2016-02-25 18:59:35 +00:00
|
|
|
, commitEmail = whenset email
|
2020-04-07 17:27:11 +00:00
|
|
|
, commitDate = whenset $ S.drop 2 gt_sp_date
|
2016-02-25 18:59:35 +00:00
|
|
|
}
|
|
|
|
where
|
2020-04-07 17:27:11 +00:00
|
|
|
(name_sp, rest) = S.break (== lt) l
|
|
|
|
(email, gt_sp_date) = S.break (== gt) (S.drop 1 rest)
|
2016-02-25 18:59:35 +00:00
|
|
|
whenset v
|
2020-04-07 17:27:11 +00:00
|
|
|
| S.null v = Nothing
|
|
|
|
| otherwise = Just (decodeBS v)
|
2016-02-25 18:59:35 +00:00
|
|
|
|
|
|
|
nl = fromIntegral (ord '\n')
|
|
|
|
sp = fromIntegral (ord ' ')
|
|
|
|
lt = fromIntegral (ord '<')
|
|
|
|
gt = fromIntegral (ord '>')
|