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.
|
|
|
|
-
|
2011-03-27 20:55:43 +00:00
|
|
|
- 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,
|
2011-02-03 22:55:12 +00:00
|
|
|
getUncachedUUID,
|
2010-10-12 17:10:07 +00:00
|
|
|
prepUUID,
|
2010-10-13 19:55:18 +00:00
|
|
|
genUUID,
|
2010-10-16 00:20:16 +00:00
|
|
|
prettyPrintUUIDs,
|
2010-10-16 20:15:31 +00:00
|
|
|
describeUUID,
|
2011-06-22 21:08:51 +00:00
|
|
|
uuidMap,
|
|
|
|
uuidLog
|
2010-10-12 17:10:07 +00:00
|
|
|
) where
|
|
|
|
|
2010-10-14 01:28:47 +00:00
|
|
|
import Control.Monad.State
|
2010-10-12 17:10:07 +00:00
|
|
|
import System.Cmd.Utils
|
|
|
|
import System.IO
|
2010-10-16 00:20:16 +00:00
|
|
|
import qualified Data.Map as M
|
2011-02-04 02:20:55 +00:00
|
|
|
import Data.Maybe
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-06-22 20:02:22 +00:00
|
|
|
import qualified Branch
|
2010-10-14 07:18:11 +00:00
|
|
|
import Types
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.UUID
|
2010-10-14 07:18:11 +00:00
|
|
|
import qualified Annex
|
2011-01-19 22:08:50 +00:00
|
|
|
import qualified SysConfig
|
2011-03-28 01:43:25 +00:00
|
|
|
import Config
|
2010-10-12 19:48:00 +00:00
|
|
|
|
2010-10-31 20:04:19 +00:00
|
|
|
configkey :: String
|
2011-03-27 20:17:56 +00:00
|
|
|
configkey = "annex.uuid"
|
2010-10-12 17:10:07 +00:00
|
|
|
|
2011-06-22 21:08:51 +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
|
2011-01-19 22:08:50 +00:00
|
|
|
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
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- Looks up a repo's UUID. May return "" if none is known.
|
2011-03-27 20:55:43 +00:00
|
|
|
-}
|
2010-10-14 06:36:41 +00:00
|
|
|
getUUID :: Git.Repo -> Annex UUID
|
2010-10-14 01:28:47 +00:00
|
|
|
getUUID r = do
|
2010-10-14 17:49:45 +00:00
|
|
|
g <- Annex.gitRepo
|
2010-10-14 18:22:05 +00:00
|
|
|
|
2010-10-31 20:04:19 +00:00
|
|
|
let c = cached g
|
2011-02-03 22:55:12 +00:00
|
|
|
let u = getUncachedUUID r
|
2010-10-19 17:39:53 +00:00
|
|
|
|
2010-11-22 19:46:57 +00:00
|
|
|
if c /= u && u /= ""
|
2010-10-14 18:22:05 +00:00
|
|
|
then do
|
2010-10-31 20:04:19 +00:00
|
|
|
updatecache g u
|
2010-10-14 18:22:05 +00:00
|
|
|
return u
|
|
|
|
else return c
|
2010-10-13 19:55:18 +00:00
|
|
|
where
|
2010-10-31 20:04:19 +00:00
|
|
|
cached g = Git.configGet g cachekey ""
|
2011-03-28 01:43:25 +00:00
|
|
|
updatecache g u = when (g /= r) $ setConfig cachekey u
|
2011-02-04 02:20:55 +00:00
|
|
|
cachekey = "remote." ++ fromMaybe "" (Git.repoRemoteName r) ++ ".annex-uuid"
|
2010-10-12 17:10:07 +00:00
|
|
|
|
2011-02-03 22:55:12 +00:00
|
|
|
getUncachedUUID :: Git.Repo -> UUID
|
2011-03-27 20:17:56 +00:00
|
|
|
getUncachedUUID r = Git.configGet r configkey ""
|
2011-02-03 22:55:12 +00:00
|
|
|
|
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
|
2011-05-15 19:27:49 +00:00
|
|
|
u <- getUUID =<< Annex.gitRepo
|
2010-10-28 16:40:05 +00:00
|
|
|
when ("" == u) $ do
|
|
|
|
uuid <- liftIO $ genUUID
|
2011-03-28 01:43:25 +00:00
|
|
|
setConfig configkey uuid
|
2010-10-14 17:49:45 +00:00
|
|
|
|
2010-10-16 00:20:16 +00:00
|
|
|
{- Pretty-prints a list of UUIDs -}
|
2010-10-15 23:32:56 +00:00
|
|
|
prettyPrintUUIDs :: [UUID] -> Annex String
|
2010-10-16 00:20:16 +00:00
|
|
|
prettyPrintUUIDs uuids = do
|
2011-05-15 19:27:49 +00:00
|
|
|
here <- getUUID =<< Annex.gitRepo
|
2010-10-16 00:20:16 +00:00
|
|
|
m <- uuidMap
|
2011-03-05 21:33:57 +00:00
|
|
|
return $ unwords $ map (\u -> "\t" ++ prettify m u here ++ "\n") uuids
|
2010-10-16 00:20:16 +00:00
|
|
|
where
|
2011-03-05 21:33:57 +00:00
|
|
|
prettify m u here = base ++ ishere
|
|
|
|
where
|
|
|
|
base = if not $ null $ findlog m u
|
|
|
|
then u ++ " -- " ++ findlog m u
|
|
|
|
else u
|
|
|
|
ishere = if here == u then " <-- here" else ""
|
2010-10-16 00:20:16 +00:00
|
|
|
findlog m u = M.findWithDefault "" u m
|
|
|
|
|
|
|
|
{- Records a description for a uuid in the uuidLog. -}
|
|
|
|
describeUUID :: UUID -> String -> Annex ()
|
|
|
|
describeUUID uuid desc = do
|
|
|
|
m <- uuidMap
|
|
|
|
let m' = M.insert uuid desc m
|
2011-06-22 20:02:22 +00:00
|
|
|
Branch.change uuidLog (serialize m')
|
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
|
|
|
|
2010-10-16 00:20:16 +00:00
|
|
|
{- Read and parse the uuidLog into a Map -}
|
|
|
|
uuidMap :: Annex (M.Map UUID String)
|
|
|
|
uuidMap = do
|
2011-06-22 20:02:22 +00:00
|
|
|
s <- Branch.get uuidLog
|
2010-11-22 19:46:57 +00:00
|
|
|
return $ M.fromList $ map pair $ lines s
|
2010-10-16 00:20:16 +00:00
|
|
|
where
|
|
|
|
pair l =
|
2010-11-22 19:46:57 +00:00
|
|
|
if 1 < length (words l)
|
|
|
|
then (head $ words l, unwords $ drop 1 $ words l)
|
2010-10-16 00:20:16 +00:00
|
|
|
else ("", "")
|