8000 using Attoparsec for git ls-tree result by yuanw · Pull Request #99 · github/semantic · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

using Attoparsec for git ls-tree result #99

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/Semantic/Git.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module Semantic.Git
, OID(..)
) where

import Control.Monad.IO.Class
import Data.Text as Text
import Shelly hiding (FilePath)
import System.IO (hSetBinaryMode)
import Control.Monad.IO.Class
import Data.Char (isSpace)
import qualified Data.Attoparsec.Text as Parser
import Data.Text as Text
import Shelly hiding (FilePath)
import System.IO (hSetBinaryMode)

-- | git clone --bare
clone :: Text -> FilePath -> IO ()
Expand All @@ -30,12 +32,25 @@ catFile gitDir (OID oid) = sh $ do
lsTree :: FilePath -> OID -> IO [TreeEntry]
lsTree gitDir (OID sha) = sh $ do
out <- run "git" [pack ("--git-dir=" <> gitDir), "ls-tree", "-rz", sha]
pure $ mkEntry <$> splitOn "\NUL" out
where
mkEntry row | [mode, ty, rest] <- splitOn " " row
, [oid, path] <- splitOn "\t" rest
= TreeEntry (objectMode mode) (objectType ty) (OID oid) (unpack path)
| otherwise = nullTreeEntry
pure $ either mempty id (Parser.parseOnly treeEntriesParser out)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite sure how to handle failure cases



treeEntryParser :: Parser.Parser TreeEntry
treeEntryParser = do
mode <- Parser.takeTill isSpace
Parser.skipSpace
ty <- Parser.takeTill isSpace
Parser.skipSpace
oid <- Parser.takeTill (== '\t')
_ <- Parser.char '\t'
Parser.skipSpace
path <- Parser.takeTill (== '\NUL')
pure $ TreeEntry (objectMode mode) (objectType ty) (OID oid) (Text.unpack path)


treeEntriesParser :: Parser.Parser [TreeEntry]
treeEntriesParser = Parser.sepBy treeEntryParser (Parser.char '\NUL')


sh :: MonadIO m => Sh a -> m a
sh = shelly . silently . onCommandHandles (initOutputHandles (`hSetBinaryMode` True))
Expand Down
0