git-annex/Core.hs

136 lines
3.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-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. -}
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-17 20:39:30 +00:00
-- handle pending commits
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-17 22:52:09 +00:00
then do
liftIO $ Git.run g ["add", gitStateDir g]
liftIO $ Git.run g ["commit", "-q", "-m",
"git-annex log update", gitStateDir g]
2010-10-15 03:52:45 +00:00
else return ()
2010-10-14 21:57:04 +00:00
2010-10-17 20:39:30 +00:00
-- clean up any files left in the temp directory
let tmp = annexTmpLocation g
exists <- liftIO $ doesDirectoryExist tmp
if (exists)
then liftIO $ removeDirectoryRecursive $ tmp
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-17 22:52:09 +00:00
{- Adds and commits a file to git.
2010-10-16 23:57:56 +00:00
-
- This is careful to not rely on the index. It may have staged changes,
- so only use operations that avoid committing such changes.
-}
2010-10-17 22:52:09 +00:00
gitAdd :: FilePath -> String -> Annex ()
2010-10-16 23:57:56 +00:00
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]
2010-10-17 22:52:09 +00:00
liftIO $ Git.run g ["commit", "--quiet",
"-m", commitmessage, file]
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
2010-10-17 22:52:09 +00:00
Annex.flagChange NeedCommit True -- commit all logs at end
2010-10-17 16:08:59 +00:00
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"