2012-06-06 18:26:15 +00:00
|
|
|
{- git FilePath library
|
|
|
|
-
|
|
|
|
- Different git commands use different types of FilePaths to refer to
|
|
|
|
- files in the repository. Some commands use paths relative to the
|
|
|
|
- top of the repository even when run in a subdirectory. Adding some
|
|
|
|
- types helps keep that straight.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <id@joeyh.name>
|
2012-06-06 18:26:15 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-12 22:18:48 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-06-06 18:26:15 +00:00
|
|
|
module Git.FilePath (
|
2016-01-05 21:33:48 +00:00
|
|
|
TopFilePath,
|
2016-07-20 19:22:55 +00:00
|
|
|
BranchFilePath(..),
|
|
|
|
descBranchFilePath,
|
2016-01-05 21:33:48 +00:00
|
|
|
getTopFilePath,
|
2013-10-17 18:51:19 +00:00
|
|
|
fromTopFilePath,
|
2012-06-06 18:26:15 +00:00
|
|
|
toTopFilePath,
|
|
|
|
asTopFilePath,
|
2013-05-12 22:18:48 +00:00
|
|
|
InternalGitPath,
|
|
|
|
toInternalGitPath,
|
2014-02-08 19:31:03 +00:00
|
|
|
fromInternalGitPath,
|
|
|
|
absoluteGitPath
|
2012-06-06 18:26:15 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
|
2014-02-08 19:31:03 +00:00
|
|
|
import qualified System.FilePath.Posix
|
|
|
|
|
2012-06-06 18:26:15 +00:00
|
|
|
{- A FilePath, relative to the top of the git repository. -}
|
|
|
|
newtype TopFilePath = TopFilePath { getTopFilePath :: FilePath }
|
2016-03-11 20:00:14 +00:00
|
|
|
deriving (Show, Eq, Ord)
|
2013-10-17 18:51:19 +00:00
|
|
|
|
2016-07-20 19:22:55 +00:00
|
|
|
{- A file in a branch or other treeish. -}
|
|
|
|
data BranchFilePath = BranchFilePath Ref TopFilePath
|
|
|
|
|
|
|
|
{- Git uses the branch:file form to refer to a BranchFilePath -}
|
|
|
|
descBranchFilePath :: BranchFilePath -> String
|
|
|
|
descBranchFilePath (BranchFilePath b f) = fromRef b ++ ':' : getTopFilePath f
|
|
|
|
|
2016-01-05 21:33:48 +00:00
|
|
|
{- Path to a TopFilePath, within the provided git repo. -}
|
2013-10-17 18:51:19 +00:00
|
|
|
fromTopFilePath :: TopFilePath -> Git.Repo -> FilePath
|
2016-01-05 21:33:48 +00:00
|
|
|
fromTopFilePath p repo = combine (repoPath repo) (getTopFilePath p)
|
2012-06-06 18:26:15 +00:00
|
|
|
|
|
|
|
{- The input FilePath can be absolute, or relative to the CWD. -}
|
|
|
|
toTopFilePath :: FilePath -> Git.Repo -> IO TopFilePath
|
2015-01-06 19:31:24 +00:00
|
|
|
toTopFilePath file repo = TopFilePath <$> relPathDirToFile (repoPath repo) file
|
2012-06-06 18:26:15 +00:00
|
|
|
|
|
|
|
{- The input FilePath must already be relative to the top of the git
|
|
|
|
- repository -}
|
|
|
|
asTopFilePath :: FilePath -> TopFilePath
|
|
|
|
asTopFilePath file = TopFilePath file
|
2013-05-12 22:18:48 +00:00
|
|
|
|
|
|
|
{- Git may use a different representation of a path when storing
|
2013-12-05 03:49:18 +00:00
|
|
|
- it internally.
|
|
|
|
-
|
|
|
|
- On Windows, git uses '/' to separate paths stored in the repository,
|
2014-02-08 18:47:57 +00:00
|
|
|
- despite Windows using '\'.
|
2013-12-05 03:49:18 +00:00
|
|
|
-
|
|
|
|
-}
|
2013-05-12 22:18:48 +00:00
|
|
|
type InternalGitPath = String
|
|
|
|
|
|
|
|
toInternalGitPath :: FilePath -> InternalGitPath
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 22:18:48 +00:00
|
|
|
toInternalGitPath = id
|
|
|
|
#else
|
2014-02-08 18:47:57 +00:00
|
|
|
toInternalGitPath = replace "\\" "/"
|
2013-05-12 22:18:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
fromInternalGitPath :: InternalGitPath -> FilePath
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 22:18:48 +00:00
|
|
|
fromInternalGitPath = id
|
|
|
|
#else
|
|
|
|
fromInternalGitPath = replace "/" "\\"
|
|
|
|
#endif
|
2014-02-08 19:31:03 +00:00
|
|
|
|
|
|
|
{- isAbsolute on Windows does not think "/foo" or "\foo" is absolute,
|
|
|
|
- so try posix paths.
|
|
|
|
-}
|
|
|
|
absoluteGitPath :: FilePath -> Bool
|
|
|
|
absoluteGitPath p = isAbsolute p ||
|
|
|
|
System.FilePath.Posix.isAbsolute (toInternalGitPath p)
|