@@ -1,3 +1,6 @@
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE MultiWayIf #-}
2-- Module : $Header$
5-- Module : $Header$
3-- Copyright : (c) 2015 Adam C. Foltzer
6-- Copyright : (c) 2015 Adam C. Foltzer
@@ -31,7 +34,6 @@
32module Development.GitRev (gitHash, gitBranch, gitDirty, gitCommitCount, gitCommitDate) where
35module Development.GitRev (gitHash, gitBranch, gitDirty, gitCommitCount, gitCommitDate) where
34−import Control.Applicative
35import Control.Exception
37import Control.Exception
36import Control.Monad
38import Control.Monad
37import Data.Maybe
39import Data.Maybe
@@ -42,6 +44,9 @@ import System.Exit
42import System.FilePath
44import System.FilePath
43import System.Process
45import System.Process
45-- | Run git with the given arguments and no stdin, returning the
50-- | Run git with the given arguments and no stdin, returning the
46-- stdout output. If git isn't available or something goes wrong,
51-- stdout output. If git isn't available or something goes wrong,
47-- return the second argument.
52-- return the second argument.
@@ -53,7 +58,7 @@ runGit args def useIdx = do
53 if gitFound
58 if gitFound
55 -- a lot of bookkeeping to record the right dependencies
60 -- a lot of bookkeeping to record the right dependencies
56− pwd <- runIO getCurrentDirectory
61+ pwd <- runIO getGitDirectory
57 let hd = pwd </> ".git" </> "HEAD"
62 let hd = pwd </> ".git" </> "HEAD"
58 index = pwd </> ".git" </> "index"
63 index = pwd </> ".git" </> "index"
59 packedRefs = pwd </> ".git" </> "packed-refs"
64 packedRefs = pwd </> ".git" </> "packed-refs"
@@ -61,8 +66,7 @@ runGit args def useIdx = do
61 when hdExists $ do
66 when hdExists $ do
62 -- the HEAD file either contains the hash of a detached head
67 -- the HEAD file either contains the hash of a detached head
63 -- or a pointer to the file that contains the hash of the head
68 -- or a pointer to the file that contains the hash of the head
64− hdRef <- runIO $ readFile hd
69+ splitAt 5 `fmap` runIO (readFile hd) >>= \case
65− case splitAt 5 hdRef of
66 -- pointer to ref
70 -- pointer to ref
67 ("ref: ", relRef) -> do
71 ("ref: ", relRef) -> do
68 let ref = pwd </> ".git" </> relRef
72 let ref = pwd </> ".git" </> relRef
@@ -85,6 +89,26 @@ runGit args def useIdx = do
85 ExitFailure _ -> return def
89 ExitFailure _ -> return def
86 else return def
90 else return def
92+-- | Determine where our git directory is, in case we're in a
94+getGitDirectory :: IO FilePath
96+ pwd <- getCurrentDirectory
97+ let dotGit = pwd </> ".git"
98+ oops = return dotGit -- it's gonna fail, that's fine
99+ isDir <- doesDirectoryExist dotGit
100+ isFile <- doesFileExist dotGit
101+ if | isDir -> return dotGit
102+ | not isFile -> oops
104+ splitAt 8 `fmap` readFile dotGit >>= \case
105+ ("gitdir: ", relDir) -> do
106+ isRelDir <- doesDirectoryExist relDir
88-- | Type to flag if the git index is used or not in a call to runGit
112-- | Type to flag if the git index is used or not in a call to runGit
89data IndexUsed = IdxUsed -- ^ The git index is used
113data IndexUsed = IdxUsed -- ^ The git index is used
90 | IdxNotUsed -- ^ The git index is /not/ used
114 | IdxNotUsed -- ^ The git index is /not/ used