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,
|
2010-10-13 18:40:56 +00:00
|
|
|
gitRepoIsLocal,
|
|
|
|
gitRepoIsRemote,
|
|
|
|
gitConfigRemotes,
|
2010-10-13 19:55:18 +00:00
|
|
|
gitRepoDescribe,
|
2010-10-12 04:53:42 +00:00
|
|
|
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-13 18:40:56 +00:00
|
|
|
gitAttributes,
|
|
|
|
gitRepoRemoteName
|
2010-10-11 21:52:46 +00:00
|
|
|
) 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
|
2010-10-11 04:19:38 +00:00
|
|
|
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,
|
2010-10-13 18:01:17 +00:00
|
|
|
config :: Map String String,
|
|
|
|
-- remoteName holds the name used for this repo in remotes
|
|
|
|
remoteName :: Maybe String
|
2010-10-12 04:53:42 +00:00
|
|
|
} | RemoteGitRepo {
|
|
|
|
url :: String,
|
|
|
|
top :: FilePath,
|
2010-10-13 18:01:17 +00:00
|
|
|
config :: Map String String,
|
|
|
|
remoteName :: Maybe String
|
2010-10-12 04:53:42 +00:00
|
|
|
} deriving (Show, Read, Eq)
|
|
|
|
|
2010-10-13 18:51:09 +00:00
|
|
|
{- Local GitRepo constructor. -}
|
|
|
|
gitRepoFromPath :: FilePath -> GitRepo
|
|
|
|
gitRepoFromPath dir =
|
|
|
|
LocalGitRepo {
|
2010-10-12 03:22:38 +00:00
|
|
|
top = dir,
|
2010-10-13 18:01:17 +00:00
|
|
|
config = Map.empty,
|
|
|
|
remoteName = Nothing
|
2010-10-10 22:05:37 +00:00
|
|
|
}
|
2010-10-10 06:22:47 +00:00
|
|
|
|
2010-10-12 16:23:34 +00:00
|
|
|
{- Remote GitRepo constructor. Throws exception on invalid url. -}
|
2010-10-13 18:51:09 +00:00
|
|
|
gitRepoFromUrl :: String -> GitRepo
|
|
|
|
gitRepoFromUrl url =
|
|
|
|
RemoteGitRepo {
|
2010-10-12 04:53:42 +00:00
|
|
|
url = url,
|
|
|
|
top = path url,
|
2010-10-13 18:01:17 +00:00
|
|
|
config = Map.empty,
|
|
|
|
remoteName = Nothing
|
2010-10-12 04:53:42 +00:00
|
|
|
}
|
|
|
|
where path url = uriPath $ fromJust $ parseURI url
|
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- User-visible description of a git repo. -}
|
|
|
|
gitRepoDescribe repo =
|
|
|
|
if (isJust $ remoteName repo)
|
|
|
|
then fromJust $ remoteName repo
|
|
|
|
else if (gitRepoIsLocal repo)
|
|
|
|
then top repo
|
|
|
|
else url repo
|
2010-10-13 18:40:56 +00:00
|
|
|
|
|
|
|
{- Returns the name of the remote that corresponds to the repo, if
|
|
|
|
- it is a remote. Otherwise, "" -}
|
|
|
|
gitRepoRemoteName r =
|
|
|
|
if (isJust $ remoteName r)
|
|
|
|
then fromJust $ remoteName r
|
|
|
|
else ""
|
2010-10-12 16:23:34 +00:00
|
|
|
|
|
|
|
{- Some code needs to vary between remote and local repos, or bare and
|
|
|
|
- non-bare, these functions help with that. -}
|
2010-10-13 18:40:56 +00:00
|
|
|
gitRepoIsLocal repo = case (repo) of
|
2010-10-12 04:53:42 +00:00
|
|
|
LocalGitRepo {} -> True
|
|
|
|
RemoteGitRepo {} -> False
|
2010-10-13 18:40:56 +00:00
|
|
|
gitRepoIsRemote repo = not $ gitRepoIsLocal repo
|
2010-10-12 04:53:42 +00:00
|
|
|
assertlocal repo action =
|
2010-10-13 18:40:56 +00:00
|
|
|
if (gitRepoIsLocal repo)
|
2010-10-12 04:53:42 +00:00
|
|
|
then action
|
2010-10-13 19:55:18 +00:00
|
|
|
else error $ "acting on remote git repo " ++ (gitRepoDescribe repo) ++
|
2010-10-12 06:51:44 +00:00
|
|
|
" not supported"
|
2010-10-12 16:23:34 +00:00
|
|
|
bare :: GitRepo -> Bool
|
|
|
|
bare repo =
|
|
|
|
if (member b (config repo))
|
|
|
|
then ("true" == fromJust (Map.lookup b (config repo)))
|
2010-10-13 19:55:18 +00:00
|
|
|
else error $ "it is not known if git repo " ++ (gitRepoDescribe repo) ++
|
2010-10-12 16:23:34 +00:00
|
|
|
" is a bare repository; config not read"
|
|
|
|
where
|
|
|
|
b = "core.bare"
|
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-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
|
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-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. -}
|
2010-10-13 18:51:09 +00:00
|
|
|
gitConfigRemotes :: GitRepo -> [GitRepo]
|
|
|
|
gitConfigRemotes repo = map construct remotes
|
2010-10-12 05:35:32 +00:00
|
|
|
where
|
2010-10-13 18:01:17 +00:00
|
|
|
remotes = toList $ filter $ config repo
|
2010-10-12 05:35:32 +00:00
|
|
|
filter = filterWithKey (\k _ -> isremote k)
|
|
|
|
isremote k = (startswith "remote." k) && (endswith ".url" k)
|
2010-10-13 18:01:17 +00:00
|
|
|
remotename k = (split "." k) !! 1
|
2010-10-13 18:51:09 +00:00
|
|
|
construct (k,v) = (gen v) { remoteName = Just $ remotename k }
|
|
|
|
gen v = if (isURI v)
|
|
|
|
then gitRepoFromUrl v
|
|
|
|
else gitRepoFromPath v
|
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-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
|
2010-10-13 18:51:09 +00:00
|
|
|
(Just dir) -> return $ 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)
|
2010-10-12 16:23:34 +00:00
|
|
|
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)
|