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