2011-02-03 22:55:12 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Map where
|
|
|
|
|
|
|
|
import Control.Exception.Extensible
|
2011-02-04 02:20:55 +00:00
|
|
|
import qualified Data.Map as M
|
2011-02-03 22:55:12 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-02-03 22:55:12 +00:00
|
|
|
import Command
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-12-14 19:30:14 +00:00
|
|
|
import qualified Git.Url
|
2011-12-13 19:05:07 +00:00
|
|
|
import qualified Git.Config
|
|
|
|
import qualified Git.Construct
|
2011-12-20 20:31:59 +00:00
|
|
|
import qualified Annex
|
2011-10-15 21:47:03 +00:00
|
|
|
import Annex.UUID
|
2011-10-15 20:21:08 +00:00
|
|
|
import Logs.UUID
|
|
|
|
import Logs.Trust
|
2011-10-16 04:04:26 +00:00
|
|
|
import Annex.Ssh
|
2011-07-06 00:36:43 +00:00
|
|
|
import qualified Utility.Dot as Dot
|
2011-02-03 22:55:12 +00:00
|
|
|
|
|
|
|
-- a link from the first repository to the second (its remote)
|
|
|
|
data Link = Link Git.Repo Git.Repo
|
|
|
|
|
2011-10-29 19:19:05 +00:00
|
|
|
def :: [Command]
|
|
|
|
def = [dontCheck repoExists $
|
|
|
|
command "map" paramNothing seek "generate map of repositories"]
|
2011-02-03 22:55:12 +00:00
|
|
|
|
|
|
|
seek :: [CommandSeek]
|
|
|
|
seek = [withNothing start]
|
|
|
|
|
2011-09-15 20:50:49 +00:00
|
|
|
start :: CommandStart
|
2011-02-03 22:55:12 +00:00
|
|
|
start = do
|
2011-11-08 19:34:10 +00:00
|
|
|
rs <- spider =<< gitRepo
|
2011-02-03 22:55:12 +00:00
|
|
|
|
2011-02-04 02:20:55 +00:00
|
|
|
umap <- uuidMap
|
2011-02-08 22:04:19 +00:00
|
|
|
trusted <- trustGet Trusted
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-02-08 22:04:19 +00:00
|
|
|
liftIO $ writeFile file (drawMap rs umap trusted)
|
2011-12-20 20:31:59 +00:00
|
|
|
next $ next $ do
|
|
|
|
fast <- Annex.getState Annex.fast
|
|
|
|
if fast
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
showLongNote $ "running: dot -Tx11 " ++ file
|
|
|
|
showOutput
|
|
|
|
liftIO $ boolSystem "dot" [Param "-Tx11", File file]
|
2011-02-03 22:55:12 +00:00
|
|
|
where
|
|
|
|
file = "map.dot"
|
|
|
|
|
2011-02-04 02:20:55 +00:00
|
|
|
{- Generates a graph for dot(1). Each repository, and any other uuids, are
|
|
|
|
- displayed as a node, and each of its remotes is represented as an edge
|
2011-02-04 04:13:47 +00:00
|
|
|
- pointing at the node for the remote.
|
|
|
|
-
|
|
|
|
- The order nodes are added to the graph matters, since dot will draw
|
|
|
|
- the first ones near to the top and left. So it looks better to put
|
|
|
|
- the repositories first, followed by uuids that were not matched
|
|
|
|
- to a repository.
|
|
|
|
-}
|
2011-07-15 16:47:14 +00:00
|
|
|
drawMap :: [Git.Repo] -> M.Map UUID String -> [UUID] -> String
|
2011-02-08 22:04:19 +00:00
|
|
|
drawMap rs umap ts = Dot.graph $ repos ++ trusted ++ others
|
2011-02-04 02:20:55 +00:00
|
|
|
where
|
2011-02-04 03:23:16 +00:00
|
|
|
repos = map (node umap rs) rs
|
2011-02-08 22:04:19 +00:00
|
|
|
ruuids = ts ++ map getUncachedUUID rs
|
|
|
|
others = map (unreachable . uuidnode) $
|
|
|
|
filter (`notElem` ruuids) (M.keys umap)
|
|
|
|
trusted = map (trustworthy . uuidnode) ts
|
2011-11-08 03:21:22 +00:00
|
|
|
uuidnode u = Dot.graphNode (fromUUID u) $
|
|
|
|
M.findWithDefault "" u umap
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-02-04 03:23:16 +00:00
|
|
|
hostname :: Git.Repo -> String
|
|
|
|
hostname r
|
2011-12-14 19:30:14 +00:00
|
|
|
| Git.repoIsUrl r = Git.Url.host r
|
2011-02-04 03:23:16 +00:00
|
|
|
| otherwise = "localhost"
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-02-04 03:23:16 +00:00
|
|
|
basehostname :: Git.Repo -> String
|
2011-12-15 22:11:42 +00:00
|
|
|
basehostname r = Prelude.head $ split "." $ hostname r
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-02-04 03:23:16 +00:00
|
|
|
{- A name to display for a repo. Uses the name from uuid.log if available,
|
|
|
|
- or the remote name if not. -}
|
2011-07-15 16:47:14 +00:00
|
|
|
repoName :: M.Map UUID String -> Git.Repo -> String
|
2011-02-04 02:20:55 +00:00
|
|
|
repoName umap r
|
2011-11-07 18:46:01 +00:00
|
|
|
| repouuid == NoUUID = fallback
|
2011-02-04 02:20:55 +00:00
|
|
|
| otherwise = M.findWithDefault fallback repouuid umap
|
|
|
|
where
|
|
|
|
repouuid = getUncachedUUID r
|
2011-12-14 19:30:14 +00:00
|
|
|
fallback = fromMaybe "unknown" $ Git.remoteName r
|
2011-02-04 02:20:55 +00:00
|
|
|
|
2011-02-04 04:06:23 +00:00
|
|
|
{- A unique id for the node for a repo. Uses the annex.uuid if available. -}
|
2011-02-04 03:23:16 +00:00
|
|
|
nodeId :: Git.Repo -> String
|
|
|
|
nodeId r =
|
2011-07-15 16:47:14 +00:00
|
|
|
case getUncachedUUID r of
|
2011-11-07 18:46:01 +00:00
|
|
|
NoUUID -> Git.repoLocation r
|
|
|
|
UUID u -> u
|
2011-02-04 03:23:16 +00:00
|
|
|
|
|
|
|
{- A node representing a repo. -}
|
2011-07-15 16:47:14 +00:00
|
|
|
node :: M.Map UUID String -> [Git.Repo] -> Git.Repo -> String
|
2011-02-04 03:23:16 +00:00
|
|
|
node umap fullinfo r = unlines $ n:edges
|
|
|
|
where
|
2011-02-08 22:26:38 +00:00
|
|
|
n = Dot.subGraph (hostname r) (basehostname r) "lightblue" $
|
2011-02-04 04:06:23 +00:00
|
|
|
decorate $ Dot.graphNode (nodeId r) (repoName umap r)
|
2011-02-04 03:23:16 +00:00
|
|
|
edges = map (edge umap fullinfo r) (Git.remotes r)
|
2011-02-04 04:06:23 +00:00
|
|
|
decorate
|
2011-12-14 19:30:14 +00:00
|
|
|
| Git.config r == M.empty = unreachable
|
2011-02-04 04:06:23 +00:00
|
|
|
| otherwise = reachable
|
2011-02-04 03:23:16 +00:00
|
|
|
|
|
|
|
{- An edge between two repos. The second repo is a remote of the first. -}
|
2011-07-15 16:47:14 +00:00
|
|
|
edge :: M.Map UUID String -> [Git.Repo] -> Git.Repo -> Git.Repo -> String
|
2011-02-04 03:23:16 +00:00
|
|
|
edge umap fullinfo from to =
|
2011-02-08 21:46:52 +00:00
|
|
|
Dot.graphEdge (nodeId from) (nodeId fullto) edgename
|
2011-02-04 03:23:16 +00:00
|
|
|
where
|
|
|
|
-- get the full info for the remote, to get its UUID
|
2011-02-08 21:52:32 +00:00
|
|
|
fullto = findfullinfo to
|
2011-02-04 03:23:16 +00:00
|
|
|
findfullinfo n =
|
2011-07-15 16:47:14 +00:00
|
|
|
case filter (same n) fullinfo of
|
2011-02-04 03:23:16 +00:00
|
|
|
[] -> n
|
|
|
|
(n':_) -> n'
|
|
|
|
{- Only name an edge if the name is different than the name
|
|
|
|
- that will be used for the destination node, and is
|
|
|
|
- different from its hostname. (This reduces visual clutter.) -}
|
2011-12-14 19:30:14 +00:00
|
|
|
edgename = maybe Nothing calcname $ Git.remoteName to
|
2011-05-15 06:49:43 +00:00
|
|
|
calcname n
|
2011-07-15 16:47:14 +00:00
|
|
|
| n `elem` [repoName umap fullto, hostname fullto] = Nothing
|
2011-05-15 06:49:43 +00:00
|
|
|
| otherwise = Just n
|
2011-02-04 03:23:16 +00:00
|
|
|
|
2011-02-04 04:06:23 +00:00
|
|
|
unreachable :: String -> String
|
2011-02-04 04:13:47 +00:00
|
|
|
unreachable = Dot.fillColor "red"
|
2011-02-04 04:06:23 +00:00
|
|
|
reachable :: String -> String
|
2011-02-04 04:13:47 +00:00
|
|
|
reachable = Dot.fillColor "white"
|
2011-02-08 22:04:19 +00:00
|
|
|
trustworthy :: String -> String
|
|
|
|
trustworthy = Dot.fillColor "green"
|
2011-02-04 04:06:23 +00:00
|
|
|
|
2011-02-03 22:55:12 +00:00
|
|
|
{- Recursively searches out remotes starting with the specified repo. -}
|
|
|
|
spider :: Git.Repo -> Annex [Git.Repo]
|
|
|
|
spider r = spider' [r] []
|
|
|
|
spider' :: [Git.Repo] -> [Git.Repo] -> Annex [Git.Repo]
|
|
|
|
spider' [] known = return known
|
|
|
|
spider' (r:rs) known
|
|
|
|
| any (same r) known = spider' rs known
|
|
|
|
| otherwise = do
|
|
|
|
r' <- scan r
|
2011-02-08 21:52:32 +00:00
|
|
|
|
|
|
|
-- The remotes will be relative to r', and need to be
|
|
|
|
-- made absolute for later use.
|
2011-12-04 16:23:10 +00:00
|
|
|
remotes <- mapM (absRepo r') (Git.remotes r')
|
2011-12-14 19:30:14 +00:00
|
|
|
let r'' = r' { Git.remotes = remotes }
|
2011-02-08 21:52:32 +00:00
|
|
|
|
|
|
|
spider' (rs ++ remotes) (r'':known)
|
2011-02-03 22:55:12 +00:00
|
|
|
|
2011-12-04 16:23:10 +00:00
|
|
|
{- Converts repos to a common absolute form. -}
|
|
|
|
absRepo :: Git.Repo -> Git.Repo -> Annex Git.Repo
|
2011-02-04 05:56:45 +00:00
|
|
|
absRepo reference r
|
2011-12-13 19:05:07 +00:00
|
|
|
| Git.repoIsUrl reference = return $ Git.Construct.localToUrl reference r
|
|
|
|
| otherwise = liftIO $ Git.Construct.fromAbsPath =<< absPath (Git.workTree r)
|
2011-02-03 22:55:12 +00:00
|
|
|
|
|
|
|
{- Checks if two repos are the same. -}
|
|
|
|
same :: Git.Repo -> Git.Repo -> Bool
|
|
|
|
same a b
|
2011-12-14 19:30:14 +00:00
|
|
|
| both Git.repoIsSsh = matching Git.Url.authority && matching Git.workTree
|
2011-02-03 22:55:12 +00:00
|
|
|
| both Git.repoIsUrl && neither Git.repoIsSsh = matching show
|
2011-02-04 03:23:36 +00:00
|
|
|
| neither Git.repoIsSsh = matching Git.workTree
|
2011-02-03 22:55:12 +00:00
|
|
|
| otherwise = False
|
|
|
|
|
|
|
|
where
|
|
|
|
matching t = t a == t b
|
|
|
|
both t = t a && t b
|
|
|
|
neither t = not (t a) && not (t b)
|
|
|
|
|
|
|
|
{- reads the config of a remote, with progress display -}
|
|
|
|
scan :: Git.Repo -> Annex Git.Repo
|
|
|
|
scan r = do
|
2011-02-04 03:23:16 +00:00
|
|
|
showStart "map" $ Git.repoDescribe r
|
2011-02-03 22:55:12 +00:00
|
|
|
v <- tryScan r
|
|
|
|
case v of
|
|
|
|
Just r' -> do
|
|
|
|
showEndOk
|
|
|
|
return r'
|
|
|
|
Nothing -> do
|
2011-07-19 18:07:23 +00:00
|
|
|
showOutput
|
2011-02-03 22:55:12 +00:00
|
|
|
showEndFail
|
|
|
|
return r
|
|
|
|
|
|
|
|
{- tries to read the config of a remote, returning it only if it can
|
|
|
|
- be accessed -}
|
|
|
|
tryScan :: Git.Repo -> Annex (Maybe Git.Repo)
|
|
|
|
tryScan r
|
|
|
|
| Git.repoIsSsh r = sshscan
|
|
|
|
| Git.repoIsUrl r = return Nothing
|
2011-12-13 19:05:07 +00:00
|
|
|
| otherwise = safely $ Git.Config.read r
|
2011-02-03 22:55:12 +00:00
|
|
|
where
|
|
|
|
safely a = do
|
2011-07-15 16:47:14 +00:00
|
|
|
result <- liftIO (try a :: IO (Either SomeException Git.Repo))
|
2011-02-03 22:55:12 +00:00
|
|
|
case result of
|
|
|
|
Left _ -> return Nothing
|
|
|
|
Right r' -> return $ Just r'
|
|
|
|
pipedconfig cmd params = safely $
|
2011-02-28 20:25:31 +00:00
|
|
|
pOpen ReadFromPipe cmd (toCommand params) $
|
2011-12-13 19:05:07 +00:00
|
|
|
Git.Config.hRead r
|
2011-02-03 22:55:12 +00:00
|
|
|
|
|
|
|
configlist =
|
2011-04-09 18:26:32 +00:00
|
|
|
onRemote r (pipedconfig, Nothing) "configlist" []
|
2011-02-03 22:55:12 +00:00
|
|
|
manualconfiglist = do
|
2011-03-05 19:47:00 +00:00
|
|
|
sshparams <- sshToRepo r [Param sshcmd]
|
|
|
|
liftIO $ pipedconfig "ssh" sshparams
|
2011-11-11 20:18:41 +00:00
|
|
|
where
|
|
|
|
sshcmd = cddir ++ " && " ++
|
2011-12-15 16:46:04 +00:00
|
|
|
"git config --null --list"
|
2011-11-11 20:18:41 +00:00
|
|
|
dir = Git.workTree r
|
|
|
|
cddir
|
2011-12-09 05:57:13 +00:00
|
|
|
| "/~" `isPrefixOf` dir =
|
2011-11-11 20:18:41 +00:00
|
|
|
let (userhome, reldir) = span (/= '/') (drop 1 dir)
|
|
|
|
in "cd " ++ userhome ++ " && cd " ++ shellEscape (drop 1 reldir)
|
|
|
|
| otherwise = "cd " ++ shellEscape dir
|
2011-02-03 22:55:12 +00:00
|
|
|
|
|
|
|
-- First, try sshing and running git config manually,
|
|
|
|
-- only fall back to git-annex-shell configlist if that
|
|
|
|
-- fails.
|
|
|
|
--
|
|
|
|
-- This is done for two reasons, first I'd like this
|
|
|
|
-- subcommand to be usable on non-git-annex repos.
|
|
|
|
-- Secondly, configlist doesn't include information about
|
|
|
|
-- the remote's remotes.
|
|
|
|
sshscan = do
|
2011-02-04 05:56:45 +00:00
|
|
|
sshnote
|
2011-02-03 22:55:12 +00:00
|
|
|
v <- manualconfiglist
|
|
|
|
case v of
|
2011-02-04 05:56:45 +00:00
|
|
|
Nothing -> do
|
|
|
|
sshnote
|
|
|
|
configlist
|
2011-02-03 22:55:12 +00:00
|
|
|
ok -> return ok
|
2011-02-04 05:56:45 +00:00
|
|
|
|
|
|
|
sshnote = do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "sshing"
|
|
|
|
showOutput
|