From c089f395b0c7d6416a3d4f2bf3211404acfd5b0e Mon Sep 17 00:00:00 2001
From: Joey Hess <joeyh@joeyh.name>
Date: Thu, 27 Feb 2020 00:52:37 -0400
Subject: [PATCH] Bugfix: Don't ignore --debug when it is followed by -c

---
 Annex.hs                                      | 34 ++++++++++++-------
 CHANGELOG                                     |  6 ++++
 ...debug_is_not_in_effect_if_precedes_-c.mdwn |  2 +-
 ..._0f66414e629e31c2de868f8b7eeb7af5._comment | 24 +++++++++++++
 4 files changed, 52 insertions(+), 14 deletions(-)
 create mode 100644 doc/bugs/--debug_is_not_in_effect_if_precedes_-c/comment_1_0f66414e629e31c2de868f8b7eeb7af5._comment

diff --git a/Annex.hs b/Annex.hs
index eff212203b..efdbe864d7 100644
--- a/Annex.hs
+++ b/Annex.hs
@@ -1,6 +1,6 @@
 {- git-annex monad
  -
- - Copyright 2010-2018 Joey Hess <id@joeyh.name>
+ - Copyright 2010-2020 Joey Hess <id@joeyh.name>
  -
  - Licensed under the GNU AGPL version 3 or higher.
  -}
@@ -104,6 +104,7 @@ data AnnexState = AnnexState
 	{ repo :: Git.Repo
 	, repoadjustment :: (Git.Repo -> IO Git.Repo)
 	, gitconfig :: GitConfig
+	, gitconfigadjustment :: (GitConfig -> GitConfig)
 	, gitremotes :: Maybe [Git.Repo]
 	, backend :: Maybe (BackendA Annex)
 	, remotes :: [Types.Remote.RemoteA Annex]
@@ -161,6 +162,7 @@ newState c r = do
 		{ repo = r
 		, repoadjustment = return
 		, gitconfig = c
+		, gitconfigadjustment = id
 		, gitremotes = Nothing
 		, backend = Nothing
 		, remotes = []
@@ -314,19 +316,13 @@ calcRepo a = do
 getGitConfig :: Annex GitConfig
 getGitConfig = getState gitconfig
 
-{- Modifies a GitConfig setting. -}
+{- Modifies a GitConfig setting. The modification persists across
+ - reloads of the repo's config. -}
 changeGitConfig :: (GitConfig -> GitConfig) -> Annex ()
-changeGitConfig a = changeState $ \s -> s { gitconfig = a (gitconfig s) }
-
-{- Changing the git Repo data also involves re-extracting its GitConfig. -}
-changeGitRepo :: Git.Repo -> Annex ()
-changeGitRepo r = do
-	adjuster <- getState repoadjustment
-	r' <- liftIO $ adjuster r
-	changeState $ \s -> s
-		{ repo = r'
-		, gitconfig = extractGitConfig FromGitConfig r'
-		}
+changeGitConfig f = changeState $ \s -> s
+	{ gitconfigadjustment = gitconfigadjustment s . f
+	, gitconfig = f (gitconfig s)
+	}
 
 {- Adds an adjustment to the Repo data. Adjustments persist across reloads
  - of the repo's config. -}
@@ -335,6 +331,18 @@ adjustGitRepo a = do
 	changeState $ \s -> s { repoadjustment = \r -> repoadjustment s r >>= a }
 	changeGitRepo =<< gitRepo
 
+{- Changing the git Repo data also involves re-extracting its GitConfig. -}
+changeGitRepo :: Git.Repo -> Annex ()
+changeGitRepo r = do
+	repoadjuster <- getState repoadjustment
+	gitconfigadjuster <- getState gitconfigadjustment
+	r' <- liftIO $ repoadjuster r
+	changeState $ \s -> s
+		{ repo = r'
+		, gitconfig = gitconfigadjuster $
+			extractGitConfig FromGitConfig r'
+		}
+
 {- Gets the RemoteGitConfig from a remote, given the Git.Repo for that
  - remote. -}
 getRemoteGitConfig :: Git.Repo -> Annex RemoteGitConfig
diff --git a/CHANGELOG b/CHANGELOG
index f02438894a..21971c1d79 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,9 @@
+git-annex (8.20200227) UNRELEASED; urgency=medium
+
+  * Bugfix: Don't ignore --debug when it is followed by -c.
+
+ -- Joey Hess <id@joeyh.name>  Thu, 27 Feb 2020 00:44:11 -0400
+
 git-annex (8.20200226) upstream; urgency=medium
 
   * New v8 repository version.
diff --git a/doc/bugs/--debug_is_not_in_effect_if_precedes_-c.mdwn b/doc/bugs/--debug_is_not_in_effect_if_precedes_-c.mdwn
index ef551b7630..0ccf3903c2 100644
--- a/doc/bugs/--debug_is_not_in_effect_if_precedes_-c.mdwn
+++ b/doc/bugs/--debug_is_not_in_effect_if_precedes_-c.mdwn
@@ -64,4 +64,4 @@ git-annex version: 7.20190819+git2-g908476a9b-1~ndall+1
 [[!meta author=yoh]]
 [[!tag projects/datalad]]
 
-
+> [[fixed|done]] --[[Joey]]
diff --git a/doc/bugs/--debug_is_not_in_effect_if_precedes_-c/comment_1_0f66414e629e31c2de868f8b7eeb7af5._comment b/doc/bugs/--debug_is_not_in_effect_if_precedes_-c/comment_1_0f66414e629e31c2de868f8b7eeb7af5._comment
new file mode 100644
index 0000000000..badeb87ff9
--- /dev/null
+++ b/doc/bugs/--debug_is_not_in_effect_if_precedes_-c/comment_1_0f66414e629e31c2de868f8b7eeb7af5._comment
@@ -0,0 +1,24 @@
+[[!comment format=mdwn
+ username="joey"
+ subject="""comment 1"""
+ date="2020-02-27T04:20:58Z"
+ content="""
+-c uses adjustGitRepo which calls changeGitRepo, which
+re-extracts the GitConfig. --debug uses changeGitConfig which 
+sets annexDebug in the GitConfig, which does not survive the changeGitRepo.
+
+There might be a broader problem here, as changeGitRepo is also
+called by setConfig in many parts of the code. I think it narrowly
+escapes being a problem, because by the time a command is started,
+it's already enabled debug output, and so the GitConfig being reloaded
+doesn't disable debugging.
+
+Other calls to changeGitConfig could also be a problem, if followed by
+an adjustGitRepo which loses those changes. There are only a few others,
+look probably ok, but this would be an easy gotcha to hit later.
+
+So changeGitConfig needs to make a config change that persists across
+changeGitRepo.
+
+Done.
+"""]]