From a76b1ba3d669e39488ae81695b13147bbc026f5e Mon Sep 17 00:00:00 2001
From: Joey Hess <joeyh@joeyh.name>
Date: Tue, 16 Jun 2020 13:24:00 -0400
Subject: [PATCH] local git remote autoinit improvements

* Improve display of problems auto-initializing or upgrading local git
  remotes.
* When a local git remote cannot be initialized because it has no
  git-annex branch or a .noannex file, avoid displaying a message about it.
---
 Annex/Init.hs | 20 +++++++++++++++++++-
 CHANGELOG     |  5 +++++
 Remote/Git.hs |  4 +++-
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/Annex/Init.hs b/Annex/Init.hs
index b05a7d6871..8c03dda835 100644
--- a/Annex/Init.hs
+++ b/Annex/Init.hs
@@ -10,6 +10,7 @@
 
 module Annex.Init (
 	ensureInitialized,
+	autoInitialize,
 	isInitialized,
 	initialize,
 	initialize',
@@ -61,7 +62,7 @@ import qualified Data.Map as M
 import Control.Concurrent.Async
 
 checkCanInitialize :: Annex a -> Annex a
-checkCanInitialize a = inRepo (noAnnexFileContent . fmap fromRawFilePath . Git.repoWorkTree) >>= \case
+checkCanInitialize a = canInitialize' >>= \case
 	Nothing -> a
 	Just noannexmsg -> do
 		warning "Initialization prevented by .noannex file (remove the file to override)"
@@ -69,6 +70,12 @@ checkCanInitialize a = inRepo (noAnnexFileContent . fmap fromRawFilePath . Git.r
 			warning noannexmsg
 		giveup "Not initialized."
 
+canInitialize :: Annex Bool
+canInitialize = isNothing <$> canInitialize'
+
+canInitialize' :: Annex (Maybe String)
+canInitialize' = inRepo (noAnnexFileContent . fmap fromRawFilePath . Git.repoWorkTree)
+
 genDescription :: Maybe String -> Annex UUIDDesc
 genDescription (Just d) = return $ UUIDDesc $ encodeBS d
 genDescription Nothing = do
@@ -160,6 +167,17 @@ ensureInitialized = getVersion >>= maybe needsinit checkUpgrade
 		, giveup "First run: git-annex init"
 		)
 
+{- Initialize if it can do so automatically.
+ -
+ - Checks repository version and handles upgrades too.
+ -}
+autoInitialize :: Annex ()
+autoInitialize = getVersion >>= maybe needsinit checkUpgrade
+  where
+	needsinit = whenM (canInitialize <&&> Annex.Branch.hasSibling) $ do
+			initialize Nothing Nothing
+			autoEnableSpecialRemotes
+
 {- Checks if a repository is initialized. Does not check version for ugrade. -}
 isInitialized :: Annex Bool
 isInitialized = maybe Annex.Branch.hasSibling (const $ return True) =<< getVersion
diff --git a/CHANGELOG b/CHANGELOG
index 7c7893ef4e..2fc3f50f33 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -35,6 +35,11 @@ git-annex (8.20200523) UNRELEASED; urgency=medium
   * import: Avoid using some strange names for temporary keys.
   * S3: The REDUCED_REDUNDANCY storage class is no longer cheaper so
     stop documenting it, and stop offering it as a choice in the assistant.
+  * Improve display of problems auto-initializing or upgrading local git
+    remotes.
+  * When a local git remote cannot be initialized because it has no
+    git-annex branch or a .noannex file, avoid displaying a message about it.
+
 
  -- Joey Hess <id@joeyh.name>  Tue, 26 May 2020 10:20:52 -0400
 
diff --git a/Remote/Git.hs b/Remote/Git.hs
index 18726dce89..7c51265296 100644
--- a/Remote/Git.hs
+++ b/Remote/Git.hs
@@ -337,7 +337,9 @@ tryGitConfigRead autoinit r hasuuid
 	readlocalannexconfig = do
 		let check = do
 			Annex.BranchState.disableUpdate
-			catchNonAsync ensureInitialized (warning . show)
+			catchNonAsync autoInitialize $ \e ->
+				warning $ "remote " ++ Git.repoDescribe r ++
+					" :"  ++ show e
 			Annex.getState Annex.repo
 		s <- Annex.new r
 		Annex.eval s $ check `finally` stopCoProcesses