2012-06-06 14:26:15 -04: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 12:50:09 -04:00
|
|
|
- Copyright 2012-2013 Joey Hess <id@joeyh.name>
|
2012-06-06 14:26:15 -04:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-12 17:18:48 -05:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-06-06 14:26:15 -04:00
|
|
|
module Git.FilePath (
|
2016-01-05 17:33:48 -04:00
|
|
|
TopFilePath,
|
2016-07-20 15:22:55 -04:00
|
|
|
BranchFilePath(..),
|
|
|
|
descBranchFilePath,
|
2016-01-05 17:33:48 -04:00
|
|
|
getTopFilePath,
|
2013-10-17 14:51:19 -04:00
|
|
|
fromTopFilePath,
|
2012-06-06 14:26:15 -04:00
|
|
|
toTopFilePath,
|
|
|
|
asTopFilePath,
|
2013-05-12 17:18:48 -05:00
|
|
|
InternalGitPath,
|
|
|
|
toInternalGitPath,
|
2014-02-08 15:31:03 -04:00
|
|
|
fromInternalGitPath,
|
|
|
|
absoluteGitPath
|
2012-06-06 14:26:15 -04:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Git
|
|
|
|
|
2014-02-08 15:31:03 -04:00
|
|
|
import qualified System.FilePath.Posix
|
|
|
|
|
2012-06-06 14:26:15 -04:00
|
|
|
{- A FilePath, relative to the top of the git repository. -}
|
|
|
|
newtype TopFilePath = TopFilePath { getTopFilePath :: FilePath }
|
2016-03-11 16:00:14 -04:00
|
|
|
deriving (Show, Eq, Ord)
|
2013-10-17 14:51:19 -04:00
|
|
|
|
2016-07-20 15:22:55 -04: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 17:33:48 -04:00
|
|
|
{- Path to a TopFilePath, within the provided git repo. -}
|
2013-10-17 14:51:19 -04:00
|
|
|
fromTopFilePath :: TopFilePath -> Git.Repo -> FilePath
|
2016-01-05 17:33:48 -04:00
|
|
|
fromTopFilePath p repo = combine (repoPath repo) (getTopFilePath p)
|
2012-06-06 14:26:15 -04:00
|
|
|
|
|
|
|
{- The input FilePath can be absolute, or relative to the CWD. -}
|
|
|
|
toTopFilePath :: FilePath -> Git.Repo -> IO TopFilePath
|
2015-01-06 15:31:24 -04:00
|
|
|
toTopFilePath file repo = TopFilePath <$> relPathDirToFile (repoPath repo) file
|
2012-06-06 14:26:15 -04: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 17:18:48 -05:00
|
|
|
|
|
|
|
{- Git may use a different representation of a path when storing
|
2013-12-04 23:49:18 -04:00
|
|
|
- it internally.
|
|
|
|
-
|
|
|
|
- On Windows, git uses '/' to separate paths stored in the repository,
|
2014-02-08 14:47:57 -04:00
|
|
|
- despite Windows using '\'.
|
2013-12-04 23:49:18 -04:00
|
|
|
-
|
|
|
|
-}
|
2013-05-12 17:18:48 -05:00
|
|
|
type InternalGitPath = String
|
|
|
|
|
|
|
|
toInternalGitPath :: FilePath -> InternalGitPath
|
2013-08-02 12:27:32 -04:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 17:18:48 -05:00
|
|
|
toInternalGitPath = id
|
|
|
|
#else
|
2014-02-08 14:47:57 -04:00
|
|
|
toInternalGitPath = replace "\\" "/"
|
2013-05-12 17:18:48 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
fromInternalGitPath :: InternalGitPath -> FilePath
|
2013-08-02 12:27:32 -04:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 17:18:48 -05:00
|
|
|
fromInternalGitPath = id
|
|
|
|
#else
|
|
|
|
fromInternalGitPath = replace "/" "\\"
|
|
|
|
#endif
|
2014-02-08 15:31:03 -04: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)
|