git-annex/GitRepo.hs

91 lines
2.4 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 22:05:37 +00:00
import Types
import BackendList
2010-10-10 17:47:04 +00:00
{- GitRepo constructor -}
gitRepo :: FilePath -> IO GitRepo
gitRepo dir = do
-- TOOD query repo for configuration settings; other repositories; etc
2010-10-10 22:05:37 +00:00
return GitRepo {
top = dir,
remotes = [],
backends = supportedBackends
}
2010-10-10 06:22:47 +00:00
{- Path to a repository's gitattributes file. -}
2010-10-10 17:47:04 +00:00
gitAttributes :: GitRepo -> IO String
gitAttributes repo = do
bare <- isBareRepo (top repo)
2010-10-10 06:29:58 +00:00
if (bare)
then return $ (top repo) ++ "/info/.gitattributes"
else return $ (top repo) ++ "/.gitattributes"
2010-10-10 06:29:58 +00:00
{- Path to a repository's .git directory.
2010-10-10 06:29:58 +00:00
- (For a bare repository, that is the root of the repository.)
- TODO: support GIT_DIR -}
2010-10-10 17:47:04 +00:00
gitDir :: GitRepo -> IO String
gitDir repo = do
bare <- isBareRepo (top repo)
2010-10-10 06:29:58 +00:00
if (bare)
then return $ (top repo)
else return $ (top repo) ++ "/.git"
2010-10-10 06:29:58 +00:00
2010-10-10 02:09:10 +00:00
{- Given a relative or absolute filename, calculates the name to use
- to refer to the file relative to a git repository directory.
2010-10-10 02:09:10 +00:00
- This is the same form displayed and used by git. -}
2010-10-10 17:47:04 +00:00
gitRelative :: GitRepo -> 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 "/" (top repo)) of
2010-10-10 02:09:10 +00:00
Just f -> f ++ "/"
Nothing -> error $ "bad repo" ++ (top repo)
2010-10-10 02:09:10 +00:00
absfile = case (secureAbsNormPath absrepo file) of
Just f -> f
Nothing -> error $ file ++ " is not located inside git repository " ++ absrepo
2010-10-10 19:04:18 +00:00
{- Stages a changed file in git's index. -}
gitAdd repo file = do
-- TODO
return ()
{- Finds the current git repository, which may be in a parent directory. -}
currentRepo :: IO GitRepo
currentRepo = 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) -> gitRepo dir
2010-10-10 01:06:46 +00:00
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)