Skip to contentSkip to conversation

environment: move diff_auto_refresh_index into repo_config_values

PATCH In review Ebele Lynda Okolo via GitGitGadget 2 participants 2 messages Nov 2 – Nov 3

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 environment: move diff_auto_refresh_index into repo_config_values

From: Lynndabel <[email protected]>

The diff_auto_refresh_index variable is currently a global, which makes it difficult to use Git as a library since global state can't be isolated between different repository instances.

Move it into repo_config_values so that each repository can have its own setting. This follows the pattern used for other recently migrated config options like trust_executable_bit.

Add a getter function repo_diff_auto_refresh_index() that returns the value from the repo's config, falling back to 1 (the default) if the repo isn't initialized yet.

Update the config handler to write to the repo config struct, remove the global variable declaration, and update the caller in builtin/diff.c to use the new getter function.

Add tests to verify the configuration can be set to both true and false without errors.

---
[Outreachy] diff: move diff_auto_refresh_index to repo_config_values

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2289%2FLynndabel%2Foutreachy-contribution-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2289/Lynndabel/outreachy-contribution-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2289

 builtin/diff.c         |  3 ++-
 diff.c                 |  4 ++--
 diff.h                 |  2 --
 environment.c          | 10 ++++++++++
 environment.h          |  5 +++++
 t/t4000-diff-format.sh | 10 ++++++++++
 6 files changed, 29 insertions(+), 5 deletions(-)
builtin/diff.c +2 -1
index 18b1083e98..ed83959921 100644
@@ -10,6 +10,7 @@
 #include "builtin.h"
 #include "config.h"
 #include "ewah/ewok.h"
+#include "environment.h"
 #include "lockfile.h"
 #include "color.h"
 #include "commit.h"
@@ -522,7 +523,7 @@ int cmd_diff(int argc,
 	 * Otherwise, we are doing the usual "git" diff; set up any
 	 * further defaults that apply to regular diffs.
 	 */
-	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
+	rev.diffopt.skip_stat_unmatch = !!repo_diff_auto_refresh_index(the_repository);
 
 	/*
 	 * Default to intent-to-add entries invisible in the
diff.c +2 -2
index b4ac17f8ef..5f34ce91a9 100644
@@ -65,7 +65,6 @@ static unsigned int diff_interhunk_context_default;
 static char *diff_word_regex_cfg;
 static struct external_diff external_diff_cfg;
 static char *diff_order_file_cfg;
-int diff_auto_refresh_index = 1;
 static int diff_mnemonic_prefix;
 static int diff_no_prefix;
 static char *diff_src_prefix;
@@ -400,7 +399,8 @@ int git_diff_ui_config(const char *var, const char *value,
 		return 0;
 	}
 	if (!strcmp(var, "diff.autorefreshindex")) {
-		diff_auto_refresh_index = git_config_bool(var, value);
+		struct repo_config_values *cfg = repo_config_values(the_repository);
+		cfg->diff_auto_refresh_index = git_config_bool(var, value);
 		return 0;
 	}
 	if (!strcmp(var, "diff.mnemonicprefix")) {
diff.h +0 -2
index bb5cddaf34..466a1230a7 100644
@@ -767,6 +767,4 @@ void print_stat_summary(FILE *fp, int files,
 			int insertions, int deletions);
 void setup_diff_pager(struct diff_options *);
 
-extern int diff_auto_refresh_index;
-
 #endif /* DIFF_H */
environment.c +10 -0
index c83cf44839..2947e558d4 100644
@@ -152,6 +152,13 @@ int repo_has_symlinks(struct repository *repo)
 		: platform_has_symlinks();
 }
 
+int repo_diff_auto_refresh_index(struct repository *repo)
+{
+	return repo->initialized
+		? repo_config_values(repo)->diff_auto_refresh_index
+		: 1;
+}
+
 const char *repo_excludes_file(struct repository *repo)
 {
 	struct repo_config_values *cfg = repo_config_values(repo);
@@ -778,6 +785,9 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->has_symlinks = platform_has_symlinks();
 	cfg->use_nanosec = 0;
 
+	/* section "diff" config values */
+	cfg->diff_auto_refresh_index = 1;
+
 	/* section "sparse" config values */
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 
environment.h +5 -0
index 49c86e8879..b6304d2e5c 100644
@@ -150,6 +150,9 @@ struct repo_config_values {
 	int has_symlinks;
 	int use_nanosec;
 
+	/* section "diff" config values */
+	int diff_auto_refresh_index;
+
 	/* section "sparse" config values */
 	int sparse_expect_files_outside_of_patterns;
 
@@ -203,6 +206,8 @@ int repo_trust_executable_bit(struct repository *repo);
 
 int repo_has_symlinks(struct repository *repo);
 
+int repo_diff_auto_refresh_index(struct repository *repo);
+
 const char *repo_excludes_file(struct repository *repo);
 
 void repo_config_values_init(struct repo_config_values *cfg);
t/t4000-diff-format.sh +10 -0
index 74dec7d34a..1fa98d1456 100755
@@ -122,4 +122,14 @@ do
 	'
 done
 
+test_expect_success 'diff.autorefreshindex can be set to false' '
+	test_config diff.autorefreshindex false &&
+	git diff-files
+'
+
+test_expect_success 'diff.autorefreshindex can be set to true' '
+	test_config diff.autorefreshindex true &&
+	git diff-files
+'
+
 test_done

base-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7

Signature · 1 line
gitgitgadget
#1 lore raw

"Ebele Lynda Okolo via GitGitGadget" <[email protected]>

Quoted 6 lines · writes:

From: Lynndabel <[email protected]>

The diff_auto_refresh_index variable is currently a global, which makes it difficult to use Git as a library since global state can't be isolated between different repository instances.

We do not want to put it in repo_config_value, because access to the structure will barf when given anything other than the_repository, hence such a move does not help the "library" scene very much to begin with.

It is only set in diff.c when the ui related configuration variable is read from the git_config() callback. The callback is *not* repository specific but is a synthesized computation result after reading system, global, and repository configuration files, which is another reason why it does not look like a good idea to tie this setting to repo_config_values.

Thanks.