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-17 17:13:49 +00:00
|
|
|
import Data.String.Utils
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-14 07:40:26 +00:00
|
|
|
import Types
|
|
|
|
import Locations
|
2010-10-17 16:08:59 +00:00
|
|
|
import LocationLog
|
2010-10-14 07:40:26 +00:00
|
|
|
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. -}
|
2010-10-15 01:10:59 +00:00
|
|
|
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
|
2010-10-17 02:36:35 +00:00
|
|
|
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 17:59:48 +00:00
|
|
|
|
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)
|
2010-10-17 17:13:49 +00:00
|
|
|
then liftIO $ Git.run g ["commit", "--quiet",
|
|
|
|
"-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
|
2010-10-17 16:08:59 +00:00
|
|
|
|
|
|
|
{- Updates the LocationLog when a key's presence changes. -}
|
|
|
|
logStatus :: Key -> LogStatus -> Annex ()
|
|
|
|
logStatus key status = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
u <- getUUID g
|
|
|
|
f <- liftIO $ logChange g key u status
|
|
|
|
gitAdd f Nothing -- all logs are committed at end
|
|
|
|
|
2010-10-17 17:13:49 +00:00
|
|
|
{- Output logging -}
|
|
|
|
showStart :: String -> String -> Annex ()
|
|
|
|
showStart command file = do
|
|
|
|
liftIO $ putStr $ command ++ " " ++ file
|
|
|
|
liftIO $ hFlush stdout
|
|
|
|
showNote :: String -> Annex ()
|
|
|
|
showNote s = do
|
|
|
|
liftIO $ putStr $ " (" ++ s ++ ")"
|
|
|
|
liftIO $ hFlush stdout
|
|
|
|
showLongNote :: String -> Annex ()
|
|
|
|
showLongNote s = do
|
|
|
|
liftIO $ putStr $ "\n" ++ (indent s)
|
|
|
|
where
|
|
|
|
indent s = join "\n" $ map (\l -> " " ++ l) $ lines s
|
|
|
|
showEndOk :: Annex ()
|
|
|
|
showEndOk = do
|
|
|
|
liftIO $ putStrLn " ok"
|
|
|
|
showEndFail :: String -> String -> Annex ()
|
|
|
|
showEndFail command file = do
|
|
|
|
liftIO $ putStrLn ""
|
|
|
|
error $ command ++ " " ++ file ++ " failed"
|