command output cleanup

This commit is contained in:
Joey Hess 2010-10-17 13:13:49 -04:00
parent 8f6e5da18f
commit 6d4fc0ca7e
4 changed files with 75 additions and 45 deletions

View file

@ -53,12 +53,13 @@ copyKeyFile key file = do
remotes <- Remotes.withKey key remotes <- Remotes.withKey key
if (0 == length remotes) if (0 == length remotes)
then cantfind then cantfind
else return () else trycopy remotes remotes
trycopy remotes remotes
where where
trycopy full [] = error $ "unable to get file with key: " ++ (keyFile key) ++ "\n" ++ trycopy full [] = do
"To get that file, need access to one of these remotes: " ++ showNote $
(Remotes.list full) "need access to one of these remotes: " ++
(Remotes.list full)
return False
trycopy full (r:rs) = do trycopy full (r:rs) = do
-- annexLocation needs the git config to have been -- annexLocation needs the git config to have been
-- read for a remote, so do that now, -- read for a remote, so do that now,
@ -67,6 +68,7 @@ copyKeyFile key file = do
case (result) of case (result) of
Nothing -> trycopy full rs Nothing -> trycopy full rs
Just r' -> do Just r' -> do
showNote $ "copying from " ++ (Git.repoDescribe r ) ++ "..."
result <- liftIO $ (try (copyFromRemote r' key file)::IO (Either SomeException ())) result <- liftIO $ (try (copyFromRemote r' key file)::IO (Either SomeException ()))
case (result) of case (result) of
Left err -> do Left err -> do
@ -77,17 +79,15 @@ copyKeyFile key file = do
g <- Annex.gitRepo g <- Annex.gitRepo
uuids <- liftIO $ keyLocations g key uuids <- liftIO $ keyLocations g key
ppuuids <- prettyPrintUUIDs uuids ppuuids <- prettyPrintUUIDs uuids
error $ "no available git remotes have file with key: " ++ showNote $ "No available git remotes have the file."
(keyFile key) ++ if (0 < length uuids)
if (0 < length uuids) then showLongNote $ "It has been seen before in these repositories:\n" ++ ppuuids
then "\nIt has been seen before in these repositories:\n" ++ ppuuids else return ()
else "" return False
{- Tries to copy a file from a remote, exception on error. -} {- Tries to copy a file from a remote, exception on error. -}
copyFromRemote :: Git.Repo -> Key -> FilePath -> IO () copyFromRemote :: Git.Repo -> Key -> FilePath -> IO ()
copyFromRemote r key file = do copyFromRemote r key file = do
putStrLn $ "copy from " ++ (Git.repoDescribe r ) ++ " " ++ file
if (Git.repoIsLocal r) if (Git.repoIsLocal r)
then getlocal then getlocal
else getremote else getremote
@ -116,9 +116,6 @@ checkRemoveKey key = do
then retNotEnoughCopiesKnown remotes numcopies then retNotEnoughCopiesKnown remotes numcopies
else findcopies numcopies remotes [] else findcopies numcopies remotes []
where where
failMsg w = do
liftIO $ hPutStrLn stderr $ "git-annex: " ++ w
return False -- failure, not enough copies found
findcopies 0 _ _ = return True -- success, enough copies found findcopies 0 _ _ = return True -- success, enough copies found
findcopies _ [] bad = notEnoughCopiesSeen bad findcopies _ [] bad = notEnoughCopiesSeen bad
findcopies n (r:rs) bad = do findcopies n (r:rs) bad = do
@ -134,21 +131,25 @@ checkRemoveKey key = do
a <- Annex.new r all a <- Annex.new r all
(result, _) <- Annex.run a (Backend.hasKey key) (result, _) <- Annex.run a (Backend.hasKey key)
return result return result
notEnoughCopiesSeen bad = failMsg $ notEnoughCopiesSeen bad = do
"I failed to find enough other copies of: " ++ showNote "failed to find enough other copies of the file"
(keyFile key) ++ if (0 /= length bad) then listbad bad else return ()
(if (0 /= length bad) then listbad bad else "") unsafe
++ unsafe return False
listbad bad = "\nI was unable to access these remotes: " ++ listbad bad =
(Remotes.list bad) showLongNote $
retNotEnoughCopiesKnown remotes numcopies = failMsg $ "I was unable to access these remotes: " ++
(Remotes.list bad)
retNotEnoughCopiesKnown remotes numcopies = do
showNote $
"I only know about " ++ (show $ length remotes) ++ "I only know about " ++ (show $ length remotes) ++
" out of " ++ (show numcopies) ++ " out of " ++ (show numcopies) ++
" necessary copies of: " ++ (keyFile key) ++ " necessary copies of the file"
unsafe unsafe
unsafe = "\n" ++ return False
" -- According to the " ++ config ++ unsafe = do
" setting, it is not safe to remove it!\n" ++ showLongNote $ "According to the " ++ config ++
" (Use --force to override.)" " setting, it is not safe to remove it!"
showLongNote "(Use --force to override.)"
config = "annex.numcopies" config = "annex.numcopies"

View file

@ -95,11 +95,11 @@ parseCmd argv state = do
addCmd :: FilePath -> Annex () addCmd :: FilePath -> Annex ()
addCmd file = inBackend file err $ do addCmd file = inBackend file err $ do
liftIO $ checkLegal file liftIO $ checkLegal file
liftIO $ putStrLn $ "add " ++ file showStart "add" file
g <- Annex.gitRepo g <- Annex.gitRepo
stored <- Backend.storeFileKey file stored <- Backend.storeFileKey file
case (stored) of case (stored) of
Nothing -> error $ "no backend could store: " ++ file Nothing -> showEndFail "no backend could store" file
Just (key, backend) -> do Just (key, backend) -> do
logStatus key ValuePresent logStatus key ValuePresent
setup g key setup g key
@ -117,11 +117,13 @@ addCmd file = inBackend file err $ do
link <- calcGitLink file key link <- calcGitLink file key
liftIO $ createSymbolicLink link file liftIO $ createSymbolicLink link file
gitAdd file $ Just $ "git-annex annexed " ++ file gitAdd file $ Just $ "git-annex annexed " ++ file
showEndOk
{- Undo addCmd. -} {- Undo addCmd. -}
unannexCmd :: FilePath -> Annex () unannexCmd :: FilePath -> Annex ()
unannexCmd file = notinBackend file err $ \(key, backend) -> do unannexCmd file = notinBackend file err $ \(key, backend) -> do
liftIO $ putStrLn $ "unannex " ++ file showStart "unannex" file
Annex.flagChange Force True -- force backend to always remove
Backend.removeKey backend key Backend.removeKey backend key
logStatus key ValueMissing logStatus key ValueMissing
g <- Annex.gitRepo g <- Annex.gitRepo
@ -132,16 +134,17 @@ unannexCmd file = notinBackend file err $ \(key, backend) -> do
moveout g src = do moveout g src = do
nocommit <- Annex.flagIsSet NoCommit nocommit <- Annex.flagIsSet NoCommit
liftIO $ removeFile file liftIO $ removeFile file
liftIO $ Git.run g ["rm", file] liftIO $ Git.run g ["rm", "--quiet", file]
if (not nocommit) if (not nocommit)
then liftIO $ Git.run g ["commit", "-m", then liftIO $ Git.run g ["commit", "--quiet",
("git-annex unannexed " ++ file), file] "-m", ("git-annex unannexed " ++ file),
file]
else return () else return ()
-- git rm deletes empty directories; -- git rm deletes empty directories;
-- put them back -- put them back
liftIO $ createDirectoryIfMissing True (parentDir file) liftIO $ createDirectoryIfMissing True (parentDir file)
liftIO $ renameFile src file liftIO $ renameFile src file
return () showEndOk
{- Gets an annexed file from one of the backends. -} {- Gets an annexed file from one of the backends. -}
getCmd :: FilePath -> Annex () getCmd :: FilePath -> Annex ()
@ -150,6 +153,7 @@ getCmd file = notinBackend file err $ \(key, backend) -> do
if (inannex) if (inannex)
then return () then return ()
else do else do
showStart "get" file
g <- Annex.gitRepo g <- Annex.gitRepo
let dest = annexLocation g key let dest = annexLocation g key
liftIO $ createDirectoryIfMissing True (parentDir dest) liftIO $ createDirectoryIfMissing True (parentDir dest)
@ -157,8 +161,8 @@ getCmd file = notinBackend file err $ \(key, backend) -> do
if (success) if (success)
then do then do
logStatus key ValuePresent logStatus key ValuePresent
return () showEndOk
else error $ "failed to get " ++ file else showEndFail "get" file
where where
err = error $ "not annexed " ++ file err = error $ "not annexed " ++ file
@ -170,11 +174,13 @@ dropCmd file = notinBackend file err $ \(key, backend) -> do
if (not inbackend) if (not inbackend)
then return () -- no-op then return () -- no-op
else do else do
liftIO $ putStrLn $ "drop " ++ file showStart "drop" file
success <- Backend.removeKey backend key success <- Backend.removeKey backend key
if (success) if (success)
then cleanup key then do
else error $ "backend refused to drop " ++ file cleanup key
showEndOk
else showEndFail "backend refused to drop" file
where where
cleanup key = do cleanup key = do
logStatus key ValueMissing logStatus key ValueMissing
@ -191,13 +197,14 @@ dropCmd file = notinBackend file err $ \(key, backend) -> do
{- Fixes the symlink to an annexed file. -} {- Fixes the symlink to an annexed file. -}
fixCmd :: String -> Annex () fixCmd :: String -> Annex ()
fixCmd file = notinBackend file err $ \(key, backend) -> do fixCmd file = notinBackend file err $ \(key, backend) -> do
liftIO $ putStrLn $ "fix " ++ file
link <- calcGitLink file key link <- calcGitLink file key
checkLegal file link checkLegal file link
showStart "fix" file
liftIO $ createDirectoryIfMissing True (parentDir file) liftIO $ createDirectoryIfMissing True (parentDir file)
liftIO $ removeFile file liftIO $ removeFile file
liftIO $ createSymbolicLink link file liftIO $ createSymbolicLink link file
gitAdd file $ Just $ "git-annex fix " ++ file gitAdd file $ Just $ "git-annex fix " ++ file
showEndOk
where where
checkLegal file link = do checkLegal file link = do
l <- liftIO $ readSymbolicLink file l <- liftIO $ readSymbolicLink file

26
Core.hs
View file

@ -7,6 +7,7 @@ import System.IO
import System.Directory import System.Directory
import Control.Monad.State (liftIO) import Control.Monad.State (liftIO)
import System.Path import System.Path
import Data.String.Utils
import Types import Types
import Locations import Locations
@ -81,8 +82,8 @@ gitAdd file commitmessage = do
g <- Annex.gitRepo g <- Annex.gitRepo
liftIO $ Git.run g ["add", file] liftIO $ Git.run g ["add", file]
if (isJust commitmessage) if (isJust commitmessage)
then liftIO $ Git.run g ["commit", "-m", then liftIO $ Git.run g ["commit", "--quiet",
(fromJust commitmessage), file] "-m", (fromJust commitmessage), file]
else Annex.flagChange NeedCommit True else Annex.flagChange NeedCommit True
{- Calculates the relative path to use to link a file to a key. -} {- Calculates the relative path to use to link a file to a key. -}
@ -104,3 +105,24 @@ logStatus key status = do
f <- liftIO $ logChange g key u status f <- liftIO $ logChange g key u status
gitAdd f Nothing -- all logs are committed at end gitAdd f Nothing -- all logs are committed at end
{- Output logging -}
showStart :: String -> String -> Annex ()
showStart command file = do
liftIO $ putStr $ command ++ " " ++ file
liftIO $ hFlush stdout
showNote :: String -> Annex ()
showNote s = do
liftIO $ putStr $ " (" ++ s ++ ")"
liftIO $ hFlush stdout
showLongNote :: String -> Annex ()
showLongNote s = do
liftIO $ putStr $ "\n" ++ (indent s)
where
indent s = join "\n" $ map (\l -> " " ++ l) $ lines s
showEndOk :: Annex ()
showEndOk = do
liftIO $ putStrLn " ok"
showEndFail :: String -> String -> Annex ()
showEndFail command file = do
liftIO $ putStrLn ""
error $ command ++ " " ++ file ++ " failed"

View file

@ -100,7 +100,7 @@ reposByUUID repos uuids = do
prettyPrintUUIDs :: [UUID] -> Annex String prettyPrintUUIDs :: [UUID] -> Annex String
prettyPrintUUIDs uuids = do prettyPrintUUIDs uuids = do
m <- uuidMap m <- uuidMap
return $ unwords $ map (\u -> " "++(prettify m u)++"\n") uuids return $ unwords $ map (\u -> "\t"++(prettify m u)++"\n") uuids
where where
prettify m u = prettify m u =
if (0 < (length $ findlog m u)) if (0 < (length $ findlog m u))