Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit 6a1a639

Browse files
committed
Fix a number of issues
These were uncovered when researching commercialhaskell/stack#105. What I discovered: * There was an extra layer of `.git` directories in computed paths * The HEAD file should be added dependently regardless of whether it's a detached head * Add a missing check in relRef for newline * Switch newline handling to check both CR and LR for Windows support
1 parent 450a7ab commit 6a1a639

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Development/GitRev.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@ runGit args def useIdx = do
6767
then do
6868
-- a lot of bookkeeping to record the right dependencies
6969
pwd <- runIO getDotGit
70-
let hd = pwd </> ".git" </> "HEAD"
71-
index = pwd </> ".git" </> "index"
72-
packedRefs = pwd </> ".git" </> "packed-refs"
70+
let hd = pwd </> "HEAD"
71+
index = pwd </> "index"
72+
packedRefs = pwd </> "packed-refs"
7373
hdExists <- runIO $ doesFileExist hd
7474
when hdExists $ do
75+
addDependentFile hd
7576
-- the HEAD file either contains the hash of a detached head
7677
-- or a pointer to the file that contains the hash of the head
7778
splitAt 5 `fmap` runIO (readFile hd) >>= \case
7879
-- pointer to ref
7980
("ref: ", relRef) -> do
80-
let ref = pwd </> ".git" </> relRef
81+
let ref = pwd </> tillNewLine relRef
8182
refExists <- runIO $ doesFileExist ref
8283
when refExists $ addDependentFile ref
8384
-- detached head
84-
_hash -> addDependentFile hd
85+
_hash -> return ()
8586
-- add the index if it exists to set the dirty flag
8687
indexExists <- runIO $ doesFileExist index
8788
when (indexExists && useIdx == IdxUsed) $ addDependentFile index
@@ -93,10 +94,13 @@ runGit args def useIdx = do
9394
runIO $ do
9495
(code, out, _err) <- readProcessWithExitCode "git" args "" `catch` oops
9596
case code of
96-
ExitSuccess -> return (takeWhile (/= '\n') out)
97+
ExitSuccess -> return (tillNewLine out)
9798
ExitFailure _ -> return def
9899
else return def
99100

101+
tillNewLine :: String -> String
102+
tillNewLine = takeWhile (\c -> c /= '\n' && c /= '\r')
103+
100104
-- | Determine where our @.git@ directory is, in case we're in a
101105
-- submodule.
102106
getDotGit :: IO FilePath
@@ -124,7 +128,7 @@ getGitRoot = do
124128
(code, out, _) <-
125129
readProcessWithExitCode "git" ["rev-parse", "--show-toplevel"] ""
126130
case code of
127-
ExitSuccess -> return $ takeWhile (/= '\n') out
131+
ExitSuccess -> return $ tillNewLine out
128132
ExitFailure _ -> return pwd -- later steps will fail, that's fine
129133

130134
-- | Type to flag if the git index is used or not in a call to runGit

0 commit comments

Comments
 (0)