2010-10-14 18:38:29 +00:00
|
|
|
{- git-annex command line -}
|
2010-10-14 07:18:11 +00:00
|
|
|
|
2010-10-15 01:10:59 +00:00
|
|
|
module Commands (parseCmd) 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-15 03:57:22 +00:00
|
|
|
{- Parses command line and returns a list of flags and a list of
|
|
|
|
- actions to be run in the Annex monad. -}
|
2010-10-15 01:10:59 +00:00
|
|
|
parseCmd :: [String] -> IO ([Flag], [Annex ()])
|
|
|
|
parseCmd argv = do
|
|
|
|
(flags, nonopts) <- getopt
|
|
|
|
case (length nonopts) of
|
|
|
|
0 -> error header
|
|
|
|
_ -> do
|
|
|
|
let c = lookupCmd (nonopts !! 0)
|
|
|
|
if (0 == length c)
|
|
|
|
then return $ (flags, map defaultCmd nonopts)
|
|
|
|
else do
|
|
|
|
return $ (flags, map (snd $ c !! 0) $ drop 1 nonopts)
|
|
|
|
where
|
|
|
|
getopt = case getOpt Permute options argv of
|
|
|
|
(flags, nonopts, []) -> return (flags, nonopts)
|
|
|
|
(_, _, errs) -> ioError (userError (concat errs ++ usageInfo header options))
|
|
|
|
lookupCmd cmd = filter (\(c, a) -> c == cmd) cmds
|
|
|
|
cmds = [ ("add", addCmd)
|
2010-10-15 03:57:22 +00:00
|
|
|
, ("get", getCmd)
|
|
|
|
, ("drop", dropCmd)
|
|
|
|
, ("want", wantCmd)
|
2010-10-15 01:10:59 +00:00
|
|
|
, ("push", pushCmd)
|
|
|
|
, ("pull", pullCmd)
|
|
|
|
, ("unannex", unannexCmd)
|
|
|
|
]
|
|
|
|
header = "Usage: git-annex [" ++
|
|
|
|
(join "|" $ map fst cmds) ++ "] file ..."
|
2010-10-15 03:57:22 +00:00
|
|
|
options = [ Option ['f'] ["force"] (NoArg Force) "allow actions that may loose annexed data" ]
|
2010-10-14 18:38:29 +00:00
|
|
|
|
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-15 02:09:03 +00:00
|
|
|
force <- Annex.flagIsSet Force
|
|
|
|
if (not force)
|
|
|
|
then requireEnoughCopies key
|
|
|
|
else return ()
|
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-15 03:52:45 +00:00
|
|
|
liftIO $ Git.run g ["add", f]
|
|
|
|
Annex.flagChange NeedCommit True
|
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-15 02:09:03 +00:00
|
|
|
unsafe = "\n" ++
|
|
|
|
" -- According to the " ++ config ++
|
|
|
|
" setting, it is not safe to remove it!\n" ++
|
|
|
|
" (Use --force to override.)"
|
|
|
|
|
2010-10-14 21:37:20 +00:00
|
|
|
config = "annex.numcopies"
|