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
|
|
|
|
|
|
|
import System.Posix.Files
|
|
|
|
import System.Directory
|
|
|
|
import GitRepo
|
|
|
|
import Utility
|
2010-10-10 19:54:02 +00:00
|
|
|
import Locations
|
|
|
|
import Backend
|
2010-10-10 22:25:31 +00:00
|
|
|
import BackendList
|
2010-10-12 17:10:07 +00:00
|
|
|
import UUID
|
2010-10-11 02:20:52 +00:00
|
|
|
import LocationLog
|
2010-10-12 19:52:18 +00:00
|
|
|
import Types
|
2010-10-11 21:19:55 +00:00
|
|
|
|
|
|
|
{- An annexed file's content is stored somewhere under .git/annex/ -}
|
2010-10-12 20:52:01 +00:00
|
|
|
annexLocation :: GitRepo -> Key -> FilePath
|
|
|
|
annexLocation repo key = gitDir repo ++ "/annex/" ++ key
|
2010-10-11 21:19:55 +00:00
|
|
|
|
2010-10-10 23:00:08 +00:00
|
|
|
{- On startup, examine the git repo, prepare it, and record state for
|
|
|
|
- later. -}
|
2010-10-10 22:25:31 +00:00
|
|
|
startAnnex :: IO State
|
|
|
|
startAnnex = do
|
2010-10-12 06:40:09 +00:00
|
|
|
r <- gitRepoFromCwd
|
2010-10-12 17:12:47 +00:00
|
|
|
r' <- prepUUID r
|
|
|
|
gitPrep r'
|
2010-10-12 03:22:38 +00:00
|
|
|
|
2010-10-11 04:19:38 +00:00
|
|
|
return State {
|
2010-10-12 17:12:47 +00:00
|
|
|
repo = r',
|
|
|
|
backends = parseBackendList $ gitConfig r' "annex.backends" ""
|
2010-10-11 04:19:38 +00:00
|
|
|
}
|
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-10 22:25:31 +00:00
|
|
|
annexFile :: State -> FilePath -> IO ()
|
|
|
|
annexFile state file = do
|
2010-10-12 20:20:41 +00:00
|
|
|
alreadyannexed <- lookupBackend state file
|
2010-10-10 19:04:07 +00:00
|
|
|
case (alreadyannexed) of
|
2010-10-11 01:00:42 +00:00
|
|
|
Just _ -> error $ "already annexed: " ++ file
|
2010-10-10 19:04:07 +00:00
|
|
|
Nothing -> do
|
2010-10-11 01:00:42 +00:00
|
|
|
checkLegal file
|
2010-10-12 20:20:41 +00:00
|
|
|
stored <- storeFile state file
|
2010-10-10 19:21:17 +00:00
|
|
|
case (stored) of
|
2010-10-11 01:00:42 +00:00
|
|
|
Nothing -> error $ "no backend could store: " ++ file
|
2010-10-10 19:21:17 +00:00
|
|
|
Just key -> symlink key
|
2010-10-10 19:04:07 +00:00
|
|
|
where
|
2010-10-10 19:21:17 +00:00
|
|
|
symlink key = do
|
2010-10-12 20:52:01 +00:00
|
|
|
let dest = annexLocation (repo state) key
|
2010-10-10 19:04:07 +00:00
|
|
|
createDirectoryIfMissing True (parentDir dest)
|
|
|
|
renameFile file dest
|
|
|
|
createSymbolicLink dest file
|
2010-10-10 22:25:31 +00:00
|
|
|
gitAdd (repo state) file
|
2010-10-10 23:00:08 +00:00
|
|
|
checkLegal file = do
|
2010-10-10 23:53:31 +00:00
|
|
|
s <- getSymbolicLinkStatus file
|
|
|
|
if ((isSymbolicLink s) || (not $ isRegularFile s))
|
|
|
|
then error $ "not a regular file: " ++ file
|
|
|
|
else return ()
|
|
|
|
|
|
|
|
{- Inverse of annexFile. -}
|
|
|
|
unannexFile :: State -> FilePath -> IO ()
|
|
|
|
unannexFile state file = do
|
2010-10-12 20:20:41 +00:00
|
|
|
alreadyannexed <- lookupBackend state file
|
2010-10-10 23:53:31 +00:00
|
|
|
case (alreadyannexed) of
|
|
|
|
Nothing -> error $ "not annexed " ++ file
|
|
|
|
Just _ -> do
|
2010-10-12 20:20:41 +00:00
|
|
|
mkey <- dropFile state file
|
2010-10-10 23:53:31 +00:00
|
|
|
case (mkey) of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just key -> do
|
2010-10-12 20:52:01 +00:00
|
|
|
let src = annexLocation (repo state) key
|
2010-10-10 23:53:31 +00:00
|
|
|
removeFile file
|
|
|
|
renameFile src file
|
|
|
|
return ()
|
2010-10-10 19:54:02 +00:00
|
|
|
|
2010-10-12 19:44:54 +00:00
|
|
|
{- Transfers the file from a remote. -}
|
|
|
|
annexGetFile :: State -> FilePath -> IO ()
|
|
|
|
annexGetFile state file = do
|
2010-10-12 20:20:41 +00:00
|
|
|
alreadyannexed <- lookupBackend state file
|
2010-10-12 19:44:54 +00:00
|
|
|
case (alreadyannexed) of
|
|
|
|
Nothing -> error $ "not annexed " ++ file
|
2010-10-12 20:52:01 +00:00
|
|
|
Just backend -> do
|
|
|
|
key <- lookupKey state backend file
|
|
|
|
let dest = annexLocation (repo state) key
|
|
|
|
createDirectoryIfMissing True (parentDir dest)
|
|
|
|
success <- retrieveFile state file dest
|
|
|
|
if (success)
|
|
|
|
then return ()
|
|
|
|
else error $ "failed to get " ++ file
|
2010-10-12 19:44:54 +00:00
|
|
|
|
|
|
|
{- Indicates a file is wanted. -}
|
|
|
|
annexWantFile :: State -> FilePath -> IO ()
|
|
|
|
annexWantFile state file = do error "not implemented" -- TODO
|
|
|
|
|
2010-10-12 19:48:00 +00:00
|
|
|
{- Indicates a file is not wanted. -}
|
2010-10-12 19:44:54 +00:00
|
|
|
annexDropFile :: State -> FilePath -> IO ()
|
|
|
|
annexDropFile state file = do error "not implemented" -- TODO
|
|
|
|
|
|
|
|
{- Pushes all files to a remote repository. -}
|
|
|
|
annexPushRepo :: State -> String -> IO ()
|
|
|
|
annexPushRepo state reponame = do error "not implemented" -- TODO
|
|
|
|
|
|
|
|
{- Pulls all files from a remote repository. -}
|
|
|
|
annexPullRepo :: State -> String -> IO ()
|
|
|
|
annexPullRepo state reponame = do error "not implemented" -- TODO
|
|
|
|
|
2010-10-10 19:54:02 +00:00
|
|
|
{- Sets up a git repo for git-annex. May be called repeatedly. -}
|
|
|
|
gitPrep :: GitRepo -> IO ()
|
|
|
|
gitPrep repo = do
|
|
|
|
-- configure git to use union merge driver on state files
|
2010-10-11 04:22:50 +00:00
|
|
|
let attrLine = stateLoc ++ "/*.log merge=union"
|
2010-10-12 04:53:42 +00:00
|
|
|
let attributes = gitAttributes repo
|
2010-10-10 19:54:02 +00:00
|
|
|
exists <- doesFileExist attributes
|
|
|
|
if (not exists)
|
|
|
|
then do
|
|
|
|
writeFile attributes $ attrLine ++ "\n"
|
|
|
|
gitAdd repo attributes
|
|
|
|
else do
|
|
|
|
content <- readFile attributes
|
|
|
|
if (all (/= attrLine) (lines content))
|
|
|
|
then do
|
|
|
|
appendFile attributes $ attrLine ++ "\n"
|
|
|
|
gitAdd repo attributes
|
|
|
|
else return ()
|