2010-10-12 03:22:38 +00:00
|
|
|
{- git repository handling
|
|
|
|
-
|
2023-03-14 02:39:16 +00:00
|
|
|
- This is written to be completely independent of git-annex and should be
|
2010-10-12 03:22:38 +00:00
|
|
|
- suitable for other uses.
|
|
|
|
-
|
2021-01-18 18:52:56 +00:00
|
|
|
- Copyright 2010-2021 Joey Hess <id@joeyh.name>
|
2010-10-27 20:53:54 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2010-10-14 06:36:41 +00:00
|
|
|
-}
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2020-10-28 19:40:50 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2013-05-10 21:29:59 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
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(..),
|
2014-02-19 05:09:17 +00:00
|
|
|
fromRef,
|
2020-04-07 15:54:27 +00:00
|
|
|
fromRef',
|
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
|
|
|
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,
|
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
|
|
|
repoIsLocal,
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare,
|
2012-06-26 21:15:17 +00:00
|
|
|
repoIsLocalUnknown,
|
2010-10-14 06:36:41 +00:00
|
|
|
repoDescribe,
|
2011-02-03 22:47:14 +00:00
|
|
|
repoLocation,
|
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
|
|
|
repoPath,
|
2016-05-06 16:26:37 +00:00
|
|
|
repoWorkTree,
|
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
|
|
|
localGitDir,
|
2010-10-14 06:36:41 +00:00
|
|
|
attributes,
|
2015-12-04 21:57:15 +00:00
|
|
|
attributesLocal,
|
2012-03-14 16:01:56 +00:00
|
|
|
hookPath,
|
2011-09-29 23:04:24 +00:00
|
|
|
assertLocal,
|
2015-01-06 18:50:15 +00:00
|
|
|
adjustPath,
|
2015-01-06 19:31:24 +00:00
|
|
|
relPath,
|
2010-10-11 21:52:46 +00:00
|
|
|
) where
|
2010-10-10 01:06:46 +00:00
|
|
|
|
2020-10-28 19:40:50 +00:00
|
|
|
import qualified Data.ByteString as B
|
2012-01-05 18:32:20 +00:00
|
|
|
import Network.URI (uriPath, uriScheme, unEscapeString)
|
2013-08-04 17:07:55 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-03-14 16:17:38 +00:00
|
|
|
import System.Posix.Files
|
2013-05-10 21:29:59 +00:00
|
|
|
#endif
|
2020-10-28 21:25:59 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
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
|
2013-08-04 17:12:18 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-03-14 16:17:38 +00:00
|
|
|
import Utility.FileMode
|
2013-08-04 17:12:18 +00:00
|
|
|
#endif
|
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
|
2021-01-18 18:52:56 +00:00
|
|
|
repoDescribe Repo { location = UnparseableUrl url } = url
|
2019-12-09 17:49:05 +00:00
|
|
|
repoDescribe Repo { location = Local { worktree = Just dir } } = fromRawFilePath dir
|
|
|
|
repoDescribe Repo { location = Local { gitdir = dir } } = fromRawFilePath dir
|
|
|
|
repoDescribe Repo { location = LocalUnknown dir } = fromRawFilePath 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
|
2021-01-18 18:52:56 +00:00
|
|
|
repoLocation Repo { location = UnparseableUrl url } = url
|
2019-12-09 17:49:05 +00:00
|
|
|
repoLocation Repo { location = Local { worktree = Just dir } } = fromRawFilePath dir
|
|
|
|
repoLocation Repo { location = Local { gitdir = dir } } = fromRawFilePath dir
|
|
|
|
repoLocation Repo { location = LocalUnknown dir } = fromRawFilePath dir
|
2023-04-10 17:38:14 +00:00
|
|
|
repoLocation Repo { location = Unknown } = giveup "unknown repoLocation"
|
2011-02-03 22:47:14 +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
|
|
|
{- Path to a repository. For non-bare, this is the worktree, for bare,
|
reinject: Fix crash when reinjecting a file from outside the repository
Commit 4bf7940d6b912fbf692b268f621ebd41ed871125 introduced this
problem, but was otherwise doing a good thing. Problem being
that fileRef "/foo" used to return ":./foo", which was actually wrong,
but as long as there was no foo in the local repository, catKey
could operate on it without crashing. After that fix though, fileRef
would return eg "../../foo", resulting in fileRef returning
":./../../foo", which will make git cat-file crash since that's
not a valid path in the repo.
Fix is simply to make fileRef detect paths outside the repo and return
Nothing. Then catKey can be skipped. This needed several bugfixes to
dirContains as well, in previous commits.
In Command.Smudge, this led to needing to check for Nothing. That case
should actually never happen, because the fileoutsiderepo check will
detect it earlier.
Sponsored-by: Brock Spratlen on Patreon
2021-10-01 18:04:18 +00:00
|
|
|
- it's the gitdir, and for URL repositories, is the path on the remote
|
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
|
|
|
- host. -}
|
2019-12-09 17:49:05 +00:00
|
|
|
repoPath :: Repo -> RawFilePath
|
|
|
|
repoPath Repo { location = Url u } = toRawFilePath $ unEscapeString $ uriPath u
|
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
|
|
|
repoPath Repo { location = Local { worktree = Just d } } = d
|
|
|
|
repoPath Repo { location = Local { gitdir = d } } = d
|
|
|
|
repoPath Repo { location = LocalUnknown dir } = dir
|
2023-04-10 17:38:14 +00:00
|
|
|
repoPath Repo { location = Unknown } = giveup "unknown repoPath"
|
|
|
|
repoPath Repo { location = UnparseableUrl _u } = giveup "unknown repoPath"
|
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
|
|
|
|
2019-12-09 17:49:05 +00:00
|
|
|
repoWorkTree :: Repo -> Maybe RawFilePath
|
2016-05-06 16:26:37 +00:00
|
|
|
repoWorkTree Repo { location = Local { worktree = Just d } } = Just d
|
|
|
|
repoWorkTree _ = Nothing
|
|
|
|
|
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
|
|
|
{- Path to a local repository's .git directory. -}
|
2019-12-09 17:49:05 +00:00
|
|
|
localGitDir :: Repo -> 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
|
|
|
localGitDir Repo { location = Local { gitdir = d } } = d
|
2023-04-10 17:38:14 +00:00
|
|
|
localGitDir _ = giveup "unknown localGitDir"
|
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
|
|
|
|
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
|
2021-01-18 18:52:56 +00:00
|
|
|
repoIsUrl Repo { location = UnparseableUrl _ } = True
|
2010-10-28 17:40:10 +00:00
|
|
|
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
|
2012-11-11 04:51:07 +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
|
|
|
|
|
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
|
|
|
repoIsLocal :: Repo -> Bool
|
|
|
|
repoIsLocal Repo { location = Local { } } = True
|
|
|
|
repoIsLocal _ = False
|
2011-03-03 21:33:15 +00:00
|
|
|
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare :: Repo -> Bool
|
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
|
|
|
repoIsLocalBare Repo { location = Local { worktree = Nothing } } = True
|
2011-03-03 18:51:57 +00:00
|
|
|
repoIsLocalBare _ = False
|
|
|
|
|
2012-06-26 21:15:17 +00:00
|
|
|
repoIsLocalUnknown :: Repo -> Bool
|
|
|
|
repoIsLocalUnknown Repo { location = LocalUnknown { } } = True
|
|
|
|
repoIsLocalUnknown _ = False
|
|
|
|
|
2010-10-31 19:38:47 +00:00
|
|
|
assertLocal :: Repo -> a -> a
|
2012-03-16 05:59:07 +00:00
|
|
|
assertLocal repo action
|
2023-04-10 17:38:14 +00:00
|
|
|
| repoIsUrl repo = giveup $ unwords
|
2012-03-16 05:59:07 +00:00
|
|
|
[ "acting on non-local git repo"
|
|
|
|
, repoDescribe repo
|
|
|
|
, "not supported"
|
|
|
|
]
|
|
|
|
| otherwise = action
|
|
|
|
|
2010-10-10 16:35:28 +00:00
|
|
|
{- Path to a repository's gitattributes file. -}
|
2020-10-28 21:25:59 +00:00
|
|
|
attributes :: Repo -> RawFilePath
|
2010-10-28 17:40:10 +00:00
|
|
|
attributes repo
|
2015-12-04 21:57:15 +00:00
|
|
|
| repoIsLocalBare repo = attributesLocal repo
|
2020-10-28 21:25:59 +00:00
|
|
|
| otherwise = repoPath repo P.</> ".gitattributes"
|
2015-12-04 21:57:15 +00:00
|
|
|
|
2020-10-28 21:25:59 +00:00
|
|
|
attributesLocal :: Repo -> RawFilePath
|
|
|
|
attributesLocal repo = localGitDir repo P.</> "info" P.</> "attributes"
|
2010-10-10 06:29:58 +00:00
|
|
|
|
2012-03-14 16:01:56 +00:00
|
|
|
{- Path to a given hook script in a repository, only if the hook exists
|
|
|
|
- and is executable. -}
|
|
|
|
hookPath :: String -> Repo -> IO (Maybe FilePath)
|
|
|
|
hookPath script repo = do
|
2019-12-09 17:49:05 +00:00
|
|
|
let hook = fromRawFilePath (localGitDir repo) </> "hooks" </> script
|
2012-03-16 05:59:07 +00:00
|
|
|
ifM (catchBoolIO $ isexecutable hook)
|
|
|
|
( return $ Just hook , return Nothing )
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2013-08-04 17:07:55 +00:00
|
|
|
#if mingw32_HOST_OS
|
2013-05-10 21:29:59 +00:00
|
|
|
isexecutable f = doesFileExist f
|
|
|
|
#else
|
2022-09-05 17:44:03 +00:00
|
|
|
isexecutable f = isExecutable . fileMode <$> getSymbolicLinkStatus f
|
2013-05-10 21:29:59 +00:00
|
|
|
#endif
|
2015-01-06 18:50:15 +00:00
|
|
|
|
2015-01-06 19:31:24 +00:00
|
|
|
{- Makes the path to a local Repo be relative to the cwd. -}
|
|
|
|
relPath :: Repo -> IO Repo
|
|
|
|
relPath = adjustPath torel
|
|
|
|
where
|
|
|
|
torel p = do
|
|
|
|
p' <- relPathCwdToFile p
|
2020-10-28 19:40:50 +00:00
|
|
|
return $ if B.null p' then "." else p'
|
2015-01-06 19:31:24 +00:00
|
|
|
|
2024-04-06 13:50:58 +00:00
|
|
|
{- Adjusts the path to a local Repo using the provided function. -}
|
2020-10-28 19:40:50 +00:00
|
|
|
adjustPath :: (RawFilePath -> IO RawFilePath) -> Repo -> IO Repo
|
2015-01-06 19:31:24 +00:00
|
|
|
adjustPath f r@(Repo { location = l@(Local { gitdir = d, worktree = w }) }) = do
|
2020-10-28 19:40:50 +00:00
|
|
|
d' <- f d
|
|
|
|
w' <- maybe (pure Nothing) (Just <$$> f) w
|
2015-01-06 19:31:24 +00:00
|
|
|
return $ r
|
|
|
|
{ location = l
|
|
|
|
{ gitdir = d'
|
|
|
|
, worktree = w'
|
|
|
|
}
|
2015-01-06 18:50:15 +00:00
|
|
|
}
|
2019-12-09 17:49:05 +00:00
|
|
|
where
|
2015-01-06 19:31:24 +00:00
|
|
|
adjustPath f r@(Repo { location = LocalUnknown d }) = do
|
2020-10-28 19:40:50 +00:00
|
|
|
d' <- f d
|
2015-01-06 19:31:24 +00:00
|
|
|
return $ r { location = LocalUnknown d' }
|
|
|
|
adjustPath _ r = pure r
|