git-annex/UUID.hs

102 lines
2.4 KiB
Haskell
Raw Normal View History

2010-10-12 17:10:07 +00:00
{- git-annex uuids
-
- Each git repository used by git-annex has an annex.uuid setting that
- uniquely identifies that repository.
-
- UUIDs of remotes are cached in git config, using keys named
- remote.<name>.annex-uuid
-
2010-10-27 20:53:54 +00:00
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
2010-10-12 17:10:07 +00:00
-}
module UUID (
2010-10-12 22:06:34 +00:00
UUID,
2010-10-12 17:10:07 +00:00
getUUID,
getUncachedUUID,
2010-10-12 17:10:07 +00:00
prepUUID,
genUUID,
2010-10-16 20:15:31 +00:00
describeUUID,
uuidMap,
uuidLog
2010-10-12 17:10:07 +00:00
) where
2010-10-16 00:20:16 +00:00
import qualified Data.Map as M
2010-10-16 20:20:49 +00:00
2011-10-05 20:02:51 +00:00
import Common.Annex
import qualified Git
2011-10-04 04:40:47 +00:00
import qualified Annex.Branch
import Types.UUID
2011-08-20 20:11:42 +00:00
import qualified Build.SysConfig as SysConfig
import Config
2010-10-12 19:48:00 +00:00
2010-10-31 20:04:19 +00:00
configkey :: String
configkey = "annex.uuid"
2010-10-12 17:10:07 +00:00
{- Filename of uuid.log. -}
uuidLog :: FilePath
uuidLog = "uuid.log"
2010-10-12 17:10:07 +00:00
{- Generates a UUID. There is a library for this, but it's not packaged,
- so use the command line tool. -}
2010-10-16 00:20:16 +00:00
genUUID :: IO UUID
genUUID = liftIO $ pOpen ReadFromPipe command params $ \h -> hGetLine h
where
command = SysConfig.uuid
params = if command == "uuid"
-- request a random uuid be generated
then ["-m"]
-- uuidgen generates random uuid by default
else []
2010-10-12 17:10:07 +00:00
{- Looks up a repo's UUID. May return "" if none is known.
-}
2010-10-14 06:36:41 +00:00
getUUID :: Git.Repo -> Annex UUID
2010-10-14 01:28:47 +00:00
getUUID r = do
g <- gitRepo
2010-10-31 20:04:19 +00:00
let c = cached g
let u = getUncachedUUID r
2010-10-19 17:39:53 +00:00
if c /= u && u /= ""
then do
2010-10-31 20:04:19 +00:00
updatecache g u
return u
else return c
where
2010-10-31 20:04:19 +00:00
cached g = Git.configGet g cachekey ""
updatecache g u = when (g /= r) $ setConfig cachekey u
cachekey = "remote." ++ fromMaybe "" (Git.repoRemoteName r) ++ ".annex-uuid"
2010-10-12 17:10:07 +00:00
getUncachedUUID :: Git.Repo -> UUID
getUncachedUUID r = Git.configGet r configkey ""
2010-10-12 17:10:07 +00:00
{- Make sure that the repo has an annex.uuid setting. -}
2010-10-14 01:28:47 +00:00
prepUUID :: Annex ()
prepUUID = do
u <- getUUID =<< gitRepo
2010-10-28 16:40:05 +00:00
when ("" == u) $ do
uuid <- liftIO genUUID
setConfig configkey uuid
2010-10-14 17:49:45 +00:00
2010-10-16 00:20:16 +00:00
{- Records a description for a uuid in the uuidLog. -}
describeUUID :: UUID -> String -> Annex ()
2011-10-04 04:40:47 +00:00
describeUUID uuid desc = Annex.Branch.change uuidLog $
serialize . M.insert uuid desc . parse
2010-10-16 00:20:16 +00:00
where
serialize m = unlines $ map (\(u, d) -> u++" "++d) $ M.toList m
2010-10-14 03:18:58 +00:00
{- Read the uuidLog into a Map -}
2010-10-16 00:20:16 +00:00
uuidMap :: Annex (M.Map UUID String)
2011-10-04 04:40:47 +00:00
uuidMap = parse <$> Annex.Branch.get uuidLog
parse :: String -> M.Map UUID String
parse = M.fromList . map pair . lines
2010-10-16 00:20:16 +00:00
where
pair l
| null ws = ("", "")
| otherwise = (head ws, unwords $ drop 1 ws)
where
ws = words l