diff --git a/Key.hs b/Key.hs index 7d6dfa3af2..89f168be11 100644 --- a/Key.hs +++ b/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'