aboutsummaryrefslogtreecommitdiff
path: root/GitRepo.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-10-09 22:09:10 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-10-09 22:09:10 -0400
commitc920505a52ab3c42b7892b7f7a1c5244c39e916f (patch)
tree2015344665a652f530f2fa1bb3d9ab0588552b34 /GitRepo.hs
parent6b54817f2688cffc8751b3b1552dca0a34744e61 (diff)
add gitRelative function
Diffstat (limited to 'GitRepo.hs')
-rw-r--r--GitRepo.hs21
1 files changed, 19 insertions, 2 deletions
diff --git a/GitRepo.hs b/GitRepo.hs
index fece79785..f1372bf3a 100644
--- a/GitRepo.hs
+++ b/GitRepo.hs
@@ -4,8 +4,25 @@ module GitRepo where
import Directory
import System.Directory
+import System.Path
import Data.String.Utils
+{- Given a relative or absolute filename, calculates the name to use
+ - relative to a git repository directory (which must be absolute).
+ - This is the same form displayed and used by git. -}
+gitRelative :: String -> String -> String
+gitRelative file repo = drop (length absrepo) absfile
+ where
+ -- normalize both repo and file, so that repo
+ -- will be substring of file
+ absrepo = case (absNormPath "/" repo) of
+ Just f -> f ++ "/"
+ Nothing -> error $ "bad repo" ++ repo
+ absfile = case (secureAbsNormPath absrepo file) of
+ Just f -> f
+ Nothing -> error $ file ++ " is not located inside git repository " ++ absrepo
+
+
{- Returns the path to the current repository's .git directory.
- (For a bare repository, that is the root of the repository.) -}
gitDir :: IO String
@@ -20,8 +37,8 @@ gitDir = do
- directory. -}
repoTop :: IO String
repoTop = do
- dir <- getCurrentDirectory
- top <- seekUp dir isRepoTop
+ cwd <- getCurrentDirectory
+ top <- seekUp cwd isRepoTop
case top of
(Just dir) -> return dir
Nothing -> error "Not in a git repository."