Skip to content

Commit 0f51816

Browse files
authored
Merge pull request #8755 from BasLaa/no-git-default
Return empty default when git fails
2 parents 0bedc4a + d85bf80 commit 0f51816

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

cabal-install/src/Distribution/Client/Init/Interactive/Command.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,10 @@ licensePrompt flags = getLicense flags $ do
355355
else fmap prettyShow knownLicenses
356356

357357
authorPrompt :: Interactive m => InitFlags -> m String
358-
authorPrompt flags = getAuthor flags $ do
359-
name <- guessAuthorName
360-
promptStr "Author name" (DefaultPrompt name)
358+
authorPrompt flags = getAuthor flags $ guessAuthorName >>= promptOrDefault "Author name"
361359

362360
emailPrompt :: Interactive m => InitFlags -> m String
363-
emailPrompt flags = getEmail flags $ do
364-
email' <- guessAuthorEmail
365-
promptStr "Maintainer email" (DefaultPrompt email')
361+
emailPrompt flags = getEmail flags $ guessAuthorEmail >>= promptOrDefault "Maintainer email"
366362

367363
homepagePrompt :: Interactive m => InitFlags -> m String
368364
homepagePrompt flags = getHomepage flags $
@@ -467,3 +463,6 @@ srcDirsPrompt flags = getSrcDirs flags $ do
467463
True
468464

469465
return [dir]
466+
467+
promptOrDefault :: Interactive m => String -> Maybe String -> m String
468+
promptOrDefault s = maybe (promptStr s MandatoryPrompt) (promptStr s . DefaultPrompt)

cabal-install/src/Distribution/Client/Init/NonInteractive/Command.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,16 @@ licenseHeuristics :: Interactive m => InitFlags -> m SpecLicense
274274
licenseHeuristics flags = getLicense flags $ guessLicense flags
275275

276276
-- | The author's name. Prompt, or try to guess from an existing
277-
-- darcs repo.
277+
-- git repo.
278278
authorHeuristics :: Interactive m => InitFlags -> m String
279-
authorHeuristics flags = getAuthor flags guessAuthorEmail
279+
authorHeuristics flags = guessAuthorName >>=
280+
maybe (getAuthor flags $ return "Unknown") (getAuthor flags . return)
280281

281282
-- | The author's email. Prompt, or try to guess from an existing
282-
-- darcs repo.
283+
-- git repo.
283284
emailHeuristics :: Interactive m => InitFlags -> m String
284-
emailHeuristics flags = getEmail flags guessAuthorName
285+
emailHeuristics flags = guessAuthorEmail >>=
286+
maybe (getEmail flags $ return "Unknown") (getEmail flags . return)
285287

286288
-- | Prompt for a homepage URL for the package.
287289
homepageHeuristics :: Interactive m => InitFlags -> m String

cabal-install/src/Distribution/Client/Init/NonInteractive/Heuristics.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,23 @@ guessSourceDirectories flags = do
151151
True -> ["src"]
152152

153153
-- | Guess author and email using git configuration options.
154-
guessAuthorName :: Interactive m => m String
154+
guessAuthorName :: Interactive m => m (Maybe String)
155155
guessAuthorName = guessGitInfo "user.name"
156156

157-
guessAuthorEmail :: Interactive m => m String
157+
guessAuthorEmail :: Interactive m => m (Maybe String)
158158
guessAuthorEmail = guessGitInfo "user.email"
159159

160-
guessGitInfo :: Interactive m => String -> m String
160+
guessGitInfo :: Interactive m => String -> m (Maybe String)
161161
guessGitInfo target = do
162-
info <- readProcessWithExitCode "git" ["config", "--local", target] ""
163-
if null $ snd' info
164-
then trim . snd' <$> readProcessWithExitCode "git" ["config", "--global", target] ""
165-
else return . trim $ snd' info
162+
localInfo <- readProcessWithExitCode "git" ["config", "--local", target] ""
163+
if null $ snd' localInfo
164+
then do
165+
globalInfo <- readProcessWithExitCode "git" ["config", "--global", target] ""
166+
case fst' globalInfo of
167+
ExitSuccess -> return $ Just (trim $ snd' globalInfo)
168+
_ -> return Nothing
169+
else return $ Just (trim $ snd' localInfo)
166170

167171
where
172+
fst' (x, _, _) = x
168173
snd' (_, x, _) = x

cabal-install/tests/UnitTests/Distribution/Client/Init/FileCreators.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ tests _v _initFlags comp pkgIx srcDb =
4444
}
4545
inputs =
4646
-- createProject stuff
47-
[ "True"
47+
[ "Foobar"
48+
49+
, "True"
4850
, "[\"quxTest/Main.hs\"]"
4951
-- writeProject stuff
5052
-- writeLicense

cabal-install/tests/UnitTests/Distribution/Client/Init/NonInteractive.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
7373
, dependencies = Flag []
7474
}
7575
inputs = NEL.fromList
76-
[ "True"
76+
["Foobar"
77+
78+
, "True"
7779
, "[\"quxTest/Main.hs\"]"
7880
]
7981

@@ -149,8 +151,11 @@ driverFunctionTest pkgIx srcDb comp = testGroup "createProject"
149151
, dependencies = Flag []
150152
}
151153
inputs = NEL.fromList
154+
155+
[ "Foobar"
156+
152157
-- extra sources
153-
[ "[\"CHANGELOG.md\"]"
158+
, "[\"CHANGELOG.md\"]"
154159
-- lib other modules
155160
, "False"
156161
-- exe other modules

0 commit comments

Comments
 (0)