2011-12-13 19:05:07 +00:00
|
|
|
{- git data types
|
|
|
|
-
|
2020-04-13 17:35:22 +00:00
|
|
|
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
|
2011-12-13 19:05:07 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-12-13 19:05:07 +00:00
|
|
|
-}
|
|
|
|
|
2020-11-02 20:31:28 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances #-}
|
2019-11-25 20:18:19 +00:00
|
|
|
|
2011-12-13 19:05:07 +00:00
|
|
|
module Git.Types where
|
|
|
|
|
|
|
|
import Network.URI
|
2019-12-02 14:57:09 +00:00
|
|
|
import Data.String
|
2019-12-05 18:36:43 +00:00
|
|
|
import Data.Default
|
2011-12-13 19:05:07 +00:00
|
|
|
import qualified Data.Map as M
|
2019-11-25 20:18:19 +00:00
|
|
|
import qualified Data.ByteString as S
|
2013-10-22 16:58:04 +00:00
|
|
|
import System.Posix.Types
|
2013-11-05 17:38:37 +00:00
|
|
|
import Utility.SafeCommand
|
2019-12-02 14:57:09 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2020-04-13 17:35:22 +00:00
|
|
|
import qualified Data.Semigroup as Sem
|
|
|
|
import Prelude
|
2019-11-25 20:18:19 +00:00
|
|
|
|
Clean up handling of git directory and git worktree.
Baked into the code was an assumption that a repository's git directory
could be determined by adding ".git" to its work tree (or nothing for bare
repos). That fails when core.worktree, or GIT_DIR and GIT_WORK_TREE are
used to separate the two.
This was attacked at the type level, by storing the gitdir and worktree
separately, so Nothing for the worktree means a bare repo.
A complication arose because we don't learn where a repository is bare
until its configuration is read. So another Location type handles
repositories that have not had their config read yet. I am not entirely
happy with this being a Location type, rather than representing them
entirely separate from the Git type. The new code is not worse than the
old, but better types could enforce more safety.
Added support for core.worktree. Overriding it with -c isn't supported
because it's not really clear what to do if a git repo's config is read, is
not bare, and is then overridden to bare. What is the right git directory
in this case? I will worry about this if/when someone has a use case for
overriding core.worktree with -c. (See Git.Config.updateLocation)
Also removed and renamed some functions like gitDir and workTree that
misused git's terminology.
One minor regression is known: git annex add in a bare repository does not
print a nice error message, but runs git ls-files in a way that fails
earlier with a less nice error message. This is because before --work-tree
was always passed to git commands, even in a bare repo, while now it's not.
2012-05-18 20:38:26 +00:00
|
|
|
{- Support repositories on local disk, and repositories accessed via an URL.
|
|
|
|
-
|
|
|
|
- Repos on local disk have a git directory, and unless bare, a worktree.
|
|
|
|
-
|
|
|
|
- A local repo may not have had its config read yet, in which case all
|
|
|
|
- that's known about it is its path.
|
|
|
|
-
|
|
|
|
- Finally, an Unknown repository may be known to exist, but nothing
|
|
|
|
- else known about it.
|
|
|
|
-}
|
|
|
|
data RepoLocation
|
2019-12-09 17:49:05 +00:00
|
|
|
= Local { gitdir :: RawFilePath, worktree :: Maybe RawFilePath }
|
|
|
|
| LocalUnknown RawFilePath
|
Clean up handling of git directory and git worktree.
Baked into the code was an assumption that a repository's git directory
could be determined by adding ".git" to its work tree (or nothing for bare
repos). That fails when core.worktree, or GIT_DIR and GIT_WORK_TREE are
used to separate the two.
This was attacked at the type level, by storing the gitdir and worktree
separately, so Nothing for the worktree means a bare repo.
A complication arose because we don't learn where a repository is bare
until its configuration is read. So another Location type handles
repositories that have not had their config read yet. I am not entirely
happy with this being a Location type, rather than representing them
entirely separate from the Git type. The new code is not worse than the
old, but better types could enforce more safety.
Added support for core.worktree. Overriding it with -c isn't supported
because it's not really clear what to do if a git repo's config is read, is
not bare, and is then overridden to bare. What is the right git directory
in this case? I will worry about this if/when someone has a use case for
overriding core.worktree with -c. (See Git.Config.updateLocation)
Also removed and renamed some functions like gitDir and workTree that
misused git's terminology.
One minor regression is known: git annex add in a bare repository does not
print a nice error message, but runs git ls-files in a way that fails
earlier with a less nice error message. This is because before --work-tree
was always passed to git commands, even in a bare repo, while now it's not.
2012-05-18 20:38:26 +00:00
|
|
|
| Url URI
|
2021-01-18 18:52:56 +00:00
|
|
|
| UnparseableUrl String
|
Clean up handling of git directory and git worktree.
Baked into the code was an assumption that a repository's git directory
could be determined by adding ".git" to its work tree (or nothing for bare
repos). That fails when core.worktree, or GIT_DIR and GIT_WORK_TREE are
used to separate the two.
This was attacked at the type level, by storing the gitdir and worktree
separately, so Nothing for the worktree means a bare repo.
A complication arose because we don't learn where a repository is bare
until its configuration is read. So another Location type handles
repositories that have not had their config read yet. I am not entirely
happy with this being a Location type, rather than representing them
entirely separate from the Git type. The new code is not worse than the
old, but better types could enforce more safety.
Added support for core.worktree. Overriding it with -c isn't supported
because it's not really clear what to do if a git repo's config is read, is
not bare, and is then overridden to bare. What is the right git directory
in this case? I will worry about this if/when someone has a use case for
overriding core.worktree with -c. (See Git.Config.updateLocation)
Also removed and renamed some functions like gitDir and workTree that
misused git's terminology.
One minor regression is known: git annex add in a bare repository does not
print a nice error message, but runs git ls-files in a way that fails
earlier with a less nice error message. This is because before --work-tree
was always passed to git commands, even in a bare repo, while now it's not.
2012-05-18 20:38:26 +00:00
|
|
|
| Unknown
|
2014-04-06 23:06:03 +00:00
|
|
|
deriving (Show, Eq, Ord)
|
2011-12-13 19:05:07 +00:00
|
|
|
|
2012-08-25 00:50:39 +00:00
|
|
|
data Repo = Repo
|
|
|
|
{ location :: RepoLocation
|
2019-12-05 18:36:43 +00:00
|
|
|
, config :: M.Map ConfigKey ConfigValue
|
2011-12-30 18:07:17 +00:00
|
|
|
-- a given git config key can actually have multiple values
|
2019-12-05 18:36:43 +00:00
|
|
|
, fullconfig :: M.Map ConfigKey [ConfigValue]
|
2018-01-09 19:36:56 +00:00
|
|
|
-- remoteName holds the name used for this repo in some other
|
|
|
|
-- repo's list of remotes, when this repo is such a remote
|
2013-11-07 22:02:00 +00:00
|
|
|
, remoteName :: Maybe RemoteName
|
2012-08-25 00:50:39 +00:00
|
|
|
-- alternate environment to use when running git commands
|
|
|
|
, gitEnv :: Maybe [(String, String)]
|
2016-04-08 18:24:00 +00:00
|
|
|
, gitEnvOverridesGitDir :: Bool
|
2013-11-05 17:38:37 +00:00
|
|
|
-- global options to pass to git when running git commands
|
|
|
|
, gitGlobalOpts :: [CommandParam]
|
2022-09-20 18:52:43 +00:00
|
|
|
-- True only when --git-dir or GIT_DIR was used
|
|
|
|
, gitDirSpecifiedExplicitly :: Bool
|
2023-09-07 18:56:26 +00:00
|
|
|
-- Use when the path to the repository was specified explicitly,
|
|
|
|
-- eg in a git remote, and so it's safe to set
|
|
|
|
-- -c safe.directory=* and -c safe.bareRepository=all
|
|
|
|
-- when using this repository.
|
|
|
|
, repoPathSpecifiedExplicitly :: Bool
|
2014-04-06 23:06:03 +00:00
|
|
|
} deriving (Show, Eq, Ord)
|
2011-12-13 19:05:07 +00:00
|
|
|
|
2019-12-02 14:57:09 +00:00
|
|
|
newtype ConfigKey = ConfigKey S.ByteString
|
|
|
|
deriving (Ord, Eq)
|
|
|
|
|
2020-04-13 17:35:22 +00:00
|
|
|
data ConfigValue
|
|
|
|
= ConfigValue S.ByteString
|
|
|
|
| NoConfigValue
|
|
|
|
-- ^ git treats a setting with no value as different than a setting
|
|
|
|
-- with an empty value
|
|
|
|
deriving (Ord, Eq)
|
|
|
|
|
|
|
|
instance Sem.Semigroup ConfigValue where
|
|
|
|
ConfigValue a <> ConfigValue b = ConfigValue (a <> b)
|
|
|
|
a <> NoConfigValue = a
|
|
|
|
NoConfigValue <> b = b
|
|
|
|
|
|
|
|
instance Monoid ConfigValue where
|
|
|
|
mempty = ConfigValue mempty
|
2019-12-05 18:36:43 +00:00
|
|
|
|
|
|
|
instance Default ConfigValue where
|
|
|
|
def = ConfigValue mempty
|
|
|
|
|
2019-12-02 14:57:09 +00:00
|
|
|
fromConfigKey :: ConfigKey -> String
|
2021-08-11 00:45:02 +00:00
|
|
|
fromConfigKey (ConfigKey s) = decodeBS s
|
2019-12-02 14:57:09 +00:00
|
|
|
|
|
|
|
instance Show ConfigKey where
|
|
|
|
show = fromConfigKey
|
|
|
|
|
2020-11-02 20:31:28 +00:00
|
|
|
class FromConfigValue a where
|
|
|
|
fromConfigValue :: ConfigValue -> a
|
|
|
|
|
|
|
|
instance FromConfigValue S.ByteString where
|
|
|
|
fromConfigValue (ConfigValue s) = s
|
|
|
|
fromConfigValue NoConfigValue = mempty
|
|
|
|
|
|
|
|
instance FromConfigValue String where
|
2021-08-11 00:45:02 +00:00
|
|
|
fromConfigValue = decodeBS . fromConfigValue
|
2019-12-05 18:36:43 +00:00
|
|
|
|
|
|
|
instance Show ConfigValue where
|
|
|
|
show = fromConfigValue
|
|
|
|
|
2019-12-02 14:57:09 +00:00
|
|
|
instance IsString ConfigKey where
|
2021-08-11 00:45:02 +00:00
|
|
|
fromString = ConfigKey . encodeBS
|
2019-12-02 14:57:09 +00:00
|
|
|
|
2019-12-05 18:36:43 +00:00
|
|
|
instance IsString ConfigValue where
|
2021-08-11 00:45:02 +00:00
|
|
|
fromString = ConfigValue . encodeBS
|
2019-12-05 18:36:43 +00:00
|
|
|
|
2013-11-07 22:02:00 +00:00
|
|
|
type RemoteName = String
|
|
|
|
|
2011-12-13 19:05:07 +00:00
|
|
|
{- A git ref. Can be a sha1, or a branch or tag name. -}
|
2020-04-06 21:14:49 +00:00
|
|
|
newtype Ref = Ref S.ByteString
|
2014-02-19 05:09:17 +00:00
|
|
|
deriving (Eq, Ord, Read, Show)
|
2011-12-13 19:05:07 +00:00
|
|
|
|
2020-04-07 15:54:27 +00:00
|
|
|
fromRef :: Ref -> String
|
2021-08-11 00:45:02 +00:00
|
|
|
fromRef = decodeBS . fromRef'
|
2020-04-07 15:54:27 +00:00
|
|
|
|
|
|
|
fromRef' :: Ref -> S.ByteString
|
|
|
|
fromRef' (Ref s) = s
|
2011-12-13 19:05:07 +00:00
|
|
|
|
|
|
|
{- Aliases for Ref. -}
|
|
|
|
type Branch = Ref
|
|
|
|
type Sha = Ref
|
|
|
|
type Tag = Ref
|
2012-06-06 06:31:31 +00:00
|
|
|
|
2014-02-06 16:43:56 +00:00
|
|
|
{- A date in the format described in gitrevisions. Includes the
|
|
|
|
- braces, eg, "{yesterday}" -}
|
|
|
|
newtype RefDate = RefDate String
|
|
|
|
|
2012-06-06 06:31:31 +00:00
|
|
|
{- Types of objects that can be stored in git. -}
|
|
|
|
data ObjectType = BlobObject | CommitObject | TreeObject
|
2020-04-10 18:03:40 +00:00
|
|
|
deriving (Show)
|
2012-06-06 06:31:31 +00:00
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
readObjectType :: S.ByteString -> Maybe ObjectType
|
2012-06-06 06:31:31 +00:00
|
|
|
readObjectType "blob" = Just BlobObject
|
|
|
|
readObjectType "commit" = Just CommitObject
|
|
|
|
readObjectType "tree" = Just TreeObject
|
|
|
|
readObjectType _ = Nothing
|
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
fmtObjectType :: ObjectType -> S.ByteString
|
|
|
|
fmtObjectType BlobObject = "blob"
|
|
|
|
fmtObjectType CommitObject = "commit"
|
|
|
|
fmtObjectType TreeObject = "tree"
|
|
|
|
|
2018-05-14 18:22:44 +00:00
|
|
|
{- Types of items in a tree. -}
|
2020-12-22 18:06:40 +00:00
|
|
|
data TreeItemType
|
|
|
|
= TreeFile
|
|
|
|
| TreeExecutable
|
|
|
|
| TreeSymlink
|
|
|
|
| TreeSubmodule
|
|
|
|
| TreeSubtree
|
2019-12-06 17:58:28 +00:00
|
|
|
deriving (Eq, Show)
|
2012-06-06 18:26:15 +00:00
|
|
|
|
2018-05-14 18:22:44 +00:00
|
|
|
{- Git uses magic numbers to denote the type of a tree item. -}
|
2019-11-25 20:18:19 +00:00
|
|
|
readTreeItemType :: S.ByteString -> Maybe TreeItemType
|
2018-05-14 18:22:44 +00:00
|
|
|
readTreeItemType "100644" = Just TreeFile
|
|
|
|
readTreeItemType "100755" = Just TreeExecutable
|
|
|
|
readTreeItemType "120000" = Just TreeSymlink
|
|
|
|
readTreeItemType "160000" = Just TreeSubmodule
|
2020-12-22 18:06:40 +00:00
|
|
|
readTreeItemType "040000" = Just TreeSubtree
|
2018-05-14 18:22:44 +00:00
|
|
|
readTreeItemType _ = Nothing
|
|
|
|
|
2019-11-25 20:18:19 +00:00
|
|
|
fmtTreeItemType :: TreeItemType -> S.ByteString
|
2018-05-14 18:22:44 +00:00
|
|
|
fmtTreeItemType TreeFile = "100644"
|
|
|
|
fmtTreeItemType TreeExecutable = "100755"
|
|
|
|
fmtTreeItemType TreeSymlink = "120000"
|
|
|
|
fmtTreeItemType TreeSubmodule = "160000"
|
2020-12-22 18:06:40 +00:00
|
|
|
fmtTreeItemType TreeSubtree = "040000"
|
2018-05-14 18:22:44 +00:00
|
|
|
|
|
|
|
toTreeItemType :: FileMode -> Maybe TreeItemType
|
|
|
|
toTreeItemType 0o100644 = Just TreeFile
|
|
|
|
toTreeItemType 0o100755 = Just TreeExecutable
|
|
|
|
toTreeItemType 0o120000 = Just TreeSymlink
|
|
|
|
toTreeItemType 0o160000 = Just TreeSubmodule
|
2020-12-22 18:06:40 +00:00
|
|
|
toTreeItemType 0o040000 = Just TreeSubtree
|
2018-05-14 18:22:44 +00:00
|
|
|
toTreeItemType _ = Nothing
|
|
|
|
|
|
|
|
fromTreeItemType :: TreeItemType -> FileMode
|
|
|
|
fromTreeItemType TreeFile = 0o100644
|
|
|
|
fromTreeItemType TreeExecutable = 0o100755
|
|
|
|
fromTreeItemType TreeSymlink = 0o120000
|
|
|
|
fromTreeItemType TreeSubmodule = 0o160000
|
2020-12-22 18:06:40 +00:00
|
|
|
fromTreeItemType TreeSubtree = 0o040000
|
2016-02-25 19:34:22 +00:00
|
|
|
|
2016-02-25 18:59:35 +00:00
|
|
|
data Commit = Commit
|
|
|
|
{ commitTree :: Sha
|
2016-03-11 16:47:14 +00:00
|
|
|
, commitParent :: [Sha]
|
2016-02-25 18:59:35 +00:00
|
|
|
, commitAuthorMetaData :: CommitMetaData
|
|
|
|
, commitCommitterMetaData :: CommitMetaData
|
|
|
|
, commitMessage :: String
|
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
data CommitMetaData = CommitMetaData
|
|
|
|
{ commitName :: Maybe String
|
|
|
|
, commitEmail :: Maybe String
|
|
|
|
, commitDate :: Maybe String -- In raw git form, "epoch -tzoffset"
|
|
|
|
}
|
|
|
|
deriving (Show)
|