encoding a git sha as a git-annex key
This is a bijective mapping, and is distinct from SHA1. As git transitions away from sha1, this could contain whatever hash git uses.
This commit is contained in:
parent
d045e39058
commit
1c1edad620
1 changed files with 21 additions and 0 deletions
21
Key.hs
21
Key.hs
|
@ -5,6 +5,7 @@
|
|||
- Licensed under the GNU AGPL version 3 or higher.
|
||||
-}
|
||||
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||
|
||||
module Key (
|
||||
|
@ -22,6 +23,8 @@ module Key (
|
|||
nonChunkKey,
|
||||
chunkKeyOffset,
|
||||
isChunkKey,
|
||||
gitShaKey,
|
||||
keyGitSha,
|
||||
isKeyPrefix,
|
||||
splitKeyNameExtension,
|
||||
|
||||
|
@ -35,6 +38,7 @@ import qualified Data.Attoparsec.ByteString as A
|
|||
|
||||
import Common
|
||||
import Types.Key
|
||||
import Git.Types
|
||||
import Utility.QuickCheck
|
||||
import Utility.Bloom
|
||||
import Utility.Aeson
|
||||
|
@ -58,6 +62,23 @@ chunkKeyOffset k = (*)
|
|||
isChunkKey :: Key -> Bool
|
||||
isChunkKey k = isJust (fromKey keyChunkSize k) && isJust (fromKey keyChunkNum k)
|
||||
|
||||
-- Encodes a git sha as a key.
|
||||
--
|
||||
-- This is not the same as a SHA1 key, because the mapping needs to be
|
||||
-- bijective, also because git may not always use SHA1.
|
||||
gitShaKey :: Sha -> Key
|
||||
gitShaKey (Ref s) = mkKey $ \kd -> kd
|
||||
{ keyName = s
|
||||
, keyVariety = OtherKey "GIT"
|
||||
}
|
||||
|
||||
-- Reverse of gitShaKey
|
||||
keyGitSha :: Key -> Maybe Sha
|
||||
keyGitSha k
|
||||
| fromKey keyVariety k == OtherKey "GIT" =
|
||||
Just (Ref (fromKey keyName k))
|
||||
| otherwise = Nothing
|
||||
|
||||
serializeKey :: Key -> String
|
||||
serializeKey = decodeBS' . serializeKey'
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue