<commit_before>module Duffer.Unified where

import Data.Attoparsec.ByteString (parseOnly)
import Data.ByteString            (init, readFile)
import Data.Bool                  (bool)
import Control.Monad.Trans.Maybe  (MaybeT(..), runMaybeT)
import Control.Applicative        ((<|>))
import System.FilePath            ((</>))

import Prelude hiding (init, readFile)

import Duffer.Loose
import Duffer.Loose.Objects (Ref, GitObject)
import Duffer.Pack
import Duffer.Loose.Parser  (parseSymRef)
import Duffer.WithRepo

readObject :: Ref -> WithRepo (Maybe GitObject)
readObject ref = runMaybeT $
    MaybeT (readLooseObject ref) <|> MaybeT (readPackObject ref)

writeObject :: GitObject -> WithRepo Ref
writeObject = writeLooseObject

resolveRef :: FilePath -> WithRepo (Maybe GitObject)
resolveRef refPath = readRef refPath >>= maybe (return Nothing) readObject

readRef :: FilePath -> WithRepo (Maybe Ref)
readRef refPath = runMaybeT $
    MaybeT (readLooseRef refPath) <|> MaybeT (readPackRef refPath)

readLooseRef :: FilePath -> WithRepo (Maybe Ref)
readLooseRef refPath = hasLooseRef refPath >>= bool
    (return Nothing)
    (do
        path    <- asks (</> refPath)
        content <- liftIO $ readFile path
        case parseOnly parseSymRef content of
            Right newPath -> readRef newPath
            Left _        -> return $ Just (init content))

updateRef :: FilePath -> GitObject -> WithRepo Ref
updateRef = updateLooseRef<commit_msg>Desugar.<commit_after>module Duffer.Unified where

import Data.Attoparsec.ByteString (parseOnly)
import Data.ByteString            (init, readFile)
import Data.Bool                  (bool)
import Control.Monad.Trans.Maybe  (MaybeT(..), runMaybeT)
import Control.Applicative        ((<|>))
import System.FilePath            ((</>))

import Prelude hiding (init, readFile)

import Duffer.Loose
import Duffer.Loose.Objects (Ref, GitObject)
import Duffer.Pack
import Duffer.Loose.Parser  (parseSymRef)
import Duffer.WithRepo

readObject :: Ref -> WithRepo (Maybe GitObject)
readObject ref = runMaybeT $
    MaybeT (readLooseObject ref) <|> MaybeT (readPackObject ref)

writeObject :: GitObject -> WithRepo Ref
writeObject = writeLooseObject

resolveRef :: FilePath -> WithRepo (Maybe GitObject)
resolveRef refPath = readRef refPath >>= maybe (return Nothing) readObject

readRef :: FilePath -> WithRepo (Maybe Ref)
readRef refPath = runMaybeT $
    MaybeT (readLooseRef refPath) <|> MaybeT (readPackRef refPath)

readLooseRef :: FilePath -> WithRepo (Maybe Ref)
readLooseRef refPath = hasLooseRef refPath >>= bool
    (return Nothing)
    (do
        content <- liftIO . readFile =<< asks (</> refPath)
        case parseOnly parseSymRef content of
            Right newPath -> readRef newPath
            Left _        -> return $ Just (init content))

updateRef :: FilePath -> GitObject -> WithRepo Ref
updateRef = updateLooseRef
{'fraction_non_alphanumeric': 0.0986453629732546, 'fraction_numerical': 0.0, 'mean_word_length': 3.521193092621664, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3788862', 'n_tokens_mistral': 857, 'n_tokens_neox': 859, 'n_words': 251}