git-annex/Annex.hs

59 lines
1.6 KiB
Haskell
Raw Normal View History

2010-10-10 19:04:07 +00:00
{- git-annex
-}
module Annex where
import System.Posix.Files
import System.Directory
import GitRepo
import Utility
import Locations
2010-10-10 22:05:37 +00:00
import Types
import Backend
2010-10-10 22:25:31 +00:00
import BackendList
startAnnex :: IO State
startAnnex = do
r <- currentRepo
return State { repo = r, backends = supportedBackends }
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
alreadyannexed <- lookupBackend (backends state) (repo state) file
2010-10-10 19:04:07 +00:00
case (alreadyannexed) of
Just _ -> error $ "already annexed " ++ file
Nothing -> do
2010-10-10 22:25:31 +00:00
stored <- storeFile (backends state) (repo state) file
2010-10-10 19:21:17 +00:00
case (stored) of
Nothing -> error $ "no backend could store " ++ file
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-10 22:25:31 +00:00
dest <- annexDir (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
{- 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
let attrLine = stateLoc ++ "/* merge=union"
attributes <- gitAttributes repo
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 ()