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.
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
|
|
|
|
module UUID (
|
2010-10-12 22:06:34 +00:00
|
|
|
UUID,
|
2010-10-12 17:10:07 +00:00
|
|
|
getUUID,
|
|
|
|
prepUUID,
|
2010-10-13 19:55:18 +00:00
|
|
|
genUUID,
|
2010-10-14 03:18:58 +00:00
|
|
|
reposByUUID,
|
2010-10-16 00:20:16 +00:00
|
|
|
prettyPrintUUIDs,
|
2010-10-16 20:15:31 +00:00
|
|
|
describeUUID,
|
|
|
|
uuidLog
|
2010-10-12 17:10:07 +00:00
|
|
|
) where
|
|
|
|
|
2010-10-14 01:28:47 +00:00
|
|
|
import Control.Monad.State
|
2010-10-13 19:55:18 +00:00
|
|
|
import Maybe
|
|
|
|
import List
|
2010-10-12 17:10:07 +00:00
|
|
|
import System.Cmd.Utils
|
|
|
|
import System.IO
|
2010-10-16 20:20:49 +00:00
|
|
|
import System.Directory
|
2010-10-16 00:20:16 +00:00
|
|
|
import qualified Data.Map as M
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-14 06:36:41 +00:00
|
|
|
import qualified GitRepo as Git
|
2010-10-14 07:18:11 +00:00
|
|
|
import Types
|
2010-10-16 00:20:16 +00:00
|
|
|
import Locations
|
2010-10-14 07:18:11 +00:00
|
|
|
import qualified Annex
|
2010-10-16 20:15:31 +00:00
|
|
|
import Utility
|
2010-10-12 17:10:07 +00:00
|
|
|
|
2010-10-12 19:48:00 +00:00
|
|
|
type UUID = String
|
|
|
|
|
2010-10-12 17:10:07 +00:00
|
|
|
configkey="annex.uuid"
|
|
|
|
|
|
|
|
{- 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
|
2010-10-14 01:28:47 +00:00
|
|
|
genUUID = liftIO $ pOpen ReadFromPipe "uuid" ["-m"] $ \h -> hGetLine h
|
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.
|
|
|
|
-
|
|
|
|
- UUIDs of remotes are cached in git config, using keys named
|
|
|
|
- remote.<name>.annex-uuid
|
|
|
|
-
|
|
|
|
- -}
|
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
|
|
|
|
|
|
|
let c = cached r g
|
|
|
|
let u = uncached r
|
|
|
|
|
|
|
|
if (c /= u && u /= "")
|
|
|
|
then do
|
|
|
|
updatecache g r u
|
|
|
|
return u
|
|
|
|
else return c
|
2010-10-13 19:55:18 +00:00
|
|
|
where
|
2010-10-14 17:49:45 +00:00
|
|
|
uncached r = Git.configGet r "annex.uuid" ""
|
|
|
|
cached r g = Git.configGet g (cachekey r) ""
|
2010-10-14 18:22:05 +00:00
|
|
|
updatecache g r u = do
|
|
|
|
if (g /= r)
|
|
|
|
then setConfig (cachekey r) u
|
2010-10-14 17:49:45 +00:00
|
|
|
else return ()
|
|
|
|
cachekey r = "remote." ++ (Git.repoRemoteName r) ++ ".annex-uuid"
|
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
|
2010-10-14 07:18:11 +00:00
|
|
|
g <- Annex.gitRepo
|
2010-10-14 01:28:47 +00:00
|
|
|
u <- getUUID g
|
|
|
|
if ("" == u)
|
2010-10-12 17:10:07 +00:00
|
|
|
then do
|
2010-10-16 00:20:16 +00:00
|
|
|
uuid <- liftIO $ genUUID
|
2010-10-14 17:49:45 +00:00
|
|
|
setConfig configkey uuid
|
2010-10-14 01:28:47 +00:00
|
|
|
else return ()
|
2010-10-13 19:55:18 +00:00
|
|
|
|
2010-10-14 17:49:45 +00:00
|
|
|
{- Changes a git config setting in both internal state and .git/config -}
|
|
|
|
setConfig :: String -> String -> Annex ()
|
|
|
|
setConfig key value = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ Git.run g ["config", key, value]
|
|
|
|
-- re-read git config and update the repo's state
|
|
|
|
g' <- liftIO $ Git.configRead g
|
|
|
|
Annex.gitRepoChange g'
|
|
|
|
return ()
|
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- Filters a list of repos to ones that have listed UUIDs. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
reposByUUID :: [Git.Repo] -> [UUID] -> Annex [Git.Repo]
|
2010-10-14 01:28:47 +00:00
|
|
|
reposByUUID repos uuids = do
|
|
|
|
filterM match repos
|
|
|
|
where
|
|
|
|
match r = do
|
|
|
|
u <- getUUID r
|
|
|
|
return $ isJust $ elemIndex u uuids
|
2010-10-14 03:18:58 +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
|
|
|
|
m <- uuidMap
|
2010-10-17 17:13:49 +00:00
|
|
|
return $ unwords $ map (\u -> "\t"++(prettify m u)++"\n") uuids
|
2010-10-16 00:20:16 +00:00
|
|
|
where
|
|
|
|
prettify m u =
|
|
|
|
if (0 < (length $ findlog m u))
|
|
|
|
then u ++ " -- " ++ (findlog m u)
|
|
|
|
else u
|
|
|
|
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
|
|
|
|
log <- uuidLog
|
2010-10-16 20:20:49 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir log)
|
2010-10-16 20:15:31 +00:00
|
|
|
liftIO $ withFileLocked log WriteMode (\h -> hPutStr h $ 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
|
|
|
|
log <- uuidLog
|
2010-10-16 20:15:31 +00:00
|
|
|
s <- liftIO $ catch
|
|
|
|
(withFileLocked log ReadMode $ \h -> hGetContentsStrict h)
|
|
|
|
(\error -> return "")
|
2010-10-16 00:20:16 +00:00
|
|
|
return $ M.fromList $ map (\l -> pair l) $ lines s
|
|
|
|
where
|
|
|
|
pair l =
|
|
|
|
if (1 < (length $ words l))
|
|
|
|
then ((words l) !! 0, unwords $ drop 1 $ words l)
|
|
|
|
else ("", "")
|
|
|
|
|
|
|
|
{- Filename of uuid.log. -}
|
|
|
|
uuidLog :: Annex String
|
|
|
|
uuidLog = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
return $ (gitStateDir g) ++ "uuid.log"
|