git-annex/Commands.hs

222 lines
6.8 KiB
Haskell
Raw Normal View History

2010-10-14 18:38:29 +00:00
{- git-annex command line -}
2010-10-14 07:18:11 +00:00
2010-10-14 18:50:46 +00:00
module Commands (argvToActions) where
2010-10-14 07:18:11 +00:00
2010-10-14 18:38:29 +00:00
import System.Console.GetOpt
2010-10-14 07:18:11 +00:00
import Control.Monad.State (liftIO)
import System.Posix.Files
import System.Directory
import Data.String.Utils
import List
2010-10-14 21:37:20 +00:00
import IO
2010-10-14 07:18:11 +00:00
import qualified GitRepo as Git
import qualified Annex
import Utility
import Locations
import qualified Backend
import BackendList
import UUID
import LocationLog
import Types
2010-10-14 18:38:29 +00:00
import Core
2010-10-14 21:37:20 +00:00
import qualified Remotes
2010-10-14 18:38:29 +00:00
2010-10-14 18:49:19 +00:00
options :: [OptDescr (String -> Annex ())]
2010-10-14 18:38:29 +00:00
options =
2010-10-14 18:54:56 +00:00
[ Option ['a'] ["add"] (NoArg addCmd) "add files to annex"
, Option ['p'] ["push"] (NoArg pushCmd) "push annex to repos"
, Option ['P'] ["pull"] (NoArg pullCmd) "pull annex from repos"
, Option ['w'] ["want"] (NoArg wantCmd) "request file contents"
, Option ['g'] ["get"] (NoArg getCmd) "transfer file contents"
, Option ['d'] ["drop"] (NoArg dropCmd) "indicate file contents not needed"
2010-10-14 18:49:19 +00:00
, Option ['u'] ["unannex"] (NoArg unannexCmd) "undo --add"
2010-10-14 18:38:29 +00:00
]
2010-10-14 18:49:19 +00:00
{- Parses command line and returns a list of actons to be run in the Annex
- monad. -}
argvToActions :: [String] -> IO [Annex ()]
argvToActions argv = do
2010-10-14 18:38:29 +00:00
case getOpt Permute options argv of
2010-10-14 18:49:19 +00:00
([],files,[]) -> return $ map defaultCmd files
2010-10-14 18:38:29 +00:00
-- one mode is normal case
2010-10-14 18:49:19 +00:00
(m:[],files,[]) -> return $ map m files
2010-10-14 18:38:29 +00:00
-- multiple modes is an error
(ms,files,[]) -> ioError (userError ("only one mode should be specified\n" ++ usageInfo header options))
-- error case
(_,files,errs) -> ioError (userError (concat errs ++ usageInfo header options))
where header = "Usage: git-annex [mode] file"
2010-10-14 16:36:40 +00:00
{- Default mode is to annex a file if it is not already, and otherwise
- get its content. -}
defaultCmd :: FilePath -> Annex ()
defaultCmd file = do
r <- liftIO $ Backend.lookupFile file
case (r) of
Just v -> getCmd file
Nothing -> addCmd file
2010-10-14 07:18:11 +00:00
{- Annexes a file, storing it in a backend, and then moving it into
- the annex directory and setting up the symlink pointing to its content. -}
2010-10-14 16:36:40 +00:00
addCmd :: FilePath -> Annex ()
addCmd file = inBackend file err $ do
2010-10-14 07:18:11 +00:00
liftIO $ checkLegal file
2010-10-14 18:14:19 +00:00
stored <- Backend.storeFileKey file
2010-10-14 07:18:11 +00:00
g <- Annex.gitRepo
case (stored) of
Nothing -> error $ "no backend could store: " ++ file
Just (key, backend) -> do
logStatus key ValuePresent
2010-10-14 23:36:11 +00:00
liftIO $ setup g key
2010-10-14 07:18:11 +00:00
where
err = error $ "already annexed " ++ file
checkLegal file = do
s <- getSymbolicLinkStatus file
if ((isSymbolicLink s) || (not $ isRegularFile s))
then error $ "not a regular file: " ++ file
else return ()
2010-10-14 23:36:11 +00:00
setup g key = do
let dest = annexLocation g key
let reldest = annexLocationRelative g key
2010-10-14 07:18:11 +00:00
createDirectoryIfMissing True (parentDir dest)
renameFile file dest
createSymbolicLink ((linkTarget file) ++ reldest) file
Git.run g ["add", file]
Git.run g ["commit", "-m",
("git-annex annexed " ++ file), file]
linkTarget file =
-- relies on file being relative to the top of the
-- git repo; just replace each subdirectory with ".."
if (subdirs > 0)
then (join "/" $ take subdirs $ repeat "..") ++ "/"
else ""
where
subdirs = (length $ split "/" file) - 1
2010-10-14 16:36:40 +00:00
{- Inverse of addCmd. -}
2010-10-14 07:18:11 +00:00
unannexCmd :: FilePath -> Annex ()
unannexCmd file = notinBackend file err $ \(key, backend) -> do
2010-10-14 18:14:19 +00:00
Backend.removeKey backend key
2010-10-14 07:18:11 +00:00
logStatus key ValueMissing
g <- Annex.gitRepo
2010-10-14 23:36:11 +00:00
let src = annexLocation g key
2010-10-14 07:18:11 +00:00
liftIO $ moveout g src
where
err = error $ "not annexed " ++ file
moveout g src = do
removeFile file
Git.run g ["rm", file]
Git.run g ["commit", "-m",
("git-annex unannexed " ++ file), file]
-- git rm deletes empty directories;
-- put them back
createDirectoryIfMissing True (parentDir file)
renameFile src file
return ()
{- Gets an annexed file from one of the backends. -}
getCmd :: FilePath -> Annex ()
getCmd file = notinBackend file err $ \(key, backend) -> do
2010-10-14 23:36:11 +00:00
inannex <- inAnnex key
2010-10-14 07:18:11 +00:00
if (inannex)
then return ()
else do
g <- Annex.gitRepo
2010-10-14 23:36:11 +00:00
let dest = annexLocation g key
2010-10-14 07:18:11 +00:00
liftIO $ createDirectoryIfMissing True (parentDir dest)
2010-10-14 18:14:19 +00:00
success <- Backend.retrieveKeyFile backend key dest
2010-10-14 07:18:11 +00:00
if (success)
then do
logStatus key ValuePresent
return ()
else error $ "failed to get " ++ file
where
err = error $ "not annexed " ++ file
{- Indicates a file is wanted. -}
wantCmd :: FilePath -> Annex ()
wantCmd file = do error "not implemented" -- TODO
{- Indicates a file is not wanted. -}
dropCmd :: FilePath -> Annex ()
2010-10-14 18:14:19 +00:00
dropCmd file = notinBackend file err $ \(key, backend) -> do
2010-10-14 21:37:20 +00:00
requireEnoughCopies key
2010-10-14 18:14:19 +00:00
success <- Backend.removeKey backend key
if (success)
then do
logStatus key ValueMissing
2010-10-14 23:36:11 +00:00
inannex <- inAnnex key
2010-10-14 18:14:19 +00:00
if (inannex)
then do
g <- Annex.gitRepo
2010-10-14 23:36:11 +00:00
let loc = annexLocation g key
2010-10-14 18:14:19 +00:00
liftIO $ removeFile loc
return ()
else return ()
else error $ "backend refused to drop " ++ file
where
err = error $ "not annexed " ++ file
2010-10-14 07:18:11 +00:00
{- Pushes all files to a remote repository. -}
pushCmd :: String -> Annex ()
pushCmd reponame = do error "not implemented" -- TODO
{- Pulls all files from a remote repository. -}
pullCmd :: String -> Annex ()
pullCmd reponame = do error "not implemented" -- TODO
{- Updates the LocationLog when a key's presence changes. -}
logStatus :: Key -> LogStatus -> Annex ()
logStatus key status = do
g <- Annex.gitRepo
u <- getUUID g
f <- liftIO $ logChange g key u status
2010-10-14 21:57:04 +00:00
liftIO $ Git.run g ["add", f] -- committed at shutdown
2010-10-14 07:18:11 +00:00
2010-10-14 07:40:26 +00:00
inBackend file yes no = do
r <- liftIO $ Backend.lookupFile file
case (r) of
Just v -> yes v
Nothing -> no
notinBackend file yes no = inBackend file no yes
2010-10-14 21:37:20 +00:00
{- Checks remotes to verify that enough copies of a key exist to allow
- for a key to be safely removed (with no data loss), and fails with an
- error if not. -}
requireEnoughCopies :: Key -> Annex ()
requireEnoughCopies key = do
g <- Annex.gitRepo
let numcopies = read $ Git.configGet g config "1"
remotes <- Remotes.withKey key
if (numcopies > length remotes)
then error $ "I only know about " ++ (show $ length remotes) ++
" out of " ++ (show numcopies) ++
" necessary copies of: " ++ (keyFile key) ++
unsafe
else findcopies numcopies remotes []
where
findcopies 0 _ _ = return () -- success, enough copies found
findcopies _ [] bad = die bad
findcopies n (r:rs) bad = do
result <- liftIO $ try $ haskey r
case (result) of
2010-10-14 22:48:21 +00:00
Right True -> findcopies (n-1) rs bad
Right False -> findcopies n rs bad
Left _ -> findcopies n rs (r:bad)
2010-10-14 21:37:20 +00:00
haskey r = do
-- To check if a remote has a key, construct a new
-- Annex monad and query its backend.
a <- Annex.new r
(result, _) <- Annex.run a (Backend.hasKey key)
return result
die bad =
error $ "I failed to find enough other copies of: " ++
2010-10-14 22:48:21 +00:00
(keyFile key) ++
(if (0 /= length bad) then listbad bad else "")
++ unsafe
listbad bad = "\nI was unable to access these remotes: " ++
(Remotes.list bad)
2010-10-14 21:37:20 +00:00
unsafe = "\n -- According to the " ++ config ++
" setting, it is not safe to remove it!"
config = "annex.numcopies"