2010-11-02 23:04:24 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Add where
|
|
|
|
|
|
|
|
import Control.Monad.State (liftIO)
|
|
|
|
import System.Posix.Files
|
2011-07-08 01:29:31 +00:00
|
|
|
import System.Directory
|
|
|
|
import Control.Exception.Control (handle)
|
|
|
|
import Control.Exception.Base (throwIO)
|
|
|
|
import Control.Exception.Extensible (IOException)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
import Command
|
2011-06-29 15:42:00 +00:00
|
|
|
import qualified Annex
|
2011-04-07 17:59:31 +00:00
|
|
|
import qualified AnnexQueue
|
2010-11-02 23:04:24 +00:00
|
|
|
import qualified Backend
|
|
|
|
import LocationLog
|
|
|
|
import Types
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2010-11-08 19:15:21 +00:00
|
|
|
import Messages
|
2011-02-28 20:10:16 +00:00
|
|
|
import Utility
|
2011-03-15 03:00:23 +00:00
|
|
|
import Touch
|
2011-07-08 01:29:31 +00:00
|
|
|
import Locations
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
2011-03-19 22:58:49 +00:00
|
|
|
command = [repoCommand "add" paramPath seek "add files to annex"]
|
2010-12-30 18:19:16 +00:00
|
|
|
|
2010-11-11 22:54:52 +00:00
|
|
|
{- Add acts on both files not checked into git yet, and unlocked files. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2010-11-11 22:54:52 +00:00
|
|
|
seek = [withFilesNotInGit start, withFilesUnlocked start]
|
|
|
|
|
2010-11-02 23:04:24 +00:00
|
|
|
{- The add subcommand 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-12-30 18:19:16 +00:00
|
|
|
start :: CommandStartBackendFile
|
2010-11-02 23:04:24 +00:00
|
|
|
start pair@(file, _) = notAnnexed file $ do
|
|
|
|
s <- liftIO $ getSymbolicLinkStatus file
|
2010-11-22 21:51:55 +00:00
|
|
|
if (isSymbolicLink s) || (not $ isRegularFile s)
|
2011-05-15 06:02:46 +00:00
|
|
|
then stop
|
2010-11-02 23:04:24 +00:00
|
|
|
else do
|
|
|
|
showStart "add" file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ perform pair
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
perform :: BackendFile -> CommandPerform
|
2010-11-02 23:04:24 +00:00
|
|
|
perform (file, backend) = do
|
2011-07-05 22:31:46 +00:00
|
|
|
k <- Backend.genKey file backend
|
|
|
|
case k of
|
2011-05-15 06:02:46 +00:00
|
|
|
Nothing -> stop
|
2011-01-08 19:54:14 +00:00
|
|
|
Just (key, _) -> do
|
2011-07-08 01:29:31 +00:00
|
|
|
handle (undo file key) $ moveAnnex key file
|
2011-05-15 06:02:46 +00:00
|
|
|
next $ cleanup file key
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-07-08 01:29:31 +00:00
|
|
|
{- On error, put the file back so it doesn't seem to have vanished.
|
|
|
|
- This can be called before or after the symlink is in place. -}
|
|
|
|
undo :: FilePath -> Key -> IOException -> Annex a
|
|
|
|
undo file key e = do
|
|
|
|
unlessM (inAnnex key) $ rethrow -- no cleanup to do
|
|
|
|
liftIO $ whenM (doesFileExist file) $ do removeFile file
|
|
|
|
handle tryharder $ fromAnnex key file
|
|
|
|
logStatus key InfoMissing
|
|
|
|
rethrow
|
|
|
|
where
|
|
|
|
rethrow = liftIO $ throwIO e
|
2011-03-15 03:00:23 +00:00
|
|
|
|
2011-07-08 01:29:31 +00:00
|
|
|
-- fromAnnex could fail if the file ownership is weird
|
|
|
|
tryharder :: IOException -> Annex ()
|
|
|
|
tryharder _ = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ renameFile (gitAnnexLocation g key) file
|
2011-07-07 23:29:36 +00:00
|
|
|
|
2011-07-08 01:29:31 +00:00
|
|
|
cleanup :: FilePath -> Key -> CommandCleanup
|
|
|
|
cleanup file key = do
|
|
|
|
handle (undo file key) $ do
|
|
|
|
link <- calcGitLink file key
|
|
|
|
liftIO $ createSymbolicLink link file
|
|
|
|
logStatus key InfoPresent
|
|
|
|
|
|
|
|
-- touch the symlink to have the same mtime as the
|
|
|
|
-- file it points to
|
|
|
|
s <- liftIO $ getFileStatus file
|
|
|
|
let mtime = modificationTime s
|
|
|
|
liftIO $ touch file (TimeSpec mtime) False
|
2011-03-15 03:00:23 +00:00
|
|
|
|
2011-06-29 15:42:00 +00:00
|
|
|
force <- Annex.getState Annex.force
|
|
|
|
if force
|
|
|
|
then AnnexQueue.add "add" [Param "-f", Param "--"] file
|
|
|
|
else AnnexQueue.add "add" [Param "--"] file
|
2010-11-02 23:04:24 +00:00
|
|
|
return True
|