module Data.Git.Ref
( Ref
, RefInvalid(..)
, RefNotFound(..)
, isHex
, isHexString
, fromHex
, fromHexString
, fromBinary
, fromDigest
, toBinary
, toHex
, toHexString
, refPrefix
, cmpPrefix
, toFilePathParts
, hash
, hashLBS
) where
import qualified Crypto.Hash
import Crypto.Hash (Digest, SHA1, digestFromByteString)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Unsafe as B (unsafeIndex)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as BC
import Data.ByteArray.Encoding
import qualified Data.ByteArray as B (convert)
import Data.Char (isHexDigit)
import Data.Data
import Control.Exception (Exception, throw)
newtype Ref = Ref (Digest SHA1)
deriving (Eq,Ord,Typeable)
instance Show Ref where
show = BC.unpack . toHex
data RefInvalid = RefInvalid ByteString
deriving (Show,Eq,Data,Typeable)
data RefNotFound = RefNotFound Ref
deriving (Show,Eq,Typeable)
instance Exception RefInvalid
instance Exception RefNotFound
isHex :: ByteString -> Bool
isHex = and . map isHexDigit . BC.unpack
isHexString :: String -> Bool
isHexString = and . map isHexDigit
fromHex :: ByteString -> Ref
fromHex s =
case either (const Nothing) Just (convertFromBase Base16 s :: Either String ByteString) >>= digestFromByteString of
Nothing -> throw $ RefInvalid s
Just hsh -> Ref hsh
fromHexString :: String -> Ref
fromHexString = fromHex . BC.pack
toHex :: Ref -> ByteString
toHex (Ref bs) = convertToBase Base16 bs
toHexString :: Ref -> String
toHexString (Ref d) = show d
fromBinary :: ByteString -> Ref
fromBinary b = maybe (throw $ RefInvalid b) Ref $ digestFromByteString b
fromDigest :: Digest SHA1 -> Ref
fromDigest = Ref
toBinary :: Ref -> ByteString
toBinary (Ref b) = B.convert b
refPrefix :: Ref -> Int
refPrefix (Ref b) = fromIntegral $ B.unsafeIndex (B.convert b) 0
cmpPrefix :: String -> Ref -> Ordering
cmpPrefix pre ref = pre `compare` (take (length pre) $ toHexString ref)
toFilePathParts :: Ref -> (String, String)
toFilePathParts ref = splitAt 2 $ show ref
hash :: ByteString -> Ref
hash = Ref . Crypto.Hash.hash
hashLBS :: L.ByteString -> Ref
hashLBS = Ref . Crypto.Hash.hashlazy