2013-04-22 19:36:34 +00:00
|
|
|
{- git-annex environment
|
2013-01-06 17:34:08 +00:00
|
|
|
-
|
2023-04-04 19:21:50 +00:00
|
|
|
- Copyright 2012-2023 Joey Hess <id@joeyh.name>
|
2013-01-06 17:34:08 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2013-01-06 17:34:08 +00:00
|
|
|
-}
|
|
|
|
|
2019-11-27 20:54:11 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2023-04-04 19:12:52 +00:00
|
|
|
module Annex.Environment (
|
|
|
|
checkEnvironment,
|
|
|
|
checkEnvironmentIO,
|
|
|
|
ensureCommit,
|
|
|
|
) where
|
2013-01-06 17:34:08 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-01-06 17:34:08 +00:00
|
|
|
import Utility.UserInfo
|
|
|
|
import qualified Git.Config
|
2023-04-04 19:15:02 +00:00
|
|
|
import Config
|
2017-12-31 20:08:31 +00:00
|
|
|
import Utility.Env.Set
|
2013-08-04 17:54:09 +00:00
|
|
|
|
2023-04-04 19:12:52 +00:00
|
|
|
import Control.Exception
|
|
|
|
|
2013-01-06 17:34:08 +00:00
|
|
|
{- Checks that the system's environment allows git to function.
|
|
|
|
- Git requires a GECOS username, or suitable git configuration, or
|
2023-04-04 19:21:50 +00:00
|
|
|
- environment variables. When none of those are set, this will set the
|
2013-07-05 16:24:28 +00:00
|
|
|
- environment variables.
|
|
|
|
-
|
|
|
|
- Git also requires the system have a hostname containing a dot.
|
|
|
|
- Otherwise, it tries various methods to find a FQDN, and will fail if it
|
|
|
|
- does not. To avoid replicating that code here, which would break if its
|
|
|
|
- methods change, this function does not check the hostname is valid.
|
2023-04-04 19:21:50 +00:00
|
|
|
- Instead, git-annex init calls ensureCommit, which makes sure that git
|
|
|
|
- gets set up to allow committing.
|
2013-07-05 16:24:28 +00:00
|
|
|
-}
|
2013-01-06 17:34:08 +00:00
|
|
|
checkEnvironment :: Annex ()
|
|
|
|
checkEnvironment = do
|
|
|
|
gitusername <- fromRepo $ Git.Config.getMaybe "user.name"
|
2013-09-25 07:09:06 +00:00
|
|
|
when (isNothing gitusername || gitusername == Just "") $
|
2023-04-04 19:12:52 +00:00
|
|
|
unlessM userConfigOnly $
|
|
|
|
liftIO checkEnvironmentIO
|
2013-04-22 19:36:34 +00:00
|
|
|
|
|
|
|
checkEnvironmentIO :: IO ()
|
2014-10-31 20:14:12 +00:00
|
|
|
checkEnvironmentIO = whenM (isNothing <$> myUserGecos) $ do
|
2016-06-08 19:04:15 +00:00
|
|
|
username <- either (const "unknown") id <$> myUserName
|
2014-10-16 00:33:52 +00:00
|
|
|
ensureEnv "GIT_AUTHOR_NAME" username
|
|
|
|
ensureEnv "GIT_COMMITTER_NAME" username
|
2013-05-03 18:08:26 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
-- existing environment is not overwritten
|
2014-10-16 00:33:52 +00:00
|
|
|
ensureEnv var val = setEnv var val False
|
2013-07-05 16:24:28 +00:00
|
|
|
|
|
|
|
{- Runs an action that commits to the repository, and if it fails,
|
2023-04-04 19:15:02 +00:00
|
|
|
- sets user.email and user.name to a dummy value and tries the action again.
|
|
|
|
-
|
|
|
|
- Note that user.email and user.name are left set afterwards, so this only
|
|
|
|
- needs to be used once to make sure that future commits will succeed.
|
|
|
|
-}
|
2013-07-05 16:24:28 +00:00
|
|
|
ensureCommit :: Annex a -> Annex a
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
ensureCommit a = either retry return =<< tryNonAsync a
|
2013-07-05 16:24:28 +00:00
|
|
|
where
|
2023-04-04 19:12:52 +00:00
|
|
|
retry e = ifM userConfigOnly
|
|
|
|
( liftIO (throwIO e)
|
|
|
|
, do
|
|
|
|
name <- liftIO $ either (const "unknown") id <$> myUserName
|
2023-04-04 19:15:02 +00:00
|
|
|
setConfig "user.name" name
|
|
|
|
setConfig "user.email" name
|
2023-04-04 19:12:52 +00:00
|
|
|
a
|
|
|
|
)
|
|
|
|
|
|
|
|
userConfigOnly :: Annex Bool
|
|
|
|
userConfigOnly = do
|
|
|
|
v <- fromRepo $ Git.Config.getMaybe "user.useconfigonly"
|
|
|
|
return (fromMaybe False (Git.Config.isTrueFalse' =<< v))
|