git-annex/GitRepo.hs

163 lines
4.4 KiB
Haskell
Raw Normal View History

2010-10-12 03:22:38 +00:00
{- git repository handling
-
- This is written to be completely independant of git-annex and should be
- suitable for other uses.
-
- -}
2010-10-10 01:06:46 +00:00
2010-10-11 21:52:46 +00:00
module GitRepo (
GitRepo,
2010-10-12 03:41:12 +00:00
gitRepoFromPath,
2010-10-11 21:52:46 +00:00
gitRepoCurrent,
gitRepoTop,
gitDir,
gitRelative,
2010-10-12 03:22:38 +00:00
gitConfig,
2010-10-11 21:52:46 +00:00
gitAdd,
gitAttributes
) where
2010-10-10 01:06:46 +00:00
import Directory
2010-10-12 03:22:38 +00:00
import System
2010-10-10 01:06:46 +00:00
import System.Directory
2010-10-10 02:09:10 +00:00
import System.Path
import System.Cmd.Utils
import System.IO
2010-10-12 03:22:38 +00:00
import System.Posix.Process
2010-10-10 01:06:46 +00:00
import Data.String.Utils
2010-10-12 03:22:38 +00:00
import Data.Map as Map (fromList, empty, lookup, Map)
2010-10-10 03:35:05 +00:00
import Utility
2010-10-11 21:19:55 +00:00
-- a git repository
data GitRepo = GitRepo {
2010-10-12 03:22:38 +00:00
top :: FilePath,
bare :: Bool,
config :: Map String String
} deriving (Show, Read, Eq)
2010-10-10 17:47:04 +00:00
{- GitRepo constructor -}
2010-10-12 03:41:12 +00:00
gitRepoFromPath :: FilePath -> IO GitRepo
gitRepoFromPath dir = do
2010-10-10 23:14:32 +00:00
b <- isBareRepo dir
2010-10-12 03:22:38 +00:00
let r = GitRepo {
top = dir,
bare = b,
config = Map.empty
2010-10-10 22:05:37 +00:00
}
2010-10-12 03:22:38 +00:00
r' <- gitConfigRead r
2010-10-10 06:22:47 +00:00
2010-10-12 03:22:38 +00:00
return r'
{- Field accessor. -}
gitRepoTop :: GitRepo -> FilePath
gitRepoTop repo = top repo
2010-10-11 21:52:46 +00:00
{- Path to a repository's gitattributes file. -}
2010-10-10 17:47:04 +00:00
gitAttributes :: GitRepo -> IO String
gitAttributes repo = do
2010-10-10 23:14:32 +00:00
if (bare repo)
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-12 03:22:38 +00:00
gitDir :: GitRepo -> String
gitDir repo =
2010-10-10 23:14:32 +00:00
if (bare repo)
2010-10-12 03:22:38 +00:00
then top repo
else 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 :: GitRepo -> FilePath -> IO ()
2010-10-12 03:22:38 +00:00
gitAdd repo file = runGit repo ["add", file]
{- Constructs a git command line operating on the specified repo. -}
gitCommandLine :: GitRepo -> [String] -> [String]
gitCommandLine repo params =
-- force use of specified repo via --git-dir and --work-tree
["--git-dir="++(gitDir repo), "--work-tree="++(top repo)] ++ params
{- Runs git in the specified repo. -}
runGit :: GitRepo -> [String] -> IO ()
runGit repo params = do
r <- executeFile "git" True (gitCommandLine repo params) Nothing
2010-10-10 19:04:18 +00:00
return ()
2010-10-12 03:22:38 +00:00
{- Runs a git subcommand and returns its output. -}
gitPipeRead :: GitRepo -> [String] -> IO String
gitPipeRead repo params =
pOpen ReadFromPipe "git" (gitCommandLine repo params) $ \h -> do
ret <- hGetContentsStrict h
return ret
{- Runs git config and populates a repo with its settings. -}
gitConfigRead :: GitRepo -> IO GitRepo
gitConfigRead repo = do
c <- gitPipeRead repo ["config", "--list"]
2010-10-12 03:41:12 +00:00
return repo { config = gitConfigParse c }
{- Parses git config --list output into a config map. -}
gitConfigParse :: String -> Map.Map String String
gitConfigParse s = Map.fromList $ map pair $ lines s
where
pair l = (key l, val l)
key l = (keyval l) !! 0
val l = join sep $ drop 1 $ keyval l
keyval l = split sep l :: [String]
sep = "="
2010-10-12 03:22:38 +00:00
{- Returns a single git config setting, or a default value if not set. -}
gitConfig :: GitRepo -> String -> String -> String
gitConfig repo key defaultValue =
case (Map.lookup key $ config repo) of
Just value -> value
Nothing -> defaultValue
{- Finds the current git repository, which may be in a parent directory. -}
2010-10-11 21:52:46 +00:00
gitRepoCurrent :: IO GitRepo
gitRepoCurrent = 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
2010-10-12 03:41:12 +00:00
(Just dir) -> gitRepoFromPath 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)