git-annex/Annex.hs

172 lines
4.9 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
import System.Posix.Files
import System.Directory
2010-10-12 21:26:34 +00:00
import Data.String.Utils
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
2010-10-12 19:52:18 +00:00
import Types
2010-10-11 21:19:55 +00:00
2010-10-12 21:56:29 +00:00
{- Checks if a given key is currently present in the annexLocation -}
inAnnex :: State -> Backend -> Key -> IO Bool
inAnnex state backend key = doesFileExist $ annexLocation state backend key
2010-10-12 21:56:29 +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
2010-10-13 04:45:09 +00:00
gitSetup r'
2010-10-12 03:22:38 +00:00
return State {
2010-10-12 17:12:47 +00:00
repo = r',
backends = parseBackendList $ gitConfig r' "annex.backends" ""
}
2010-10-10 19:04:07 +00:00
2010-10-13 07:30:51 +00:00
inBackend file yes no = do
r <- lookupFile file
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-10 22:25:31 +00:00
annexFile :: State -> FilePath -> IO ()
2010-10-13 07:30:51 +00:00
annexFile state file = inBackend file err $ do
checkLegal file
stored <- storeFile state file
case (stored) of
Nothing -> error $ "no backend could store: " ++ file
Just (key, backend) -> setup 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-13 00:04:36 +00:00
setup key backend = do
let dest = annexLocation state backend key
2010-10-13 05:36:20 +00:00
let reldest = annexLocationRelative state 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
gitRun (repo state) ["add", file]
2010-10-13 00:04:36 +00:00
gitRun (repo state) ["commit", "-m",
("git-annex annexed " ++ file), file]
2010-10-13 00:04:36 +00:00
logStatus state key ValuePresent
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. -}
unannexFile :: State -> FilePath -> IO ()
2010-10-13 07:30:51 +00:00
unannexFile state file = notinBackend file err $ \(key, backend) -> do
2010-10-13 07:41:12 +00:00
dropFile state backend key
cleanup key backend
2010-10-13 07:30:51 +00:00
where
err = error $ "not annexed " ++ file
cleanup key backend = do
let src = annexLocation state backend key
removeFile file
gitRun (repo state) ["rm", file]
gitRun (repo state) ["commit", "-m",
("git-annex unannexed " ++ file), file]
-- git rm deletes empty directories;
-- put them back
createDirectoryIfMissing True (parentDir file)
renameFile src file
logStatus state key ValueMissing
return ()
2010-10-12 19:44:54 +00:00
{- Transfers the file from a remote. -}
annexGetFile :: State -> FilePath -> IO ()
annexGetFile state file = do
2010-10-13 07:20:05 +00:00
r <- lookupFile file
case (r) of
2010-10-12 19:44:54 +00:00
Nothing -> error $ "not annexed " ++ file
2010-10-13 07:20:05 +00:00
Just (key, backend) -> do
inannex <- inAnnex state backend key
2010-10-12 21:56:29 +00:00
if (inannex)
2010-10-12 20:52:01 +00:00
then return ()
2010-10-12 21:56:29 +00:00
else do
let dest = annexLocation state backend key
2010-10-12 21:56:29 +00:00
createDirectoryIfMissing True (parentDir dest)
2010-10-13 07:20:05 +00:00
success <- retrieveFile state backend key dest
2010-10-12 21:56:29 +00:00
if (success)
2010-10-13 00:04:36 +00:00
then do
logStatus state key ValuePresent
return ()
2010-10-12 21:56:29 +00:00
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
{- 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. -}
logStatus state key status = do
f <- logChange (repo state) key (getUUID (repo state)) status
gitRun (repo state) ["add", f]
gitRun (repo state) ["commit", "-m", "git-annex log update", f]