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,
|
|
|
|
gitRepoCurrent,
|
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-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
|
2010-10-11 04:19:38 +00:00
|
|
|
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 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,
|
|
|
|
bare :: Bool,
|
|
|
|
config :: Map String String
|
|
|
|
} | RemoteGitRepo {
|
|
|
|
url :: String,
|
|
|
|
top :: FilePath,
|
|
|
|
config :: Map String String
|
|
|
|
} deriving (Show, Read, Eq)
|
|
|
|
|
|
|
|
{- Local 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 04:53:42 +00:00
|
|
|
let r = LocalGitRepo {
|
2010-10-12 03:22:38 +00:00
|
|
|
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'
|
|
|
|
|
2010-10-12 04:53:42 +00:00
|
|
|
{- Remote GitRepo constructor. Note that remote repo config is not read.
|
|
|
|
- Throws exception on invalid url. -}
|
|
|
|
gitRepoFromUrl :: String -> IO GitRepo
|
|
|
|
gitRepoFromUrl url = 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
|
|
|
|
|
2010-10-12 05:35:32 +00:00
|
|
|
{- Some code needs to vary between remote and local repos, 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 not supported"
|
2010-10-11 21:52:46 +00:00
|
|
|
|
2010-10-10 16:35:28 +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-10 16:35:28 +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
|
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-12 03:22:38 +00:00
|
|
|
then top repo
|
|
|
|
else top repo ++ "/.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
|
2010-10-10 19:54:02 +00:00
|
|
|
absrepo = case (absNormPath "/" (top repo)) of
|
2010-10-10 02:09:10 +00:00
|
|
|
Just f -> f ++ "/"
|
2010-10-10 19:54:02 +00:00
|
|
|
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. -}
|
2010-10-11 04:19:38 +00:00
|
|
|
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
|
2010-10-12 04:53:42 +00:00
|
|
|
if (local repo)
|
|
|
|
then ["--git-dir="++(gitDir repo), "--work-tree="++(top repo)] ++ params
|
|
|
|
else error "gitCommandLine not implemented for remote repo"
|
2010-10-12 03:22:38 +00:00
|
|
|
|
|
|
|
{- Runs git in the specified repo. -}
|
|
|
|
runGit :: GitRepo -> [String] -> IO ()
|
2010-10-12 04:53:42 +00:00
|
|
|
runGit repo params =
|
|
|
|
if (local repo)
|
|
|
|
then do
|
|
|
|
r <- executeFile "git" True (gitCommandLine repo params) Nothing
|
|
|
|
return ()
|
|
|
|
else error "runGit not implemented for remote repo"
|
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
|
|
|
|
gitPipeRead repo params =
|
2010-10-12 04:53:42 +00:00
|
|
|
if (local repo)
|
|
|
|
then pOpen ReadFromPipe "git" (gitCommandLine repo params) $ \h -> do
|
|
|
|
ret <- hGetContentsStrict h
|
|
|
|
return ret
|
|
|
|
else error "gitPipeRead not implemented for remote repo"
|
2010-10-12 03:22:38 +00:00
|
|
|
|
|
|
|
{- Runs git config and populates a repo with its settings. -}
|
|
|
|
gitConfigRead :: GitRepo -> IO GitRepo
|
2010-10-12 04:53:42 +00:00
|
|
|
gitConfigRead repo =
|
|
|
|
if (local repo)
|
|
|
|
then do
|
|
|
|
c <- gitPipeRead repo ["config", "--list"]
|
|
|
|
return repo { config = gitConfigParse c }
|
|
|
|
else error "gitConfigRead not implemented for remote repo"
|
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 05:35:32 +00:00
|
|
|
Map.findWithDefault key defaultValue (config repo)
|
|
|
|
|
|
|
|
{- 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
|
|
|
|
else gitRepoFromPath r
|
2010-10-11 04:19:38 +00:00
|
|
|
|
2010-10-10 19:54:02 +00:00
|
|
|
{- 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)
|