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.
|
|
|
|
-
|
2011-12-13 19:05:07 +00:00
|
|
|
- Copyright 2010, 2011 Joey Hess <joey@kitenet.net>
|
2010-10-27 20:53:54 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2010-10-14 06:36:41 +00:00
|
|
|
-}
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2011-06-30 17:16:57 +00:00
|
|
|
module Git (
|
2011-12-14 19:30:14 +00:00
|
|
|
Repo(..),
|
improve type signatures with a Ref newtype
In git, a Ref can be a Sha, or a Branch, or a Tag. I added type aliases for
those. Note that this does not prevent mixing up of eg, refs and branches
at the type level. Since git really doesn't care, except rare cases like
git update-ref, or git tag -d, that seems ok for now.
There's also a tree-ish, but let's just use Ref for it. A given Sha or Ref
may or may not be a tree-ish, depending on the object type, so there seems
no point in trying to represent it at the type level.
2011-11-16 06:23:34 +00:00
|
|
|
Ref(..),
|
|
|
|
Branch,
|
|
|
|
Sha,
|
|
|
|
Tag,
|
2010-10-22 18:05:30 +00:00
|
|
|
repoIsUrl,
|
2010-10-22 17:40:19 +00:00
|
|
|
repoIsSsh,
|
2011-08-16 23:23:56 +00:00
|
|
|
repoIsHttp,
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare,
|
2010-10-14 06:36:41 +00:00
|
|
|
repoDescribe,
|
2011-02-03 22:47:14 +00:00
|
|
|
repoLocation,
|
2010-10-14 06:36:41 +00:00
|
|
|
workTree,
|
2010-10-31 19:38:47 +00:00
|
|
|
gitDir,
|
2010-10-28 16:15:21 +00:00
|
|
|
configTrue,
|
2010-10-14 06:36:41 +00:00
|
|
|
attributes,
|
2011-09-29 23:04:24 +00:00
|
|
|
assertLocal,
|
2010-10-11 21:52:46 +00:00
|
|
|
) where
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2011-12-13 19:22:43 +00:00
|
|
|
import qualified Data.Map as M
|
2010-11-06 21:07:11 +00:00
|
|
|
import Data.Char
|
2012-01-05 18:32:20 +00:00
|
|
|
import Network.URI (uriPath, uriScheme, unEscapeString)
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2011-10-04 02:24:57 +00:00
|
|
|
import Common
|
2011-12-13 19:05:07 +00:00
|
|
|
import Git.Types
|
2011-02-04 05:56:45 +00:00
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- User-visible description of a git repo. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
repoDescribe :: Repo -> String
|
2010-10-28 17:40:10 +00:00
|
|
|
repoDescribe Repo { remoteName = Just name } = name
|
|
|
|
repoDescribe Repo { location = Url url } = show url
|
|
|
|
repoDescribe Repo { location = Dir dir } = dir
|
2011-03-28 01:43:25 +00:00
|
|
|
repoDescribe Repo { location = Unknown } = "UNKNOWN"
|
2010-10-13 18:40:56 +00:00
|
|
|
|
2011-02-03 22:47:14 +00:00
|
|
|
{- Location of the repo, either as a path or url. -}
|
|
|
|
repoLocation :: Repo -> String
|
|
|
|
repoLocation Repo { location = Url url } = show url
|
|
|
|
repoLocation Repo { location = Dir dir } = dir
|
2011-03-28 01:43:25 +00:00
|
|
|
repoLocation Repo { location = Unknown } = undefined
|
2011-02-03 22:47:14 +00:00
|
|
|
|
2010-10-22 18:05:30 +00:00
|
|
|
{- Some code needs to vary between URL and normal repos,
|
2010-10-22 16:38:20 +00:00
|
|
|
- or bare and non-bare, these functions help with that. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
repoIsUrl :: Repo -> Bool
|
2010-10-28 17:40:10 +00:00
|
|
|
repoIsUrl Repo { location = Url _ } = True
|
|
|
|
repoIsUrl _ = False
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
repoIsSsh :: Repo -> Bool
|
2010-10-28 17:40:10 +00:00
|
|
|
repoIsSsh Repo { location = Url url }
|
2011-12-14 19:30:14 +00:00
|
|
|
| scheme == "ssh:" = True
|
2010-12-14 16:46:09 +00:00
|
|
|
-- git treats these the same as ssh
|
2011-12-14 19:30:14 +00:00
|
|
|
| scheme == "git+ssh:" = True
|
|
|
|
| scheme == "ssh+git:" = True
|
2010-10-28 17:40:10 +00:00
|
|
|
| otherwise = False
|
2011-12-14 19:30:14 +00:00
|
|
|
where
|
|
|
|
scheme = uriScheme url
|
2010-10-28 17:40:10 +00:00
|
|
|
repoIsSsh _ = False
|
|
|
|
|
2011-08-16 23:23:56 +00:00
|
|
|
repoIsHttp :: Repo -> Bool
|
|
|
|
repoIsHttp Repo { location = Url url }
|
|
|
|
| uriScheme url == "http:" = True
|
|
|
|
| uriScheme url == "https:" = True
|
|
|
|
| otherwise = False
|
|
|
|
repoIsHttp _ = False
|
|
|
|
|
2011-03-03 21:33:15 +00:00
|
|
|
configAvail ::Repo -> Bool
|
2011-07-15 07:12:05 +00:00
|
|
|
configAvail Repo { config = c } = c /= M.empty
|
2011-03-03 21:33:15 +00:00
|
|
|
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare :: Repo -> Bool
|
2011-03-03 21:33:15 +00:00
|
|
|
repoIsLocalBare r@(Repo { location = Dir _ }) = configAvail r && configBare r
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare _ = False
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
assertLocal :: Repo -> a -> a
|
2010-10-22 18:05:30 +00:00
|
|
|
assertLocal repo action =
|
2010-11-22 19:46:57 +00:00
|
|
|
if not $ repoIsUrl repo
|
2010-10-12 04:53:42 +00:00
|
|
|
then action
|
2011-12-14 19:30:14 +00:00
|
|
|
else error $ "acting on non-local git repo " ++ repoDescribe repo ++
|
2010-10-12 06:51:44 +00:00
|
|
|
" not supported"
|
2011-03-03 18:51:57 +00:00
|
|
|
configBare :: Repo -> Bool
|
2012-02-25 23:15:29 +00:00
|
|
|
configBare repo = maybe unknown (fromMaybe False . configTrue) $
|
|
|
|
M.lookup "core.bare" $ config repo
|
2011-05-15 16:25:58 +00:00
|
|
|
where
|
|
|
|
unknown = error $ "it is not known if git repo " ++
|
2010-11-06 21:07:11 +00:00
|
|
|
repoDescribe repo ++
|
2010-10-12 16:23:34 +00:00
|
|
|
" is a bare repository; config not read"
|
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-14 06:36:41 +00:00
|
|
|
attributes :: Repo -> String
|
2010-10-28 17:40:10 +00:00
|
|
|
attributes repo
|
2011-03-03 18:51:57 +00:00
|
|
|
| configBare repo = workTree repo ++ "/info/.gitattributes"
|
2010-11-06 21:07:11 +00:00
|
|
|
| otherwise = workTree repo ++ "/.gitattributes"
|
2010-10-10 06:29:58 +00:00
|
|
|
|
2011-08-19 16:59:07 +00:00
|
|
|
{- Path to a repository's .git directory. -}
|
2010-10-31 19:38:47 +00:00
|
|
|
gitDir :: Repo -> String
|
|
|
|
gitDir repo
|
2011-08-19 16:59:07 +00:00
|
|
|
| configBare repo = workTree repo
|
|
|
|
| otherwise = workTree repo </> ".git"
|
2010-10-10 06:29:58 +00:00
|
|
|
|
2010-10-22 19:06:14 +00:00
|
|
|
{- Path to a repository's --work-tree, that is, its top.
|
|
|
|
-
|
2010-10-28 17:40:10 +00:00
|
|
|
- Note that for URL repositories, this is the path on the remote host. -}
|
2010-10-14 06:36:41 +00:00
|
|
|
workTree :: Repo -> FilePath
|
2012-01-05 18:32:20 +00:00
|
|
|
workTree Repo { location = Url u } = unEscapeString $ uriPath u
|
2011-12-14 19:30:14 +00:00
|
|
|
workTree Repo { location = Dir d } = d
|
2011-03-28 01:43:25 +00:00
|
|
|
workTree Repo { location = Unknown } = undefined
|
2010-10-12 04:53:42 +00:00
|
|
|
|
2011-03-28 01:43:25 +00:00
|
|
|
{- Checks if a string from git config is a true value. -}
|
2012-02-25 23:15:29 +00:00
|
|
|
configTrue :: String -> Maybe Bool
|
|
|
|
configTrue s
|
|
|
|
| s' == "true" = Just True
|
|
|
|
| s' == "false" = Just False
|
|
|
|
| otherwise = Nothing
|
|
|
|
where
|
|
|
|
s' = map toLower s
|