git-annex/Core.hs

97 lines
2.6 KiB
Haskell
Raw Normal View History

2010-10-14 07:40:26 +00:00
{- git-annex core functions -}
module Core where
2010-10-16 23:57:56 +00:00
import Maybe
2010-10-14 07:40:26 +00:00
import System.IO
import System.Directory
import Control.Monad.State (liftIO)
2010-10-17 01:03:25 +00:00
import System.Path
2010-10-16 20:20:49 +00:00
2010-10-14 07:40:26 +00:00
import Types
import Locations
import UUID
import qualified GitRepo as Git
import qualified Annex
2010-10-17 01:03:25 +00:00
import Utility
2010-10-14 20:13:43 +00:00
2010-10-14 21:57:04 +00:00
{- Sets up a git repo for git-annex. -}
startup :: [Flag] -> Annex ()
startup flags = do
2010-10-15 03:52:45 +00:00
mapM (\f -> Annex.flagChange f True) flags
2010-10-14 20:13:43 +00:00
g <- Annex.gitRepo
2010-10-14 21:57:04 +00:00
liftIO $ gitAttributes g
2010-10-14 20:13:43 +00:00
prepUUID
2010-10-14 21:57:04 +00:00
{- When git-annex is done, it runs this. -}
shutdown :: Annex ()
shutdown = do
g <- Annex.gitRepo
2010-10-16 23:43:32 +00:00
nocommit <- Annex.flagIsSet NoCommit
2010-10-15 03:52:45 +00:00
needcommit <- Annex.flagIsSet NeedCommit
2010-10-16 23:43:32 +00:00
if (needcommit && not nocommit)
2010-10-16 17:22:48 +00:00
then liftIO $ Git.run g ["commit", "-q", "-m",
2010-10-15 18:31:06 +00:00
"git-annex log update", gitStateDir g]
2010-10-15 03:52:45 +00:00
else return ()
2010-10-14 21:57:04 +00:00
{- configure git to use union merge driver on state files, if it is not
- already -}
gitAttributes :: Git.Repo -> IO ()
gitAttributes repo = do
exists <- doesFileExist attributes
if (not exists)
then do
writeFile attributes $ attrLine ++ "\n"
commit
else do
content <- readFile attributes
if (all (/= attrLine) (lines content))
2010-10-14 07:40:26 +00:00
then do
2010-10-14 21:57:04 +00:00
appendFile attributes $ attrLine ++ "\n"
2010-10-14 07:40:26 +00:00
commit
2010-10-14 21:57:04 +00:00
else return ()
where
attrLine = stateLoc ++ "*.log merge=union"
2010-10-14 21:57:04 +00:00
attributes = Git.attributes repo
commit = do
Git.run repo ["add", attributes]
Git.run repo ["commit", "-m", "git-annex setup",
attributes]
2010-10-14 18:38:29 +00:00
{- Checks if a given key is currently present in the annexLocation -}
2010-10-14 23:36:11 +00:00
inAnnex :: Key -> Annex Bool
inAnnex key = do
2010-10-14 18:38:29 +00:00
g <- Annex.gitRepo
2010-10-14 23:36:11 +00:00
liftIO $ doesFileExist $ annexLocation g key
2010-10-16 23:57:56 +00:00
{- Adds, optionally also commits a file to git.
-
- All changes to the git repository should go through this function.
-
- This is careful to not rely on the index. It may have staged changes,
- so only use operations that avoid committing such changes.
-}
gitAdd :: FilePath -> Maybe String -> Annex ()
gitAdd file commitmessage = do
nocommit <- Annex.flagIsSet NoCommit
if (nocommit)
2010-10-17 15:57:39 +00:00
then return ()
2010-10-16 23:57:56 +00:00
else do
g <- Annex.gitRepo
liftIO $ Git.run g ["add", file]
if (isJust commitmessage)
then liftIO $ Git.run g ["commit", "-m",
(fromJust commitmessage), file]
2010-10-17 15:57:39 +00:00
else Annex.flagChange NeedCommit True
2010-10-17 01:03:25 +00:00
{- Calculates the relative path to use to link a file to a key. -}
calcGitLink :: FilePath -> Key -> Annex FilePath
calcGitLink file key = do
g <- Annex.gitRepo
cwd <- liftIO $ getCurrentDirectory
let absfile = case (absNormPath cwd file) of
Just f -> f
Nothing -> error $ "unable to normalize " ++ file
return $ (relPathDirToDir (parentDir absfile) (Git.workTree g)) ++
annexLocationRelative g key