Skip to content
Snippets Groups Projects
Difference.hs 2.64 KiB
Newer Older
{- git-annex repository differences
 -
 - Copyright 2015 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Types.Difference (
	Difference(..),
	Differences(..),
Joey Hess's avatar
Joey Hess committed
	readDifferences,
	getDifferences,
	differenceConfigKey,
	differenceConfigVal,
	hasDifference,
) where

Joey Hess's avatar
Joey Hess committed
import Utility.PartialPrelude
import qualified Git
import qualified Git.Config

import Data.List
import Data.Maybe
import Data.Monoid

-- Describes differences from the v5 repository format.
--
Joey Hess's avatar
Joey Hess committed
-- The serialization is stored in difference.log, so avoid changes that
Joey Hess's avatar
Joey Hess committed
-- Not breaking compatability is why a list of Differences is used, rather
-- than a sum type. With a sum type, adding a new field for some future
-- difference would serialize to a value that an older version could not
-- parse, even if that new field was not used. With the Differences list,
-- old versions can still parse it, unless the new Difference constructor 
-- is used.
Joey Hess's avatar
Joey Hess committed
--
-- The constructors intentionally do not have parameters; this is to
-- ensure that any Difference that can be expressed is supported.
-- So, a new repository version would be Version6, rather than Version Int.
Joey Hess's avatar
Joey Hess committed
	= ObjectHashLower
	| OneLevelObjectHash
	| OneLevelBranchHash
	deriving (Show, Read, Ord, Eq, Enum, Bounded)

data Differences
	= Differences [Difference]
	| UnknownDifferences

instance Eq Differences where
Joey Hess's avatar
Joey Hess committed
	Differences a == Differences b = canon a == canon b
	_ == _ = False -- UnknownDifferences cannot be equal

instance Monoid Differences where
	mempty = Differences []
Joey Hess's avatar
Joey Hess committed
	mappend (Differences l1) (Differences l2) = Differences (canon (l1 ++ l2))
Joey Hess's avatar
Joey Hess committed
-- Canonical form, allowing comparison.
Joey Hess's avatar
Joey Hess committed
canon :: [Difference] -> [Difference]
canon = nub . sort
Joey Hess's avatar
Joey Hess committed
readDifferences :: String -> Differences
readDifferences = maybe UnknownDifferences Differences . readish

Joey Hess's avatar
Joey Hess committed
getDifferences r = Differences $ catMaybes $
	map getmaybe [minBound .. maxBound]
Joey Hess's avatar
Joey Hess committed
	getmaybe d = case Git.Config.isTrue =<< Git.Config.getMaybe (differenceConfigKey d) r of
		Just True -> Just d
		_ -> Nothing
Joey Hess's avatar
Joey Hess committed
differenceConfigKey ObjectHashLower = tunable "objecthashlower"
differenceConfigKey OneLevelObjectHash = tunable "objecthash1"
differenceConfigKey OneLevelBranchHash = tunable "branchhash1"
Joey Hess's avatar
Joey Hess committed
differenceConfigVal _ = Git.Config.boolConfig True

tunable :: String -> String
tunable k = "annex.tune." ++ k

hasDifference :: (Difference -> Bool) -> Differences -> Bool
hasDifference f (Differences l) = any f l
hasDifference _ UnknownDifferences = False