git-annex/GitRepo.hs

100 lines
2.8 KiB
Haskell
Raw Normal View History

2010-10-10 01:06:46 +00:00
{- git repository handling -}
module GitRepo where
import Directory
import System.Directory
2010-10-10 02:09:10 +00:00
import System.Path
2010-10-10 01:06:46 +00:00
import Data.String.Utils
2010-10-10 03:35:05 +00:00
import Utility
2010-10-10 01:06:46 +00:00
2010-10-10 06:22:47 +00:00
{- Long-term state is stored in files inside the .git-annex directory
- in the git repository. -}
stateLoc = ".git-annex"
gitStateDir :: String -> String
gitStateDir repo = repo ++ "/" ++ stateLoc ++ "/"
2010-10-10 06:29:58 +00:00
{- Path to the current repository's gitattributes file. -}
gitAttributes :: IO String
gitAttributes = do
repo <- repoTop
bare <- isBareRepo repo
if (bare)
then return $ repo ++ "/info/.gitattributes"
else return $ repo ++ "/.gitattributes"
{- Path to the current repository's .git directory.
- (For a bare repository, that is the root of the repository.)
- TODO: support GIT_DIR -}
gitDir :: IO String
gitDir = do
repo <- repoTop
bare <- isBareRepo repo
if (bare)
then return $ repo
else return $ repo ++ "/.git"
2010-10-10 02:09:10 +00:00
{- Given a relative or absolute filename, calculates the name to use
- relative to a git repository directory (which must be absolute).
- This is the same form displayed and used by git. -}
gitRelative :: String -> String -> String
2010-10-10 02:14:13 +00:00
gitRelative repo file = drop (length absrepo) absfile
2010-10-10 02:09:10 +00:00
where
-- normalize both repo and file, so that repo
-- will be substring of file
absrepo = case (absNormPath "/" repo) of
Just f -> f ++ "/"
Nothing -> error $ "bad repo" ++ repo
absfile = case (secureAbsNormPath absrepo file) of
Just f -> f
Nothing -> error $ file ++ " is not located inside git repository " ++ absrepo
2010-10-10 06:22:47 +00:00
{- Sets up the current git repo for git-annex. May be called repeatedly. -}
gitPrep :: IO ()
gitPrep = do
-- configure git to use union merge driver on state files
let attrLine = stateLoc ++ "/* merge=union"
attributes <- gitAttributes
2010-10-10 06:22:47 +00:00
exists <- doesFileExist attributes
if (not exists)
then writeFile attributes $ attrLine ++ "\n"
else do
content <- readFile attributes
if (all (/= attrLine) (lines content))
2010-10-10 06:29:58 +00:00
then do
appendFile attributes $ attrLine ++ "\n"
-- TODO check attributes file into git?
else return ()
2010-10-10 01:06:46 +00:00
{- Finds the top of the current git repository, which may be in a parent
- directory. -}
repoTop :: IO String
repoTop = do
2010-10-10 02:09:10 +00:00
cwd <- getCurrentDirectory
top <- seekUp cwd isRepoTop
2010-10-10 01:06:46 +00:00
case top of
(Just dir) -> return dir
Nothing -> error "Not in a git repository."
seekUp :: String -> (String -> IO Bool) -> IO (Maybe String)
seekUp dir want = do
ok <- want dir
if ok
then return (Just dir)
else case (parentDir dir) of
2010-10-10 03:35:05 +00:00
"" -> return Nothing
d -> seekUp d want
2010-10-10 01:06:46 +00:00
isRepoTop dir = do
r <- isGitRepo dir
b <- isBareRepo dir
return (r || b)
isGitRepo dir = gitSignature dir ".git" ".git/config"
isBareRepo dir = gitSignature dir "objects" "config"
gitSignature dir subdir file = do
s <- (doesDirectoryExist (dir ++ "/" ++ subdir))
f <- (doesFileExist (dir ++ "/" ++ file))
return (s && f)