File: Objects.hs

package info (click to toggle)
git-annex 8.20210223-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 68,764 kB
  • sloc: haskell: 70,359; javascript: 9,103; sh: 1,304; makefile: 212; perl: 136; ansic: 44
file content (55 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{- .git/objects
 -
 - Copyright 2013 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

{-# LANGUAGE OverloadedStrings #-}

module Git.Objects where

import Common
import Git
import Git.Sha

import qualified Data.ByteString as B
import qualified System.FilePath.ByteString as P

objectsDir :: Repo -> RawFilePath
objectsDir r = localGitDir r P.</> "objects"

packDir :: Repo -> RawFilePath
packDir r = objectsDir r P.</> "pack"

packIdxFile :: RawFilePath -> RawFilePath
packIdxFile = flip P.replaceExtension "idx"

listPackFiles :: Repo -> IO [FilePath]
listPackFiles r = filter (".pack" `isSuffixOf`) 
	<$> catchDefaultIO [] (dirContents $ fromRawFilePath $ packDir r)

listLooseObjectShas :: Repo -> IO [Sha]
listLooseObjectShas r = catchDefaultIO [] $
	mapMaybe (extractSha . encodeBS . concat . reverse . take 2 . reverse . splitDirectories)
		<$> dirContentsRecursiveSkipping (== "pack") True (fromRawFilePath (objectsDir r))

looseObjectFile :: Repo -> Sha -> RawFilePath
looseObjectFile r sha = objectsDir r P.</> prefix P.</> rest
  where
	(prefix, rest) = B.splitAt 2 (fromRef' sha)

listAlternates :: Repo -> IO [FilePath]
listAlternates r = catchDefaultIO [] $
	lines <$> readFile (fromRawFilePath alternatesfile)
  where
	alternatesfile = objectsDir r P.</> "info" P.</> "alternates"

{- 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
	]