From 34fc0d358e53e4c615b1fe6fb80374a5302ccf1c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 5 Aug 2012 15:45:47 -0400 Subject: fix crashes when run in a git repo that has been initted but has no master branch yet --- Git/Branch.hs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'Git/Branch.hs') diff --git a/Git/Branch.hs b/Git/Branch.hs index 4d239d8fc..098aa1a1a 100644 --- a/Git/Branch.hs +++ b/Git/Branch.hs @@ -5,6 +5,8 @@ - Licensed under the GNU GPL version 3 or higher. -} +{-# LANGUAGE BangPatterns #-} + module Git.Branch where import Common @@ -12,13 +14,22 @@ import Git import Git.Sha import Git.Command -{- The currently checked out branch. -} +{- The currently checked out branch. + - + - In a just initialized git repo before the first commit, + - symbolic-ref will show the master branch, even though that + - branch is not created yet. So, this also looks at show-ref HEAD + - to double-check. + -} current :: Repo -> IO (Maybe Git.Ref) -current r = parse <$> pipeRead [Param "symbolic-ref", Param "HEAD"] r - where - parse v - | null v = Nothing - | otherwise = Just $ Git.Ref $ firstLine v +current r = do + branch <- firstLine <$> pipeRead [Param "symbolic-ref", Param "HEAD"] r + if null branch + then return Nothing + else ifM (null <$> pipeRead [Param "show-ref", Param branch] r) + ( return Nothing + , return $ Just $ Git.Ref branch + ) {- Checks if the second branch has any commits not present on the first - branch. -} -- cgit v1.2.3