2013-10-20 21:50:51 +00:00
|
|
|
{- .git/objects
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-10-20 21:50:51 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-10-20 21:50:51 +00:00
|
|
|
-}
|
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2013-10-20 21:50:51 +00:00
|
|
|
module Git.Objects where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
2013-11-20 22:31:00 +00:00
|
|
|
import Git.Sha
|
2013-10-20 21:50:51 +00:00
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import qualified System.FilePath.ByteString as P
|
|
|
|
|
|
|
|
objectsDir :: Repo -> RawFilePath
|
|
|
|
objectsDir r = localGitDir r P.</> "objects"
|
2013-10-20 21:50:51 +00:00
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
packDir :: Repo -> RawFilePath
|
|
|
|
packDir r = objectsDir r P.</> "pack"
|
2013-10-20 21:50:51 +00:00
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
packIdxFile :: RawFilePath -> RawFilePath
|
|
|
|
packIdxFile = flip P.replaceExtension "idx"
|
2013-11-20 22:31:00 +00:00
|
|
|
|
2013-10-20 21:50:51 +00:00
|
|
|
listPackFiles :: Repo -> IO [FilePath]
|
|
|
|
listPackFiles r = filter (".pack" `isSuffixOf`)
|
2020-11-05 22:45:37 +00:00
|
|
|
<$> catchDefaultIO [] (dirContents $ fromRawFilePath $ packDir r)
|
2013-10-20 21:50:51 +00:00
|
|
|
|
2013-11-20 22:31:00 +00:00
|
|
|
listLooseObjectShas :: Repo -> IO [Sha]
|
|
|
|
listLooseObjectShas r = catchDefaultIO [] $
|
2020-04-06 21:14:49 +00:00
|
|
|
mapMaybe (extractSha . encodeBS . concat . reverse . take 2 . reverse . splitDirectories)
|
2023-08-15 16:57:41 +00:00
|
|
|
<$> emptyWhenDoesNotExist (dirContentsRecursiveSkipping (== "pack") True (fromRawFilePath (objectsDir r)))
|
2013-10-20 21:50:51 +00:00
|
|
|
|
2020-11-05 22:45:37 +00:00
|
|
|
looseObjectFile :: Repo -> Sha -> RawFilePath
|
|
|
|
looseObjectFile r sha = objectsDir r P.</> prefix P.</> rest
|
2013-10-20 21:50:51 +00:00
|
|
|
where
|
2020-11-05 22:45:37 +00:00
|
|
|
(prefix, rest) = B.splitAt 2 (fromRef' sha)
|
2014-09-05 17:44:09 +00:00
|
|
|
|
|
|
|
listAlternates :: Repo -> IO [FilePath]
|
2020-11-05 22:45:37 +00:00
|
|
|
listAlternates r = catchDefaultIO [] $
|
|
|
|
lines <$> readFile (fromRawFilePath alternatesfile)
|
2014-09-05 17:44:09 +00:00
|
|
|
where
|
2020-11-05 22:45:37 +00:00
|
|
|
alternatesfile = objectsDir r P.</> "info" P.</> "alternates"
|
2014-09-05 17:44:09 +00:00
|
|
|
|
|
|
|
{- A repository recently cloned with --shared will have one or more
|
|
|
|
- alternates listed, and contain no loose objects or packs. -}
|
|
|
|
isSharedClone :: Repo -> IO Bool
|
|
|
|
isSharedClone r = allM id
|
|
|
|
[ not . null <$> listAlternates r
|
|
|
|
, null <$> listLooseObjectShas r
|
|
|
|
, null <$> listPackFiles r
|
|
|
|
]
|