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
|
2010-10-15 20:09:30 +00:00
|
|
|
import System.Path
|
2010-10-14 07:18:11 +00:00
|
|
|
import Data.String.Utils
|
|
|
|
import List
|
2010-10-14 21:37:20 +00:00
|
|
|
import IO
|
2010-10-16 20:20:49 +00:00
|
|
|
|
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-16 18:58:35 +00:00
|
|
|
import qualified BackendTypes
|
|
|
|
|
2010-10-16 20:15:31 +00:00
|
|
|
data CmdWants = FilesInGit | FilesNotInGit | RepoName | SingleString
|
2010-10-16 18:58:35 +00:00
|
|
|
data Command = Command {
|
|
|
|
cmdname :: String,
|
|
|
|
cmdaction :: (String -> Annex ()),
|
|
|
|
cmdwants :: CmdWants
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds :: [Command]
|
2010-10-16 23:43:32 +00:00
|
|
|
cmds = [
|
|
|
|
(Command "add" addCmd FilesNotInGit)
|
2010-10-16 18:58:35 +00:00
|
|
|
, (Command "get" getCmd FilesInGit)
|
|
|
|
, (Command "drop" dropCmd FilesInGit)
|
|
|
|
, (Command "push" pushCmd RepoName)
|
|
|
|
, (Command "pull" pullCmd RepoName)
|
|
|
|
, (Command "unannex" unannexCmd FilesInGit)
|
2010-10-16 20:15:31 +00:00
|
|
|
, (Command "describe" describeCmd SingleString)
|
2010-10-16 18:58:35 +00:00
|
|
|
]
|
|
|
|
|
2010-10-16 23:43:32 +00:00
|
|
|
options = [
|
|
|
|
Option ['f'] ["force"] (NoArg Force) "allow actions that may loose annexed data"
|
|
|
|
, Option ['N'] ["no-commit"] (NoArg NoCommit) "do not stage or commit changes"
|
|
|
|
]
|
|
|
|
|
2010-10-16 18:58:35 +00:00
|
|
|
{- Finds the type of parameters a command wants, from among the passed
|
|
|
|
- parameter list. -}
|
|
|
|
findWanted :: CmdWants -> [String] -> Git.Repo -> IO [String]
|
|
|
|
findWanted FilesNotInGit params repo = do
|
|
|
|
files <- mapM (Git.notInRepo repo) params
|
|
|
|
return $ foldl (++) [] files
|
|
|
|
findWanted FilesInGit params repo = do
|
|
|
|
files <- mapM (Git.inRepo repo) params
|
|
|
|
return $ foldl (++) [] files
|
2010-10-16 20:15:31 +00:00
|
|
|
findWanted SingleString params _ = do
|
|
|
|
return $ [unwords params]
|
2010-10-16 18:58:35 +00:00
|
|
|
findWanted RepoName params _ = do
|
|
|
|
return $ params
|
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-16 18:58:35 +00:00
|
|
|
parseCmd :: [String] -> AnnexState -> IO ([Flag], [Annex ()])
|
|
|
|
parseCmd argv state = do
|
2010-10-16 17:38:59 +00:00
|
|
|
(flags, params) <- getopt
|
|
|
|
case (length params) of
|
2010-10-17 00:03:41 +00:00
|
|
|
0 -> error usage
|
2010-10-16 17:59:48 +00:00
|
|
|
_ -> case (lookupCmd (params !! 0)) of
|
2010-10-17 00:03:41 +00:00
|
|
|
[] -> error usage
|
2010-10-16 18:58:35 +00:00
|
|
|
[Command _ action want] -> do
|
|
|
|
f <- findWanted want (drop 1 params)
|
|
|
|
(BackendTypes.repo state)
|
|
|
|
return (flags, map action f)
|
2010-10-15 01:10:59 +00:00
|
|
|
where
|
|
|
|
getopt = case getOpt Permute options argv of
|
2010-10-16 18:58:35 +00:00
|
|
|
(flags, params, []) -> return (flags, params)
|
2010-10-17 00:03:41 +00:00
|
|
|
(_, _, errs) -> ioError (userError (concat errs ++ usage))
|
2010-10-16 18:58:35 +00:00
|
|
|
lookupCmd cmd = filter (\c -> cmd == cmdname c) cmds
|
2010-10-15 01:10:59 +00:00
|
|
|
header = "Usage: git-annex [" ++
|
2010-10-16 19:10:07 +00:00
|
|
|
(join "|" $ map cmdname cmds) ++ "] ..."
|
2010-10-17 00:03:41 +00:00
|
|
|
usage = usageInfo header options
|
2010-10-14 18:38:29 +00:00
|
|
|
|
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
|
|
|
|
g <- Annex.gitRepo
|
2010-10-15 20:09:30 +00:00
|
|
|
link <- liftIO $ calcGitLink file g
|
|
|
|
stored <- Backend.storeFileKey file
|
2010-10-14 07:18:11 +00:00
|
|
|
case (stored) of
|
|
|
|
Nothing -> error $ "no backend could store: " ++ file
|
|
|
|
Just (key, backend) -> do
|
|
|
|
logStatus key ValuePresent
|
2010-10-16 23:43:32 +00:00
|
|
|
setup g key link
|
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-15 20:09:30 +00:00
|
|
|
calcGitLink file g = do
|
|
|
|
cwd <- getCurrentDirectory
|
|
|
|
let absfile = case (absNormPath cwd file) of
|
|
|
|
Just f -> f
|
|
|
|
Nothing -> error $ "unable to normalize " ++ file
|
|
|
|
return $ relPathDirToDir (parentDir absfile) (Git.workTree g)
|
|
|
|
setup g key link = do
|
2010-10-14 23:36:11 +00:00
|
|
|
let dest = annexLocation g key
|
|
|
|
let reldest = annexLocationRelative g key
|
2010-10-16 23:43:32 +00:00
|
|
|
liftIO $ createDirectoryIfMissing True (parentDir dest)
|
|
|
|
liftIO $ renameFile file dest
|
|
|
|
liftIO $ createSymbolicLink (link ++ reldest) file
|
2010-10-16 23:57:56 +00:00
|
|
|
gitAdd file $ Just $ "git-annex annexed " ++ file
|
2010-10-14 07:18:11 +00:00
|
|
|
|
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
|
|
|
|
|
2010-10-16 20:15:31 +00:00
|
|
|
{- Indicates a file's content is not wanted anymore, and should be removed
|
|
|
|
- if it's safe to do so. -}
|
2010-10-14 07:18:11 +00:00
|
|
|
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
|
|
|
|
|
2010-10-16 20:15:31 +00:00
|
|
|
{- Stores description for the repository. -}
|
|
|
|
describeCmd :: String -> Annex ()
|
|
|
|
describeCmd description = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
u <- getUUID g
|
|
|
|
describeUUID u description
|
|
|
|
log <- uuidLog
|
2010-10-16 23:57:56 +00:00
|
|
|
gitAdd log Nothing -- all logs are committed at end
|
2010-10-16 20:15:31 +00:00
|
|
|
liftIO $ putStrLn "description set"
|
|
|
|
|
2010-10-14 07:18:11 +00:00
|
|
|
{- 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-16 23:57:56 +00:00
|
|
|
gitAdd f Nothing -- all logs are committed at end
|
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"
|