2011-10-15 21:47:03 +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
|
|
|
|
-
|
|
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.UUID (
|
|
|
|
getUUID,
|
|
|
|
getRepoUUID,
|
|
|
|
getUncachedUUID,
|
|
|
|
prepUUID,
|
2012-04-27 16:21:38 +00:00
|
|
|
genUUID,
|
|
|
|
removeRepoUUID,
|
2011-10-15 21:47:03 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import qualified Git
|
2011-12-13 19:05:07 +00:00
|
|
|
import qualified Git.Config
|
2011-10-15 21:47:03 +00:00
|
|
|
import qualified Build.SysConfig as SysConfig
|
|
|
|
import Config
|
|
|
|
|
2012-05-06 00:15:32 +00:00
|
|
|
configkey :: ConfigKey
|
|
|
|
configkey = annexConfig "uuid"
|
2011-10-15 21:47:03 +00:00
|
|
|
|
|
|
|
{- Generates a UUID. There is a library for this, but it's not packaged,
|
|
|
|
- so use the command line tool. -}
|
|
|
|
genUUID :: IO UUID
|
2011-11-08 03:21:22 +00:00
|
|
|
genUUID = pOpen ReadFromPipe command params $ liftM toUUID . hGetLine
|
2011-10-15 21:47:03 +00:00
|
|
|
where
|
|
|
|
command = SysConfig.uuid
|
2012-03-14 21:43:34 +00:00
|
|
|
params
|
2011-10-15 21:47:03 +00:00
|
|
|
-- request a random uuid be generated
|
2012-03-14 21:43:34 +00:00
|
|
|
| command == "uuid" = ["-m"]
|
2011-10-15 21:47:03 +00:00
|
|
|
-- uuidgen generates random uuid by default
|
2012-03-14 21:43:34 +00:00
|
|
|
| otherwise = []
|
2011-10-15 21:47:03 +00:00
|
|
|
|
2011-11-19 19:40:40 +00:00
|
|
|
{- Get current repository's UUID. -}
|
2011-10-15 21:47:03 +00:00
|
|
|
getUUID :: Annex UUID
|
|
|
|
getUUID = getRepoUUID =<< gitRepo
|
|
|
|
|
2011-11-19 19:40:40 +00:00
|
|
|
{- Looks up a repo's UUID, caching it in .git/config if it's not already. -}
|
2011-10-15 21:47:03 +00:00
|
|
|
getRepoUUID :: Git.Repo -> Annex UUID
|
|
|
|
getRepoUUID r = do
|
2012-03-22 04:23:15 +00:00
|
|
|
c <- toUUID <$> getConfig cachekey ""
|
2011-10-15 21:47:03 +00:00
|
|
|
let u = getUncachedUUID r
|
|
|
|
|
2011-11-07 18:46:01 +00:00
|
|
|
if c /= u && u /= NoUUID
|
2011-10-15 21:47:03 +00:00
|
|
|
then do
|
2011-11-08 19:34:10 +00:00
|
|
|
updatecache u
|
2011-10-15 21:47:03 +00:00
|
|
|
return u
|
|
|
|
else return c
|
|
|
|
where
|
2011-11-08 19:34:10 +00:00
|
|
|
updatecache u = do
|
|
|
|
g <- gitRepo
|
|
|
|
when (g /= r) $ storeUUID cachekey u
|
2011-11-19 19:57:08 +00:00
|
|
|
cachekey = remoteConfig r "uuid"
|
2011-10-15 21:47:03 +00:00
|
|
|
|
2012-04-27 16:21:38 +00:00
|
|
|
removeRepoUUID :: Annex ()
|
2012-05-06 00:15:32 +00:00
|
|
|
removeRepoUUID = unsetConfig configkey
|
2012-04-27 16:21:38 +00:00
|
|
|
|
2011-10-15 21:47:03 +00:00
|
|
|
getUncachedUUID :: Git.Repo -> UUID
|
2012-05-06 00:15:32 +00:00
|
|
|
getUncachedUUID = toUUID . Git.Config.get key ""
|
|
|
|
where
|
|
|
|
(ConfigKey key) = configkey
|
2011-10-15 21:47:03 +00:00
|
|
|
|
|
|
|
{- Make sure that the repo has an annex.uuid setting. -}
|
|
|
|
prepUUID :: Annex ()
|
2011-11-07 18:46:01 +00:00
|
|
|
prepUUID = whenM ((==) NoUUID <$> getUUID) $
|
|
|
|
storeUUID configkey =<< liftIO genUUID
|
|
|
|
|
2012-05-06 00:15:32 +00:00
|
|
|
storeUUID :: ConfigKey -> UUID -> Annex ()
|
2011-11-08 03:21:22 +00:00
|
|
|
storeUUID configfield = setConfig configfield . fromUUID
|