git-annex/GitRepo.hs

214 lines
6.3 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 06:40:09 +00:00
gitRepoFromCwd,
2010-10-12 04:53:42 +00:00
gitRepoFromPath,
gitRepoFromUrl,
gitWorkTree,
2010-10-11 21:52:46 +00:00
gitDir,
gitRelative,
2010-10-12 03:22:38 +00:00
gitConfig,
2010-10-12 17:12:47 +00:00
gitConfigRead,
2010-10-12 17:02:41 +00:00
gitRun,
2010-10-11 21:52:46 +00:00
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-12 16:47:11 +00:00
import System.Posix.Directory
2010-10-10 02:09:10 +00:00
import System.Path
2010-10-12 21:43:54 +00:00
import System.Cmd
import System.Cmd.Utils
import System.IO
2010-10-12 16:47:11 +00:00
import IO (bracket_)
2010-10-10 01:06:46 +00:00
import Data.String.Utils
2010-10-12 05:35:32 +00:00
import Data.Map as Map hiding (map, split)
2010-10-12 04:53:42 +00:00
import Network.URI
import Maybe
2010-10-10 03:35:05 +00:00
import Utility
2010-10-11 21:19:55 +00:00
2010-10-12 06:00:29 +00:00
{- A git repository can be on local disk or remote. Not to be confused
- with a git repo's configured remotes, some of which may be on local
- disk. -}
2010-10-12 04:53:42 +00:00
data GitRepo =
LocalGitRepo {
top :: FilePath,
config :: Map String String
} | RemoteGitRepo {
url :: String,
top :: FilePath,
config :: Map String String
} deriving (Show, Read, Eq)
{- Local GitRepo constructor. Can optionally query the repo for its config. -}
gitRepoFromPath :: FilePath -> Bool -> IO GitRepo
gitRepoFromPath dir query = do
2010-10-12 04:53:42 +00:00
let r = LocalGitRepo {
2010-10-12 03:22:38 +00:00
top = dir,
config = Map.empty
2010-10-10 22:05:37 +00:00
}
if (query)
then gitConfigRead r
else return r
2010-10-10 06:22:47 +00:00
{- Remote GitRepo constructor. Throws exception on invalid url. -}
gitRepoFromUrl :: String -> Bool -> IO GitRepo
gitRepoFromUrl url query = do
2010-10-12 05:35:32 +00:00
return $ RemoteGitRepo {
2010-10-12 04:53:42 +00:00
url = url,
top = path url,
config = Map.empty
}
where path url = uriPath $ fromJust $ parseURI url
{- User-visible description of a git repo by path or url -}
describe repo = if (local repo) then top repo else url repo
{- Some code needs to vary between remote and local repos, or bare and
- non-bare, these functions help with that. -}
2010-10-12 04:53:42 +00:00
local repo = case (repo) of
LocalGitRepo {} -> True
RemoteGitRepo {} -> False
remote repo = not $ local repo
assertlocal repo action =
if (local repo)
then action
else error $ "acting on remote git repo " ++ (describe repo) ++
2010-10-12 06:51:44 +00:00
" not supported"
bare :: GitRepo -> Bool
bare repo =
if (member b (config repo))
then ("true" == fromJust (Map.lookup b (config repo)))
else error $ "it is not known if git repo " ++ (describe repo) ++
" is a bare repository; config not read"
where
b = "core.bare"
2010-10-11 21:52:46 +00:00
{- Path to a repository's gitattributes file. -}
2010-10-12 04:53:42 +00:00
gitAttributes :: GitRepo -> String
gitAttributes repo = assertlocal repo $ do
2010-10-10 23:14:32 +00:00
if (bare repo)
2010-10-12 04:53:42 +00:00
then (top repo) ++ "/info/.gitattributes"
else (top repo) ++ "/.gitattributes"
2010-10-10 06:29:58 +00:00
2010-10-13 04:58:59 +00:00
{- Path to a repository's .git directory, relative to its topdir. -}
2010-10-12 03:22:38 +00:00
gitDir :: GitRepo -> String
2010-10-12 04:53:42 +00:00
gitDir repo = assertlocal repo $
2010-10-10 23:14:32 +00:00
if (bare repo)
2010-10-13 04:58:59 +00:00
then ""
else ".git"
2010-10-10 06:29:58 +00:00
2010-10-12 04:53:42 +00:00
{- Path to a repository's --work-tree. -}
gitWorkTree :: GitRepo -> FilePath
gitWorkTree repo = top repo
{- Given a relative or absolute filename in a repository, calculates the
- name to use to refer to the file relative to a git repository's top.
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-12 03:22:38 +00:00
{- Constructs a git command line operating on the specified repo. -}
gitCommandLine :: GitRepo -> [String] -> [String]
2010-10-12 06:51:44 +00:00
gitCommandLine repo params = assertlocal repo $
2010-10-12 03:22:38 +00:00
-- force use of specified repo via --git-dir and --work-tree
2010-10-13 04:58:59 +00:00
["--git-dir="++(top repo)++"/"++(gitDir repo), "--work-tree="++(top repo)] ++ params
2010-10-12 03:22:38 +00:00
{- Runs git in the specified repo. -}
2010-10-12 17:02:41 +00:00
gitRun :: GitRepo -> [String] -> IO ()
gitRun repo params = assertlocal repo $ do
2010-10-12 21:43:54 +00:00
r <- rawSystem "git" (gitCommandLine repo params)
2010-10-12 06:51:44 +00:00
return ()
2010-10-10 19:04:18 +00:00
2010-10-12 03:22:38 +00:00
{- Runs a git subcommand and returns its output. -}
gitPipeRead :: GitRepo -> [String] -> IO String
2010-10-12 06:51:44 +00:00
gitPipeRead repo params = assertlocal repo $ do
pOpen ReadFromPipe "git" (gitCommandLine repo params) $ \h -> do
ret <- hGetContentsStrict h
return ret
2010-10-12 03:22:38 +00:00
2010-10-12 16:47:11 +00:00
{- Runs git config and populates a repo with its config. -}
2010-10-12 03:22:38 +00:00
gitConfigRead :: GitRepo -> IO GitRepo
2010-10-12 06:51:44 +00:00
gitConfigRead repo = assertlocal repo $ do
2010-10-12 16:47:11 +00:00
{- Cannot use gitPipeRead because it relies on the config having
been already read. Instead, chdir to the repo. -}
cwd <- getCurrentDirectory
bracket_ (changeWorkingDirectory (top repo))
(\_ -> changeWorkingDirectory cwd) $ do
pOpen ReadFromPipe "git" ["config", "--list"] $ \h -> do
val <- hGetContentsStrict h
return repo { config = gitConfigParse val }
2010-10-12 03:41:12 +00:00
{- 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 =
2010-10-12 16:31:19 +00:00
Map.findWithDefault defaultValue key (config repo)
2010-10-12 05:35:32 +00:00
{- Returns a list of a repo's configured remotes. -}
gitConfigRemotes :: GitRepo -> IO [GitRepo]
gitConfigRemotes repo = mapM construct remotes
where
remotes = elems $ filter $ config repo
filter = filterWithKey (\k _ -> isremote k)
isremote k = (startswith "remote." k) && (endswith ".url" k)
construct r =
if (isURI r)
then gitRepoFromUrl r False
else gitRepoFromPath r False
{- Finds the current git repository, which may be in a parent directory. -}
2010-10-12 06:40:09 +00:00
gitRepoFromCwd :: IO GitRepo
gitRepoFromCwd = 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) -> gitRepoFromPath dir True
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)
where
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)