git-annex/Annex.hs

190 lines
5.1 KiB
Haskell
Raw Normal View History

2010-10-10 23:00:08 +00:00
{- git-annex toplevel code
2010-10-10 19:04:07 +00:00
-}
2010-10-11 21:52:46 +00:00
module Annex (
startAnnex,
annexFile,
2010-10-12 19:44:54 +00:00
unannexFile,
annexGetFile,
annexWantFile,
annexDropFile,
annexPushRepo,
annexPullRepo
2010-10-11 21:52:46 +00:00
) where
2010-10-10 19:04:07 +00:00
2010-10-14 01:28:47 +00:00
import Control.Monad.State (liftIO)
2010-10-10 19:04:07 +00:00
import System.Posix.Files
import System.Directory
2010-10-12 21:26:34 +00:00
import Data.String.Utils
2010-10-13 18:40:56 +00:00
import List
2010-10-10 19:04:07 +00:00
import GitRepo
import Utility
import Locations
import Backend
2010-10-10 22:25:31 +00:00
import BackendList
2010-10-12 17:10:07 +00:00
import UUID
import LocationLog
import AbstractTypes
2010-10-11 21:19:55 +00:00
2010-10-14 01:28:47 +00:00
{- Create and returns an Annex state object.
- Examines and prepares the git repo.
-}
startAnnex :: IO AnnexState
2010-10-10 22:25:31 +00:00
startAnnex = do
2010-10-14 01:28:47 +00:00
g <- gitRepoFromCwd
let s = makeAnnexState g
(_,s') <- runAnnexState s (prep g)
return s'
where
prep g = do
-- setup git and read its config; update state
g' <- liftIO $ gitConfigRead g
gitAnnexChange g'
2010-10-14 01:35:10 +00:00
liftIO $ gitSetup g'
2010-10-14 01:28:47 +00:00
backendsAnnexChange $ parseBackendList $
gitConfig g' "annex.backends" ""
prepUUID
2010-10-10 19:04:07 +00:00
2010-10-13 07:30:51 +00:00
inBackend file yes no = do
2010-10-14 01:28:47 +00:00
r <- liftIO $ lookupFile file
2010-10-13 07:30:51 +00:00
case (r) of
Just v -> yes v
Nothing -> no
notinBackend file yes no = inBackend file no yes
2010-10-10 19:04:07 +00:00
{- Annexes a file, storing it in a backend, and then moving it into
2010-10-10 19:21:17 +00:00
- the annex directory and setting up the symlink pointing to its content. -}
2010-10-14 01:28:47 +00:00
annexFile :: FilePath -> Annex ()
annexFile file = inBackend file err $ do
liftIO $ checkLegal file
stored <- storeFile file
g <- gitAnnex
2010-10-13 07:30:51 +00:00
case (stored) of
Nothing -> error $ "no backend could store: " ++ file
2010-10-14 01:28:47 +00:00
Just (key, backend) -> do
logStatus key ValuePresent
liftIO $ setup g key backend
2010-10-10 19:04:07 +00:00
where
2010-10-13 07:30:51 +00:00
err = error $ "already annexed " ++ file
2010-10-13 05:36:20 +00:00
checkLegal file = do
s <- getSymbolicLinkStatus file
if ((isSymbolicLink s) || (not $ isRegularFile s))
then error $ "not a regular file: " ++ file
else return ()
2010-10-14 01:28:47 +00:00
setup g key backend = do
let dest = annexLocation g backend key
let reldest = annexLocationRelative g backend key
2010-10-10 19:04:07 +00:00
createDirectoryIfMissing True (parentDir dest)
renameFile file dest
2010-10-13 05:36:20 +00:00
createSymbolicLink ((linkTarget file) ++ reldest) file
2010-10-14 01:28:47 +00:00
gitRun g ["add", file]
gitRun g ["commit", "-m",
("git-annex annexed " ++ file), file]
2010-10-13 05:36:20 +00:00
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-10 23:53:31 +00:00
{- Inverse of annexFile. -}
2010-10-14 01:28:47 +00:00
unannexFile :: FilePath -> Annex ()
unannexFile file = notinBackend file err $ \(key, backend) -> do
dropFile backend key
logStatus key ValueMissing
g <- gitAnnex
let src = annexLocation g backend key
liftIO $ moveout g src
2010-10-13 07:30:51 +00:00
where
err = error $ "not annexed " ++ file
2010-10-14 01:28:47 +00:00
moveout g src = do
removeFile file
gitRun g ["rm", file]
gitRun g ["commit", "-m",
("git-annex unannexed " ++ file), file]
-- git rm deletes empty directories;
-- put them back
createDirectoryIfMissing True (parentDir file)
renameFile src file
return ()
2010-10-13 07:51:55 +00:00
{- Gets an annexed file from one of the backends. -}
2010-10-14 01:28:47 +00:00
annexGetFile :: FilePath -> Annex ()
annexGetFile file = notinBackend file err $ \(key, backend) -> do
inannex <- inAnnex backend key
2010-10-13 07:46:40 +00:00
if (inannex)
then return ()
else do
2010-10-14 01:28:47 +00:00
g <- gitAnnex
let dest = annexLocation g backend key
liftIO $ createDirectoryIfMissing True (parentDir dest)
success <- retrieveFile backend key dest
2010-10-13 07:46:40 +00:00
if (success)
then do
2010-10-14 01:28:47 +00:00
logStatus key ValuePresent
2010-10-13 07:46:40 +00:00
return ()
else error $ "failed to get " ++ file
where
err = error $ "not annexed " ++ file
2010-10-12 19:44:54 +00:00
{- Indicates a file is wanted. -}
2010-10-14 01:28:47 +00:00
annexWantFile :: FilePath -> Annex ()
annexWantFile file = do error "not implemented" -- TODO
2010-10-12 19:44:54 +00:00
2010-10-12 19:48:00 +00:00
{- Indicates a file is not wanted. -}
2010-10-14 01:28:47 +00:00
annexDropFile :: FilePath -> Annex ()
annexDropFile file = do error "not implemented" -- TODO
2010-10-12 19:44:54 +00:00
{- Pushes all files to a remote repository. -}
2010-10-14 01:28:47 +00:00
annexPushRepo :: String -> Annex ()
annexPushRepo reponame = do error "not implemented" -- TODO
2010-10-12 19:44:54 +00:00
{- Pulls all files from a remote repository. -}
2010-10-14 01:28:47 +00:00
annexPullRepo :: String -> Annex ()
annexPullRepo reponame = do error "not implemented" -- TODO
2010-10-12 19:44:54 +00:00
{- Sets up a git repo for git-annex. May be called repeatedly. -}
2010-10-13 04:45:09 +00:00
gitSetup :: GitRepo -> IO ()
gitSetup repo = do
-- configure git to use union merge driver on state files
exists <- doesFileExist attributes
if (not exists)
then do
writeFile attributes $ attrLine ++ "\n"
2010-10-13 00:04:36 +00:00
commit
else do
content <- readFile attributes
if (all (/= attrLine) (lines content))
then do
appendFile attributes $ attrLine ++ "\n"
2010-10-13 00:04:36 +00:00
commit
else return ()
2010-10-13 00:04:36 +00:00
where
attrLine = stateLoc ++ "/*.log merge=union"
attributes = gitAttributes repo
commit = do
gitRun repo ["add", attributes]
gitRun repo ["commit", "-m", "git-annex setup",
attributes]
{- Updates the LocationLog when a key's presence changes. -}
2010-10-14 01:28:47 +00:00
logStatus :: Key -> LogStatus -> Annex ()
logStatus key status = do
g <- gitAnnex
u <- getUUID g
f <- liftIO $ logChange g key u status
liftIO $ commit g f
where
commit g f = do
gitRun g ["add", f]
gitRun g ["commit", "-m", "git-annex log update", f]
2010-10-13 07:51:55 +00:00
{- Checks if a given key is currently present in the annexLocation -}
2010-10-14 01:28:47 +00:00
inAnnex :: Backend -> Key -> Annex Bool
inAnnex backend key = do
g <- gitAnnex
liftIO $ doesFileExist $ annexLocation g backend key