Skip to content

Commit

Permalink
idx: search all idx files for a given sha
Browse files Browse the repository at this point in the history
  • Loading branch information
kfish committed May 10, 2011
1 parent 41ffcc3 commit 35f7598
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 18 additions & 3 deletions Git/PackIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Git.PackIndex (
dumpRawPackIndex,
findInPackIndex,
findInPackIdxs,

-- * Paths
idxPath
Expand All @@ -17,6 +17,7 @@ import Data.Word (Word32)
import Foreign.Ptr
import Foreign.Storable
import Data.Storable.Endian
import System.Directory
import System.FilePath
import System.IO.MMap
import System.Posix.Types
Expand Down Expand Up @@ -100,6 +101,15 @@ outOfRange = error "Index out of range"

------------------------------------------------------------

idxFiles :: IO [FilePath]
idxFiles = do
packDir <- gitPath ("objects" </> "pack")
map (packDir </>) . filter isIdx <$> getDirectoryContents packDir
where
isIdx = (== ".idx") . takeExtension

------------------------------------------------------------

idxFind :: IDX -> BS.ByteString -> IO (Maybe (IDX, Int))
idxFind idx sha = idxFind' 0 (idxSize idx)
where
Expand All @@ -118,8 +128,13 @@ idxFind idx sha = idxFind' 0 (idxSize idx)
where
i = floor ((fromIntegral (lo + hi)) / 2.0)

findInPackIndex :: FilePath -> BS.ByteString -> IO ()
findInPackIndex fp sha = do
findInPackIdxs :: BS.ByteString -> IO ()
findInPackIdxs sha = do
idxs <- idxFiles
mapM_ (findInPackIndex' sha) idxs

findInPackIndex' :: BS.ByteString -> FilePath -> IO ()
findInPackIndex' sha fp = do
idx <- readIdx fp
m'i <- idxFind idx sha
case m'i of
Expand Down
9 changes: 4 additions & 5 deletions tools/ght.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,13 @@ ghtFindIdx = defCmd {
cmdName = "find-idx",
cmdHandler = ghtFindIdxHandler,
cmdCategory = "Blob management",
cmdShortDesc = "Find a SHA in a pack index",
cmdExamples = [("Find SHA1 333fff in pack-abcd.idx", "abcd 333fff")]
cmdShortDesc = "Find a SHA in any pack index",
cmdExamples = [("Find SHA1 333fff", "333fff")]
}

ghtFindIdxHandler = do
(fp:sha:_) <- appArgs
fp' <- liftIO $ fIdx [fp]
liftIO $ findInPackIndex fp' (readDigestBS sha)
(sha:_) <- appArgs
liftIO $ findInPackIdxs (readDigestBS sha)

------------------------------------------------------------
-- show-raw
Expand Down

0 comments on commit 35f7598

Please sign in to comment.