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,
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
catObjectStream,
|
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
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
import Control.Concurrent.Async
|
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
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
import qualified Git.LsTree as LsTree
|
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
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
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"
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
<*> startp ("--batch-check=" ++ batchFormat)
|
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
|
|
|
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
batchFormat :: String
|
|
|
|
batchFormat = "%(objectname) %(objecttype) %(objectsize)"
|
|
|
|
|
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
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
Just r@(ParsedResp sha objtype _size) -> do
|
|
|
|
content <- readObjectContent from r
|
|
|
|
return $ Just (content, sha, objtype)
|
2016-10-05 19:21:36 +00:00
|
|
|
Just DNE -> return Nothing
|
|
|
|
Nothing -> error $ "unknown response from git cat-file " ++ show (header, object)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
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
|
|
|
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
readObjectContent :: Handle -> ParsedResp -> IO L.ByteString
|
|
|
|
readObjectContent h (ParsedResp _ _ size) = do
|
|
|
|
content <- S.hGet h (fromIntegral size)
|
|
|
|
eatchar '\n'
|
|
|
|
return (L.fromChunks [content])
|
|
|
|
where
|
|
|
|
eatchar expected = do
|
|
|
|
c <- hGetChar h
|
|
|
|
when (c /= expected) $
|
|
|
|
error $ "missing " ++ (show expected) ++ " from git cat-file"
|
|
|
|
readObjectContent _ DNE = error "internal"
|
|
|
|
|
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
|
|
|
|
}
|
2020-06-04 16:13:26 +00:00
|
|
|
withCreateProcess p' go
|
|
|
|
where
|
|
|
|
go _ (Just outh) _ pid = do
|
|
|
|
output <- reader outh
|
|
|
|
hClose outh
|
|
|
|
ifM (checkSuccessProcess pid)
|
2018-09-20 16:49:14 +00:00
|
|
|
( return (Just output)
|
|
|
|
, return Nothing
|
|
|
|
)
|
2020-06-04 16:13:26 +00:00
|
|
|
go _ _ _ _ = error "internal"
|
2018-09-20 16:49:14 +00:00
|
|
|
|
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 '>')
|
sped up the --all option by 2x to 16x by using git cat-file --buffer
This assumes that no location log files will have a newline or carriage
return in their name. catObjectStream skips any such files due to
cat-file not supporting them.
Keys have been prevented from containing newlines since 2011,
commit 480495beb4a3422f006ee529df807a20cc944727. If some old repo
had a key with a newline in it, --all will just skip processing that key.
Other things, like .git/annex/unused files certianly assume no newlines in
keys too, and AFAICR, such keys never actually worked.
Carriage return is escaped by preSanitizeKeyName since 2013. WORM keys
generated before that point could perhaps contain a CR. (URL probably not,
http probably doesn't support an URL with a raw CR in it.) So, added
a warning in fsck about such keys. Although, fsck --all will naturally
skip them, so won't be able to warn about them. Not entirely
satisfactory, but I'll bet there are not really any such keys in
existence.
Thanks to Lukey for finding this optimisation.
2020-07-07 17:46:45 +00:00
|
|
|
|
|
|
|
{- Uses cat-file to stream the contents of the files listed by lstree
|
|
|
|
- as efficiently as possible. This is much faster than querying it
|
|
|
|
- repeatedly per file.
|
|
|
|
-
|
|
|
|
- Any files with a newline or carriage return in their name will be
|
|
|
|
- skipped, because the interface does not support them.
|
|
|
|
-}
|
|
|
|
catObjectStream
|
|
|
|
:: (MonadMask m, MonadIO m)
|
|
|
|
=> [LsTree.TreeItem]
|
|
|
|
-> (LsTree.TreeItem -> Bool)
|
|
|
|
-> Repo
|
|
|
|
-> (IO (Maybe (TopFilePath, L.ByteString)) -> m ())
|
|
|
|
-> m ()
|
|
|
|
catObjectStream l want repo a = assertLocal repo $
|
|
|
|
bracketIO start stop $ \(_, _, hout, _) -> a (reader hout)
|
|
|
|
where
|
|
|
|
feeder h = do
|
|
|
|
forM_ l $ \ti ->
|
|
|
|
when (want ti) $ do
|
|
|
|
let f = getTopFilePath (LsTree.file ti)
|
|
|
|
-- skip files with newlines or carriage returns
|
|
|
|
unless (any (`S8.elem` f) ['\n', '\r']) $
|
|
|
|
S8.hPutStrLn h $
|
|
|
|
fromRef' (LsTree.sha ti) <> " " <> f
|
|
|
|
hClose h
|
|
|
|
|
|
|
|
reader h = ifM (hIsEOF h)
|
|
|
|
( return Nothing
|
|
|
|
, do
|
|
|
|
resp <- S8.hGetLine h
|
|
|
|
case eitherToMaybe $ A.parseOnly respparser resp of
|
|
|
|
Nothing -> error $ "unknown response from git cat-file " ++ show resp
|
|
|
|
Just (r, f) -> do
|
|
|
|
content <- readObjectContent h r
|
|
|
|
return (Just (asTopFilePath f, content))
|
|
|
|
)
|
|
|
|
|
|
|
|
params =
|
|
|
|
[ Param "cat-file"
|
|
|
|
-- %(rest) is used to feed the filename through
|
|
|
|
-- cat-file; it will be at the end of the response
|
|
|
|
, Param ("--batch=" ++ batchFormat ++ " %(rest)")
|
|
|
|
, Param "--buffer"
|
|
|
|
]
|
|
|
|
|
|
|
|
respparser = (,)
|
|
|
|
<$> respParser
|
|
|
|
<* A8.char ' '
|
|
|
|
<*> A.takeByteString
|
|
|
|
|
|
|
|
start = do
|
|
|
|
let p = gitCreateProcess params repo
|
|
|
|
(Just hin, Just hout, _, pid) <- createProcess p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
}
|
|
|
|
f <- async (feeder hin)
|
|
|
|
return (f, hin, hout, pid)
|
|
|
|
|
|
|
|
stop (f, hin, hout, pid) = do
|
|
|
|
cancel f
|
|
|
|
hClose hin
|
|
|
|
hClose hout
|
|
|
|
void $ checkSuccessProcess pid
|