Skip to contentSkip to conversation

environment: move excludes_file into repo_config_values

PATCH Superseded Tian Yuchen 5 participants 114 messages Jun 26 – Jul 14

lore
Share & tools
lore
#0 lore raw

v2 cover environment: move excludes_file into repo_config_values

This series continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

The series is structured in two commits:

- Abstract the XDG fallback lazy-loading logic out of dir.c into a proper
getter.

- Move the variables into the struct and introduce 'repo_config_values_clear()'.

Note on Submodules: A temporary shield 'if (repo != the_repository)' is included in both the getter and the clear function. This prevents uninitialized submodules from triggering the BUG() assertion. (Inspiration: [1])

Changes since V1:

- fix a typo in the second commit.

- initialize excludes_file to NULL in repo_config_values_init().

- call FREE_AND_NULL() to free attributes_file as well in repo_config_values_clear().

Thanks!

[1] https://lore.kernel.org/git/[email protected]/

Tian Yuchen (2):
dir: encapsulate excludes_file lazy-load
environment: move excludes_file into repo_config_values

 dir.c         |  4 ++--
 environment.c | 32 +++++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 44 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 dir: encapsulate excludes_file lazy-load

The global variable 'excludes_file' is used to track the path to the global ignore file, 'core.excludesfile'. If this variable is NULL, setup_standard_excludes() in dir.c forcefully evaluates and assigns the XDG default path to it.

Introduce repo_excludes_file() as a getter to encapsulate this lazy-loading logic. This prepares the variable to be safely moved into 'struct repo_config_values' in the subsequent commit.

---
 dir.c         | 4 ++--
 environment.c | 7 +++++++
 environment.h | 5 +++++
 3 files changed, 14 insertions(+), 2 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +7 -0
index ba2c60103f..8efcaeafa6 100644
@@ -134,6 +134,13 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!excludes_file)
+		excludes_file = xdg_config_home("ignore");
+	return excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
environment.h +5 -0
index 6f18286955..52d531e4ea 100644
@@ -133,6 +133,11 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+/*
+ * TODO: This still relies on the global state.
+ */
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] dir: encapsulate excludes_file lazy-load

Quoted 5 lines · Tian Yuchen
environment.c +0 -0
index ba2c60103f..8efcaeafa6 100644

environment.c int is_bare_repository(void) · line 142

Quoted 9 lines
environment.c +6 -0
@@ -134,6 +134,13 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!excludes_file)
+		excludes_file = xdg_config_home("ignore");
+	return excludes_file;
+}

This function has a 'repo' parameter, which is not used in the function at all. This causes build failure when trying to build this commit using DEVELOPER=1:

environment.c: In function ‘repo_excludes_file’:
environment.c:137:51: error: unused parameter ‘repo’ [-Werror=unused-parameter]
137 | const char *repo_excludes_file(struct repository *repo)
| ~~~~~~~~~~~~~~~~~~~^~~~
cc1: all warnings being treated as errors
make: *** [Makefile:2922: environment.o] Error 1

Please make sure that all commits can be built with 'make DEVELOPER=1'.

#3 lore raw

Re: [PATCH v2 1/2] dir: encapsulate excludes_file lazy-load

Quoted 27 lines · SZEDER Gábor
Quoted 5 lines · Tian Yuchen
environment.c +0 -0
index ba2c60103f..8efcaeafa6 100644

environment.c int is_bare_repository(void) · line 142

Quoted 9 lines
environment.c +6 -0
@@ -134,6 +134,13 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!excludes_file)
+		excludes_file = xdg_config_home("ignore");
+	return excludes_file;
+}

This function has a 'repo' parameter, which is not used in the function at all. This causes build failure when trying to build this commit using DEVELOPER=1:

environment.c: In function ‘repo_excludes_file’:
environment.c:137:51: error: unused parameter ‘repo’ [-Werror=unused-parameter]
137 | const char *repo_excludes_file(struct repository *repo)
| ~~~~~~~~~~~~~~~~~~~^~~~
cc1: all warnings being treated as errors
make: *** [Makefile:2922: environment.o] Error 1

Quoted 2 lines

Please make sure that all commits can be built with 'make DEVELOPER=1'.

Good point. In this case, we can start with UNUSED in step 1/2 and then drop the UNUSED in the second step. I wonder how harder to read it would become if these two patches are squashed together...

Thanks.

#4 lore raw

Re: [PATCH v2 1/2] dir: encapsulate excludes_file lazy-load

Quoted 29 lines · Junio C Hamano
Quoted 27 lines · SZEDER Gábor
Quoted 5 lines · Tian Yuchen
environment.c +0 -0
index ba2c60103f..8efcaeafa6 100644

environment.c int is_bare_repository(void) · line 142

Quoted 9 lines
environment.c +6 -0
@@ -134,6 +134,13 @@ int is_bare_repository(void)
  	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
  }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!excludes_file)
+		excludes_file = xdg_config_home("ignore");
+	return excludes_file;
+}

This function has a 'repo' parameter, which is not used in the function at all. This causes build failure when trying to build this commit using DEVELOPER=1:

environment.c: In function ‘repo_excludes_file’:
environment.c:137:51: error: unused parameter ‘repo’ [-Werror=unused-parameter]
137 | const char *repo_excludes_file(struct repository *repo)
| ~~~~~~~~~~~~~~~~~~~^~~~
cc1: all warnings being treated as errors
make: *** [Makefile:2922: environment.o] Error 1

Quoted 6 lines
Quoted 2 lines

Please make sure that all commits can be built with 'make DEVELOPER=1'.

Good point. In this case, we can start with UNUSED in step 1/2 and then drop the UNUSED in the second step. I wonder how harder to read it would become if these two patches are squashed together...

That makes sense. I think these two commits can be squadshed together... I hope so.

Quoted 2 lines

Thanks.

Regards, yuchen

#5 lore raw

PATCH v2 2/2 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

Continue the libification effort by moving the 'excludes_file' global variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string (char *), it requires proper memory management. Introduce repo_config_values_clear() to safely free the heap memory when repository instance is destroyed.

Note:

- 'if (repo != the_repository)' fallback logic is temporarily added
in both the getter and the clear function. This prevents calling repo_config_values() on uninitialized submodules, which triggers BUG().

- 'attribute_file' is another string variable that was migrated earlier. Its FREE_AND_NULL() call is also added to repo_config_values_clear().

---
 environment.c | 31 +++++++++++++++++++++++++------
 environment.h | 14 ++++++++++----
 repository.c  |  1 +
 3 files changed, 36 insertions(+), 10 deletions(-)
environment.c +25 -6
index 8efcaeafa6..b8dfa3e213 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -136,9 +135,13 @@ int is_bare_repository(void)
 
 const char *repo_excludes_file(struct repository *repo)
 {
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
-	return excludes_file;
+	if (!repo || !repo->initialized || repo != the_repository)
+		return NULL;
+
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
 }
 
 int have_git_dir(void)
@@ -468,8 +471,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -722,6 +725,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -733,3 +737,18 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+	if (!cfg)
+		return;
+
+	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
+}
environment.h +10 -4
index 52d531e4ea..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,13 +134,19 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
-/*
- * TODO: This still relies on the global state.
- */
 const char *repo_excludes_file(struct repository *repo);
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
@@ -213,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#6 lore raw
Quoted 9 lines · Tian Yuchen

Continue the libification effort by moving the 'excludes_file' global variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string (char *), it requires proper memory management. Introduce repo_config_values_clear() to safely free the heap memory when repository instance is destroyed.

Note:

Quoted 7 lines

- 'if (repo != the_repository)' fallback logic is temporarily added
in both the getter and the clear function. This prevents calling repo_config_values() on uninitialized submodules, which triggers BUG().

- 'attribute_file' is another string variable that was migrated earlier. Its FREE_AND_NULL() call is also added to repo_config_values_clear().

OK. I think the placement of the new member in repo_config_values in this round, moving to the spot next to existing attributes_file, makes more sense than the previous round, too.

Looking good. Shall we mark it as ready for 'next' now?

#7 lore raw
In reply to Tian Yuchen
Quoted 7 lines · Tian Yuchen

Continue the libification effort by moving the 'excludes_file' global variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string (char *), it requires proper memory management. Introduce repo_config_values_clear() to safely free the heap memory when repository instance is destroyed.

Quoted 5 lines

Note:

- 'if (repo != the_repository)' fallback logic is temporarily added
in both the getter and the clear function. This prevents calling repo_config_values() on uninitialized submodules, which triggers BUG().

Would it be possible for the function to be called on the_repository before it gets initialized?

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 743

Quoted 3 lines
environment.c +3 -0
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;

What I am wondering is if this check

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (repo != the_repository)
+		return;

wants to be more like

	if (!repo->initialized)
		return;

or even

	if (!repo->initialized) {
		BUG("clearing uninitialised repo config");
		return;
	}

Or perhaps not doing anything special there.

For that matter,

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 750

Quoted 4 lines
environment.c +4 -0
+
+	cfg = repo_config_values(repo);
+	if (!cfg)
+		return;

Wouldn't it be a bug to see NULL returned from the above function? Why is it healthy to pretend as if nothing bad happened?

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 754

Quoted 3 lines
environment.c +3 -0
+	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
+}

What do we want to happen when somebody does want to access (not _clear(), but repo_config_values() itself) repo_config_values() in today's code? Don't we want to catch such a code as buggy? Isn't it the reason why repository.c:repo_config_values() check these conditions and calls BUG() in the first place? And if that is the case, I find that a caller that "works around" by pretending nothing bad happened and not calling repo_config_values(), like the above code does, highly questionable, as it smells like sweeping problems that you designed other parts of the code to detect with BUG() under the rug.

In the longer run, we would want to have separate settings, which used to be global variables but now are stored in per repository config, available and usable in the context of each repository that they are configured within. If a caller wants to clear per repo config for a repository instance by calling this function, this function is in no place to tweak the intention of the caller by short-circuiting the request and pretending it did what it was asked to do. In other words, the rest of the code not quite prepared to deal with these global variables that turned into per repository configuration values is *not* a problem this function should address. Let it be noticed by repo_config_values() function to catch offending callers for now, and once the codebase becomes ready to use one repo_config_values per repository, this function does not have to change.

#8 lore raw
Quoted 67 lines · Junio C Hamano
Quoted 7 lines · Tian Yuchen

Continue the libification effort by moving the 'excludes_file' global variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string (char *), it requires proper memory management. Introduce repo_config_values_clear() to safely free the heap memory when repository instance is destroyed.

Quoted 5 lines

Note:

- 'if (repo != the_repository)' fallback logic is temporarily added
in both the getter and the clear function. This prevents calling repo_config_values() on uninitialized submodules, which triggers BUG().

Would it be possible for the function to be called on the_repository before it gets initialized?

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 743

Quoted 3 lines
environment.c +3 -0
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;

What I am wondering is if this check

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (repo != the_repository)
+		return;

wants to be more like

	if (!repo->initialized)
		return;

or even

	if (!repo->initialized) {
		BUG("clearing uninitialised repo config");
		return;
	}

Or perhaps not doing anything special there.

For that matter,

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 750

Quoted 4 lines
environment.c +4 -0
+
+	cfg = repo_config_values(repo);
+	if (!cfg)
+		return;

Wouldn't it be a bug to see NULL returned from the above function? Why is it healthy to pretend as if nothing bad happened?

environment.c void repo_config_values_init(struct repo_config_values *cfg) · line 754

Quoted 3 lines
environment.c +3 -0
+	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
+}

What do we want to happen when somebody does want to access (not _clear(), but repo_config_values() itself) repo_config_values() in today's code? Don't we want to catch such a code as buggy? Isn't it the reason why repository.c:repo_config_values() check these conditions and calls BUG() in the first place? And if that is the case, I find that a caller that "works around" by pretending nothing bad happened and not calling repo_config_values(), like the above code does, highly questionable, as it smells like sweeping problems that you designed other parts of the code to detect with BUG() under the rug.

Quoted 14 lines

In the longer run, we would want to have separate settings, which used to be global variables but now are stored in per repository config, available and usable in the context of each repository that they are configured within. If a caller wants to clear per repo config for a repository instance by calling this function, this function is in no place to tweak the intention of the caller by short-circuiting the request and pretending it did what it was asked to do. In other words, the rest of the code not quite prepared to deal with these global variables that turned into per repository configuration values is *not* a problem this function should address. Let it be noticed by repo_config_values() function to catch offending callers for now, and once the codebase becomes ready to use one repo_config_values per repository, this function does not have to change.

Yes, indeed. I initially thought these were two solutions leading to the same goal, but now that you mention it, I think your point is more valid.

First, 'repo_config_values_clear()' shouldn't know whether "only 'the_repository' is currently supported" (even if this logic is necessary, this function is clearly not the optimal place), so we shouldn't use 'repo != the_repository' to determine this.

Second, what's wrong with '!cfg' is that what it's supposed to avoid deviates from what it tries to avoid. 'repo_config_values(repo)' shouldn't validly return NULL at all. I admit this code is nonsensical.

Using 'repo->initialized' to determine this is consistent with our previous approach. I'll try it. Thanks for the review!

Regards, yuchen

#9 lore raw
In reply to Tian Yuchen
Quoted 4 lines · Tian Yuchen

This series continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

This appears here:

https://lore.kernel.org/git/[email protected]/

and as you can see, there is no linkage back to the previous round.

The lack of In-Reply-To and References headers unfortunately delays my automation in marking topics with newer iterations available to be reviewed when I come back to the keyboard, which happens overnight.

#10 lore raw

On 6/26/26 23:42, Junio C Hamano wrote:

Sorry, there was a problem with my Thunderbird client... so the my emails didn't send without me noticing...

Quoted 9 lines
Quoted 4 lines · Tian Yuchen

This series continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

This appears here:

Quoted 7 lines
   https://lore.kernel.org/git/[email protected]/

and as you can see, there is no linkage back to the previous round.

The lack of In-Reply-To and References headers unfortunately delays my automation in marking topics with newer iterations available to be reviewed when I come back to the keyboard, which happens overnight.

Regarding this, I forgot to bring --in-reply-to. I'll remember next time.

Thanks, yuchen

#11 lore raw

v4 cover environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

This patch continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

This patch mainly does three things:

- Abstract the XDG fallback lazy-loading logic out of dir.c into a proper getter.

- Move the variables into the struct repo_config_values.

- Introduce the memory destructor 'repo_config_values_clear()'.

Changes since V2:

- Squash together the previous two commits into one.

- The 'repo->initialized' check is used in both the getter and destructor. This eliminates redundant checks and follows the fail-fast principle. This is consistent with the previous global variable removal patches [1][2][3].

Note: Resending as v3 purely to fix a malformed In-Reply-To header in v2 that broke the email thread. No actual code changes since v2...

Thanks!

[1] https://lore.kernel.org/git/[email protected]/T/#m856253610936d052a798259bfc06d598561e53c4
[2] https://lore.kernel.org/git/[email protected]/
[3] https://lore.kernel.org/git/[email protected]/T/#m8843984a6175a1a4c7e00877085c77b0c72f5803

Tian Yuchen (1):
environment: move excludes_file into repo_config_values

 dir.c         |  4 ++--
 environment.c | 30 +++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 42 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#12 lore raw

PATCH v4 1/1 environment: move excludes_file into repo_config_values

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, setup_standard_excludes() in dir.c forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. Introduce repo_config_values_clear() and wire it up in repo_clear() to safely free this memory when a repository instance is destroyed. Also clean up the heap-allocated 'attributes_file' in this new destructor while we are at it.

Note: 'if (!repo->initialized)' is added in both the getter and the destructor. This ensures we safely return or bypass cleaning up uninitialized repositories without hardcoding a dependency on 'the_repository'.

---
 dir.c         |  4 ++--
 environment.c | 30 +++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 42 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +27 -3
index ba2c60103f..2519c60918 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,17 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo || !repo->initialized)
+		return NULL;
+
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +471,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +725,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -726,3 +737,16 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	if (repo->initialized)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
+}
environment.h +12 -1
index 6f18286955..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,8 +134,19 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
@@ -208,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#13 lore raw

Hi all,

Apologies again for the duplicate...

environment.c int is_bare_repository(void) · line 145

Quoted 10 lines · Tian Yuchen
environment.c +10 -0
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo || !repo->initialized)
+		return NULL;
+
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}

One more thing:

I deliberately didn't write a comment for the getter because it will probably be merged with comments from the previous several patches in some form in the near future... I'm not sure if it would be more appropriate to write a separate patch to add the corresponding comments then.

Regards, yuchen

#14 lore raw
Quoted 10 lines · Tian Yuchen

Hi all,

Apologies again for the duplicate...

environment.c int is_bare_repository(void) · line 139

Quoted 4 lines · Tian Yuchen
environment.c +4 -0
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo || !repo->initialized)
+		return NULL;

I might already have said this, but I am not sure why want to be as loose as this code. It is not limited to this line, but I think we saw plenty of other "We know we must get an already initialized thing here, and the subsequent operation we perform on that thing will cause us to die() later, so let's return silently and early to avoid hitting die()" attempts to sweep problems under the rug.

Wouldn't we rather want to try to be more strict and say

	if (!repo || !repo->initialized)
		BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

Quoted 8 lines

environment.c int is_bare_repository(void) · line 145

Quoted 5 lines
environment.c +5 -0
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}

One more thing:

Quoted 5 lines

I deliberately didn't write a comment for the getter because it will probably be merged with comments from the previous several patches in some form in the near future... I'm not sure if it would be more appropriate to write a separate patch to add the corresponding comments then.

That's very sensible.

#15 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 10 lines · Tian Yuchen

Hi all,

Apologies again for the duplicate...

environment.c int is_bare_repository(void) · line 139

Quoted 4 lines · Tian Yuchen
environment.c +4 -0
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo || !repo->initialized)
+		return NULL;

I might already have said this, but I am not sure why want to be as loose as this code. It is not limited to this line, but I think we saw plenty of other "We know we must get an already initialized thing here, and the subsequent operation we perform on that thing will cause us to die() later, so let's return silently and early to avoid hitting die()" attempts to sweep problems under the rug.

Quoted 8 lines

Wouldn't we rather want to try to be more strict and say

	if (!repo || !repo->initialized)
		BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

That makes sense, but from my point of view...

'repo_config_values()' already has a check for 'repo->initialized'. If we're absolutely certain that the 'repo' is initialized, wouldn't it be better to simply remove all the checks inside the getter and leave the judgment to 'repo_config_values()'?

This also aligns to some extent with the previous flag getters: since an uninitialized 'repo' will trigger a BUG() in 'repo_config_values()', but "reading and writing these flags when the 'repo' is uninitialized" is sometimes a valid operation that's why we choose to intercept 'repo->initialized' _before_ 'repo_config_values()' and fallback to the hard-coded values.

This gives the impression that _'repo_config_values()' is the function responsible for checking_, and the way flags are handled is an exception to this approach, which I think is more consistent and self-explanatory.

What do you think?

Quoted 9 lines
Quoted 9 lines

environment.c int is_bare_repository(void) · line 145

Quoted 6 lines
environment.c +5 -0
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}

One more thing:

Quoted 8 lines
Quoted 5 lines

I deliberately didn't write a comment for the getter because it will probably be merged with comments from the previous several patches in some form in the near future... I'm not sure if it would be more appropriate to write a separate patch to add the corresponding comments then.

That's very sensible.

Thanks.

regards, yuchen

#16 lore raw
Quoted 4 lines · Tian Yuchen

Let it be noticed by repo_config_values() function to
catch offending callers for now, and once the codebase becomes ready to use one repo_config_values per repository, this function does not have to change.

And

Quoted 7 lines

Wouldn't we rather want to try to be more strict and say

	if (!repo || !repo->initialized)
		BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

In my opinion, these two suggestions are not entirely consistent, and I think we need to determine the most appropriate approach.

Regards, yuchen

#17 lore raw
In reply to Tian Yuchen
Quoted 8 lines · Tian Yuchen
Quoted 7 lines

Wouldn't we rather want to try to be more strict and say

	if (!repo || !repo->initialized)
		BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

Quoted 6 lines

That makes sense, but from my point of view...

'repo_config_values()' already has a check for 'repo->initialized'. If we're absolutely certain that the 'repo' is initialized, wouldn't it be better to simply remove all the checks inside the getter and leave the judgment to 'repo_config_values()'?

Yes, that was what I was getting at ;-).

#18 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Tian Yuchen
Quoted 7 lines

Wouldn't we rather want to try to be more strict and say

	if (!repo || !repo->initialized)
		BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

Quoted 8 lines
Quoted 6 lines

That makes sense, but from my point of view...

'repo_config_values()' already has a check for 'repo->initialized'. If we're absolutely certain that the 'repo' is initialized, wouldn't it be better to simply remove all the checks inside the getter and leave the judgment to 'repo_config_values()'?

Yes, that was what I was getting at ;-).

A lot of CI tests are failing, but that just goes to show that the "bugs" are being properly identified, doesn’t it?

It means there are a lot of "invalid" calls in the tests (if the way we define a 'valid' call, i.e. repo must be initialized, is correct)... It seems that code like 'if (repo != the_repository) return' or something similar is inevitably going to end up somewhere, even though, as you said, it’s "sweeping problems under the rug."

I’m not sure how to proceed from here either..

Regards, yuchen

#19 lore raw
Quoted 24 lines · Tian Yuchen
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Tian Yuchen
Quoted 7 lines

Wouldn't we rather want to try to be more strict and say

    if (!repo || !repo->initialized)
            BUG("repo must be an initialied repository");

here? Aren't all the callers of this function supposed to be dealing with an already initialized repository?

Quoted 8 lines
Quoted 6 lines

That makes sense, but from my point of view...

'repo_config_values()' already has a check for 'repo->initialized'. If we're absolutely certain that the 'repo' is initialized, wouldn't it be better to simply remove all the checks inside the getter and leave the judgment to 'repo_config_values()'?

Yes, that was what I was getting at ;-).

A lot of CI tests are failing, but that just goes to show that the "bugs" are being properly identified, doesn’t it?

Quoted 7 lines

It means there are a lot of "invalid" calls in the tests (if the way we define a 'valid' call, i.e. repo must be initialized, is correct)... It seems that code like 'if (repo != the_repository) return' or something similar is inevitably going to end up somewhere, even though, as you said, it’s "sweeping problems under the rug."

I’m not sure how to proceed from here either..

I agree that the best end state would be to have no if (!repo || !repo->initialized) check, but we shouldn't have to get there right away. I think it's fine to proceed in several steps:

1) if (!repo || !repo->initialized) return NULL; allows us to remove the global variable and use getters which will help us in the next step.

2) if (!repo || !repo->initialized) return BUG("repo must be an initialized repository"); now we want to find and fix callers (including tests) that haven't properly initialized things.

3) No if (!repo || !repo->initialized) check, as we are sure that all the callers that didn't properly initialized things have been found and fixed.

So I think 1) is fine for now as long as we properly explain in the commit messages and in code comments (maybe using NEEDSWORK comments) that we know there is more work to do on this in the future.

#20 lore raw
Quoted 12 lines · Christian Couder

I agree that the best end state would be to have no if (!repo || !repo->initialized) check, but we shouldn't have to get there right away. I think it's fine to proceed in several steps:

1) if (!repo || !repo->initialized) return NULL; allows us to remove the global variable and use getters which will help us in the next step.

2) if (!repo || !repo->initialized) return BUG("repo must be an initialized repository"); now we want to find and fix callers (including tests) that haven't properly initialized things.

Quoted 7 lines

3) No if (!repo || !repo->initialized) check, as we are sure that all the callers that didn't properly initialized things have been found and fixed.

So I think 1) is fine for now as long as we properly explain in the commit messages and in code comments (maybe using NEEDSWORK comments) that we know there is more work to do on this in the future.

I am OK with the progressive improvements, but if "wean us away from global variables" topic stops at step 1 I would have to say that is a failed experiment. Not doing (2) means you made the system bug-to-bug compatible from the old world where these things weren't in repo-config but were still globals, which is code churn for nothing good to show in the end result. We need to get to (2) before declaring victory.

But of course, we need to start somewhere. (1) with in-code comments sprinkled to such places that say that we'd need to revisit would be a good place to start.

Thanks.

#21 lore raw

Hi Christian and Junio,

Quoted 29 lines · Junio C Hamano
Quoted 12 lines · Christian Couder

I agree that the best end state would be to have no if (!repo || !repo->initialized) check, but we shouldn't have to get there right away. I think it's fine to proceed in several steps:

1) if (!repo || !repo->initialized) return NULL; allows us to remove the global variable and use getters which will help us in the next step.

2) if (!repo || !repo->initialized) return BUG("repo must be an initialized repository"); now we want to find and fix callers (including tests) that haven't properly initialized things.

Quoted 7 lines

3) No if (!repo || !repo->initialized) check, as we are sure that all the callers that didn't properly initialized things have been found and fixed.

So I think 1) is fine for now as long as we properly explain in the commit messages and in code comments (maybe using NEEDSWORK comments) that we know there is more work to do on this in the future.

I am OK with the progressive improvements, but if "wean us away from global variables" topic stops at step 1 I would have to say that is a failed experiment. Not doing (2) means you made the system bug-to-bug compatible from the old world where these things weren't in repo-config but were still globals, which is code churn for nothing good to show in the end result. We need to get to (2) before declaring victory.

Quoted 5 lines

But of course, we need to start somewhere. (1) with in-code comments sprinkled to such places that say that we'd need to revisit would be a good place to start.

Thanks.

Thank you both for paving a clear way forward.

For the upcoming V5 patch, I will implement:

1. Revert to the shields ('return NULL' for the getter, and bypassing 'repo != repository' for the _clear()).
2. Add 'NEEDSWORK' comments above them documenting that these are temporary changes.
3. Update the commit message to reflect this.

Once this initial migration safely lands, the next goal will be to investigate those failing CI tests.

Will send out V5 shortly.

Regards, yuchen

#22 lore raw
Quoted 9 lines · Tian Yuchen

Thank you both for paving a clear way forward.

For the upcoming V5 patch, I will implement:

1. Revert to the shields ('return NULL' for the getter, and bypassing 'repo != repository' for the _clear()).
2. Add 'NEEDSWORK' comments above them documenting that these are temporary changes.
3. Update the commit message to reflect this.

Quoted 6 lines

Once this initial migration safely lands, the next goal will be to investigate those failing CI tests.

Will send out V5 shortly.

Regards, yuchen

Sorry, I forgot to test the repo_excludes_file() and repo_config_values_clear() with/without checks separately earlier. As for repo_excludes_files(), I found that the check wasn't necessary, so I removed it in v6.

In other words, we're now only filtering out calls that aren't passing 'the_repository' to _clear(), which is still a task to be addressed in the future. I think the current situation should be fairly satisfactory.

Hope this helps.

Regards, yuchen

#23 lore raw

v5 cover environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

This patch continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

This patch mainly does three things:

- Abstract the XDG fallback lazy-loading logic out of dir.c into a proper getter.

- Move the variables into the struct repo_config_values.

- Introduce the memory destructor 'repo_config_values_clear()'.

Changes since V4:

Defensive checks are retained in both the getter (returning NULL if uninitialized) and the destructor (bypassing non-the_repository instances) to maintain bug-to-bug compatibility. These are marked with 'NEEDSWORK' comments.

Future work will track down and fix the offending callers to eventually replace these shields with stricter BUG() assertions. [1]

THANKS!

[1] https://lore.kernel.org/git/[email protected]/T/#mca0730b6735298316ec00a53d4719935d18837a6

Tian Yuchen (1):
environment: move excludes_file into repo_config_values

 dir.c         |  4 ++--
 environment.c | 43 ++++++++++++++++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 55 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#24 lore raw

PATCH v5 1/1 environment: move excludes_file into repo_config_values

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. Introduce repo_config_values_clear() and wire it up in 'repo_clear()' to safely free this memory when a repository instance is destroyed. Also clean up the heap-allocated 'attributes_file' in this new destructor while we are at it.

Note on transition:

Defensive checks are temporarily retained in both the getter (returning NULL if uninitialized) and the destructor (bypassing non-the_repository instances) to maintain bug-to-bug compatibility. These are marked with NEEDSWORK comments. Future work will track down and fix the offending callers to eventually replace these shields with stricter BUG() assertions.

---
 dir.c         |  4 ++--
 environment.c | 43 ++++++++++++++++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 55 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +40 -3

70 lines not on this page · raw message

environment.h +12 -1
index 6f18286955..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,8 +134,19 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
@@ -208,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#25 lore raw

v6 cover environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

This patch continues the libification effort by migrating the global string variable 'excludes_file' into 'struct repo_config_values'. Since this is a dynamically allocated variable, the migration requires proper heap memory management.

This patch mainly does three things:

- Abstract the XDG fallback lazy-loading logic out of dir.c into a proper getter.

- Move the variables into the struct repo_config_values.

- Introduce the memory destructor 'repo_config_values_clear()'.

Changes since V5:

Drop the defensive check in 'repo_excludes_file()'.

All known test errors that occur *without* defensive checks are intercepted by the 'repo != the_repository' check when checks *are there*, rather than by the '!repo->initialized' check. In other words, the getter actually doesn't need any checks added, because all of its calls are safe. We just need to keep the check and the 'NEEDSWORK' comment *on the destructor* until 'repo_config_values()' eventually supports submodules, at which point we can remove them.

THANKS!

Tian Yuchen (1):
environment: move excludes_file into repo_config_values

 dir.c         |  4 ++--
 environment.c | 33 ++++++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 45 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#26 lore raw

PATCH v6 1/1 environment: move excludes_file into repo_config_values

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. Introduce repo_config_values_clear() and wire it up in 'repo_clear()' to safely free this memory when a repository instance is destroyed. Also clean up the heap-allocated 'attributes_file' in this new destructor while we are at it.

Note on transition:

Submodules are currently not supported by repo_config_values(). Since repo_clear() cleans up all repository instances (including submodules), we must bypass them in repo_config_values_clear() to prevent hitting the BUG() in repository.c:59. In the future when submodules are supported, this check should be removed.

---
 dir.c         |  4 ++--
 environment.c | 33 ++++++++++++++++++++++++++++++---
 environment.h | 13 ++++++++++++-
 repository.c  |  1 +
 4 files changed, 45 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +30 -3
index ba2c60103f..f1f859dd08 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,14 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -726,3 +734,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(). Since repo_clear() cleans up all
+	 * repository instances (including submodules), we must bypass
+	 * them here to prevent hitting the BUG() in repository.c:59.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
+}
environment.h +12 -1
index 6f18286955..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,8 +134,19 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
@@ -208,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#27 lore raw

v7 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Changes since v6:

Only the first two commits in this patch overlap with v6. The reason the subsequent commits were not released separately is that Christian suggested placing the introduction of 'repo_config_values_clear()' as a standalone commit at the very beginning.

In other words, the v6 structure has been discarded, and this series is being released as almost a new patch.

Thanks!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

 apply.c        | 20 +++++++-----
 branch.c       |  2 +-
 builtin/push.c |  8 ++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 87 +++++++++++++++++++++++++++++++++++---------------
 environment.h  | 75 +++++++++++++++++++++++++++----------------
 object-file.c  |  2 +-
 pager.c        | 17 ++++++----
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 12 files changed, 145 insertions(+), 80 deletions(-)
Signature · 1 line
2.43.0
#28 lore raw

PATCH v7 1/9 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
 environment.c | 19 +++++++++++++++++++
 environment.h |  9 +++++++++
 repository.c  |  1 +
 3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#29 lore raw

PATCH v7 2/9 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()'
in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced repo_config_values_clear() function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 15 ++++++++++++---
 environment.h |  4 +++-
 3 files changed, 17 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +12 -3
index 13677484de..5950592d63 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,14 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +752,5 @@ void repo_config_values_clear(struct repository *repo)
 	cfg = repo_config_values(repo);
 
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index c4a6a45704..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#30 lore raw

PATCH v7 3/9 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..07d264cba0 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor && repo_config_values(the_repository)->editor_program)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 5950592d63..0a01f4761a 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -435,8 +434,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -723,6 +722,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -753,4 +753,5 @@ void repo_config_values_clear(struct repository *repo)
 
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 2e8352de7f..1ec19149cb 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#31 lore raw

Re: [PATCH v7 3/9] environment: move editor_program into repo_config_values

Quoted 9 lines · Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

Quoted 7 lines

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes managing multiple repositories concurrently must not overwrite each other's program configurations.

Nit:

"As the codebase moves toward becoming long-running processes, managing multiple repositories concurrently must not overwrite each other's program configurations"

Quoted 4 lines

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

Why no getter function here? in patch 2/9 repo_excludes_file() is introduced and also access the field via repo_config_values().

Super nit: on a previous commit you used backquotes for repo_config_values_clear() but now you're using single quotes.

Quoted 16 lines
---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +0 -0
index fd174e6a03..07d264cba0 100644

editor.c const char *git_editor(void) · line 33

Quoted 7 lines
editor.c +2 -2
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor && repo_config_values(the_repository)->editor_program)
+		editor = repo_config_values(the_repository)->editor_program;

Same as the previous patch, we can store repo_config_values() and avoid re-calling the function.
Also, do we need the right-side condition?

Quoted 53 lines
editor.c +0 -0
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 5950592d63..0a01f4761a 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -435,8 +434,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -723,6 +722,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -753,4 +753,5 @@ void repo_config_values_clear(struct repository *repo)
 
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -0
index 2e8352de7f..1ec19149cb 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;

environment.h const char *get_commit_output_encoding(void); · line 224

Quoted 7 lines
environment.h +0 -1
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*

Regards,
Pablo

#33 lore raw

PATCH v7 4/9 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 17 ++++++++++-------
 3 files changed, 13 insertions(+), 7 deletions(-)
environment.c +2 -0
index 0a01f4761a..a1204fdcb2 100644
@@ -723,6 +723,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -754,4 +755,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 1ec19149cb..22f6697c52 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +10 -7
index 35b210e048..450ad053b6 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,12 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
+	struct repository *r = data;
+
 	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
 	return 0;
 }
 
@@ -91,10 +94,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -303,6 +306,6 @@ int check_pager_config(struct repository *r, const char *cmd)
 	read_early_config(r, pager_command_config, &data);
 
 	if (data.value)
-		pager_program = data.value;
+		repo_config_values(r)->pager_program = data.value;
 	return data.want;
 }
Signature · 1 line
2.43.0
#34 lore raw

Re: [PATCH v7 4/9] environment: move pager_program into repo_config_values

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 311

Quoted 5 lines · Tian Yuchen
pager.c +1 -1
 	if (data.value)
-		pager_program = data.value;
+		repo_config_values(r)->pager_program = data.value;
 	return data.want;
 }

May not be a new problem, but does the old value or pager_program leak here, if callers call this function more than once (or pager_program gets assigned elsewhere)?

#35 lore raw

PATCH v7 5/9 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +4 -2
index a1204fdcb2..3782bf68aa 100644
@@ -462,8 +462,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -724,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -756,4 +757,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -0
index 22f6697c52..d55b1ba073 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#36 lore raw

PATCH v7 6/9 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 20 ++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 18 insertions(+), 12 deletions(-)
apply.c +12 -8
index 249248d4f2..66db9b7678 100644
@@ -47,11 +47,13 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &repo_config_values(repo)->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &repo_config_values(repo)->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +128,12 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+	if (repo_config_values(repo)->apply_default_whitespace &&
+	    parse_whitespace_option(state, repo_config_values(repo)->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (repo_config_values(repo)->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, repo_config_values(repo)->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +196,7 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 3782bf68aa..8744790219 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -725,6 +723,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -758,4 +758,6 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index d55b1ba073..9aecd64152 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#37 lore raw

PATCH v7 7/9 environment: move push_default into repo_config_values

In reply to Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c |  8 ++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 30 insertions(+), 22 deletions(-)
builtin/push.c +4 -4
index 6021b71d66..6dc3224b60 100644
@@ -88,7 +88,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (repo_config_values(the_repository)->push_default == PUSH_DEFAULT_UPSTREAM &&
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +160,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -232,7 +232,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	const char *dst;
 	int same_remote;
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +252,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 8744790219..09de2fee87 100644
@@ -59,7 +59,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -619,21 +618,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -725,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index 9aecd64152..72859b5d76 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +1 -1
index 00723b385e..d48c01d375 100644
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 
Signature · 1 line
2.43.0
#38 lore raw

PATCH v7 8/9 environment: move autorebase into repo_config_values

In reply to Tian Yuchen

The global variable 'autorebase' dictates whether a newly created branch should be configured to automatically rebase by default. Move it into 'struct repo_config_values' to continue the libification effort.

The 'enum rebase_setup_type' definition is moved higher up in 'environment.h' so that it is visible to the repository-specific structure. The default state AUTOREBASE_NEVER is now correctly initialized in 'repo_config_values_init()'.

Configuration parsing in 'git_default_branch_config()' is updated to write directly to the repository's configuration instance.

---
 branch.c      |  2 +-
 environment.c | 10 +++++-----
 environment.h | 16 ++++++++--------
 3 files changed, 14 insertions(+), 14 deletions(-)
branch.c +1 -1
index 243db7d0fc..e1c1f8c89d 100644
@@ -61,7 +61,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
 static int should_setup_rebase(const char *origin)
 {
-	switch (autorebase) {
+	switch (repo_config_values(the_repository)->autorebase) {
 	case AUTOREBASE_NEVER:
 		return 0;
 	case AUTOREBASE_LOCAL:
environment.c +5 -5
index 09de2fee87..7701aa3bc0 100644
@@ -58,7 +58,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
-enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -600,13 +599,13 @@ static int git_default_branch_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "never"))
-			autorebase = AUTOREBASE_NEVER;
+			cfg->autorebase = AUTOREBASE_NEVER;
 		else if (!strcmp(value, "local"))
-			autorebase = AUTOREBASE_LOCAL;
+			cfg->autorebase = AUTOREBASE_LOCAL;
 		else if (!strcmp(value, "remote"))
-			autorebase = AUTOREBASE_REMOTE;
+			cfg->autorebase = AUTOREBASE_REMOTE;
 		else if (!strcmp(value, "always"))
-			autorebase = AUTOREBASE_ALWAYS;
+			cfg->autorebase = AUTOREBASE_ALWAYS;
 		else
 			return error(_("malformed value for %s"), var);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
+	cfg->autorebase = AUTOREBASE_NEVER;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +8 -8
index 72859b5d76..464ff73136 100644
@@ -102,6 +102,13 @@ enum push_default_type {
 	PUSH_DEFAULT_UNSPECIFIED
 };
 
+enum rebase_setup_type {
+	AUTOREBASE_NEVER = 0,
+	AUTOREBASE_LOCAL,
+	AUTOREBASE_REMOTE,
+	AUTOREBASE_ALWAYS
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -112,6 +119,7 @@ struct repo_config_values {
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
+	enum rebase_setup_type autorebase;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -205,14 +213,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum rebase_setup_type {
-	AUTOREBASE_NEVER = 0,
-	AUTOREBASE_LOCAL,
-	AUTOREBASE_REMOTE,
-	AUTOREBASE_ALWAYS
-};
-extern enum rebase_setup_type autorebase;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
Signature · 1 line
2.43.0
#39 lore raw

PATCH v7 9/9 environment: move object_creation_mode into repo_config_values

In reply to Tian Yuchen

The global variable 'object_creation_mode' controls how Git creates object files, specifically determining whether to use hardlinks or renames when moving temporary files into the object database. Move it into 'struct repo_config_values' to continue the libification effort.

Move the 'enum object_creation_mode' definition higher up in 'environment.h' to ensure it is visible to the structure. Initialize the per-repository value to its default macro value OBJECT_CREATION_MODE inside 'repo_config_values_init()'.

Update configuration parsing in 'git_default_core_config()' to write directly to the repository-specific configuration structure.

---
 environment.c |  6 +++---
 environment.h | 12 ++++++------
 object-file.c |  2 +-
 3 files changed, 10 insertions(+), 10 deletions(-)
environment.c +3 -3
index 7701aa3bc0..e50beda918 100644
@@ -61,7 +61,6 @@ char *check_roundtrip_encoding;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
-enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
 unsigned long pack_size_limit_cfg;
 
@@ -511,9 +510,9 @@ int git_default_core_config(const char *var, const char *value,
 		if (!value)
 			return config_error_nonbool(var);
 		if (!strcmp(value, "rename"))
-			object_creation_mode = OBJECT_CREATION_USES_RENAMES;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_RENAMES;
 		else if (!strcmp(value, "link"))
-			object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
 		else
 			die(_("invalid mode for object creation: %s"), value);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->autorebase = AUTOREBASE_NEVER;
+	cfg->object_creation_mode = OBJECT_CREATION_MODE;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +6 -6
index 464ff73136..eaa0aba7bc 100644
@@ -109,6 +109,11 @@ enum rebase_setup_type {
 	AUTOREBASE_ALWAYS
 };
 
+enum object_creation_mode {
+	OBJECT_CREATION_USES_HARDLINKS = 0,
+	OBJECT_CREATION_USES_RENAMES = 1
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -120,6 +125,7 @@ struct repo_config_values {
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
 	enum rebase_setup_type autorebase;
+	enum object_creation_mode object_creation_mode;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -213,12 +219,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum object_creation_mode {
-	OBJECT_CREATION_USES_HARDLINKS = 0,
-	OBJECT_CREATION_USES_RENAMES = 1
-};
-extern enum object_creation_mode object_creation_mode;
-
 extern int grafts_keep_true_parents;
 
 const char *get_log_output_encoding(void);
object-file.c +1 -1
index 9afa842da2..cbbfc8f1dc 100644
@@ -415,7 +415,7 @@ int finalize_object_file_flags(struct repository *repo,
 retry:
 	ret = 0;
 
-	if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
+	if (repo_config_values(repo)->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
 		goto try_rename;
 	else if (link(tmpfile, filename))
 		ret = errno;
Signature · 1 line
2.43.0
#40 lore raw

v8 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Change since v7:

Fixed a memory leak in pager.c.

Thanks!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

 apply.c        | 20 +++++++-----
 branch.c       |  2 +-
 builtin/push.c |  8 ++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 87 +++++++++++++++++++++++++++++++++++---------------
 environment.h  | 75 +++++++++++++++++++++++++++----------------
 object-file.c  |  2 +-
 pager.c        | 21 +++++++-----
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 12 files changed, 148 insertions(+), 81 deletions(-)
Signature · 1 line
2.43.0
#41 lore raw

PATCH v8 1/9 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
 environment.c | 19 +++++++++++++++++++
 environment.h |  9 +++++++++
 repository.c  |  1 +
 3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#42 lore raw

PATCH v8 2/9 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()'
in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced repo_config_values_clear() function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 15 ++++++++++++---
 environment.h |  4 +++-
 3 files changed, 17 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +12 -3
index 13677484de..5950592d63 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,14 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +752,5 @@ void repo_config_values_clear(struct repository *repo)
 	cfg = repo_config_values(repo);
 
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index c4a6a45704..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#43 lore raw

PATCH v8 3/9 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..07d264cba0 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor && repo_config_values(the_repository)->editor_program)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 5950592d63..0a01f4761a 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -435,8 +434,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -723,6 +722,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -753,4 +753,5 @@ void repo_config_values_clear(struct repository *repo)
 
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 2e8352de7f..1ec19149cb 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#44 lore raw

PATCH v8 4/9 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix a memory leak in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 21 +++++++++++++--------
 3 files changed, 16 insertions(+), 8 deletions(-)
environment.c +2 -0
index 0a01f4761a..a1204fdcb2 100644
@@ -723,6 +723,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -754,4 +755,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 1ec19149cb..22f6697c52 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +13 -8
index 35b210e048..c8ebdd4b31 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,12 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
+	struct repository *r = data;
+
 	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
 	return 0;
 }
 
@@ -91,10 +94,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +305,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
 	return data.want;
 }
Signature · 1 line
2.43.0
#45 lore raw

Re: [PATCH v8 4/9] environment: move pager_program into repo_config_values

Quoted 1 line · Tian Yuchen

On top of that, fix a memory leak in pager.c while we are at it.

Hmph.

pager.c static void wait_for_pager_signal(int signo) · line 84

Quoted 12 lines
pager.c +4 -2
@@ -75,10 +76,12 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
+	struct repository *r = data;
+
 	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);

Isn't this still overwriting what was in the .pager_program member of the config values struct? In check_pager_config() below, there is a free() to avoid such a leak, but wouldn't this have the same issue?

Quoted 26 lines
pager.c +7 -5
@@ -91,10 +94,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +305,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
 	return data.want;
 }
#46 lore raw

Re: [PATCH v8 4/9] environment: move pager_program into repo_config_values

Quoted 42 lines · Junio C Hamano
Quoted 1 line · Tian Yuchen

On top of that, fix a memory leak in pager.c while we are at it.

Hmph.

pager.c static void wait_for_pager_signal(int signo) · line 84

Quoted 12 lines
pager.c +4 -2
@@ -75,10 +76,12 @@ static void wait_for_pager_signal(int signo)
 
  static int core_pager_config(const char *var, const char *value,
  			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
  {
+	struct repository *r = data;
+
  	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);

Isn't this still overwriting what was in the .pager_program member of the config values struct? In check_pager_config() below, there is a free() to avoid such a leak, but wouldn't this have the same issue?

Quoted 18 lines
pager.c +3 -3
@@ -91,10 +94,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
  	pager = getenv("GIT_PAGER");
  	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
  			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
  	}
  	if (!pager)
  		pager = getenv("PAGER");
@@ -302,7 +305,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
  	read_early_config(r, pager_command_config, &data);
 
Quoted 8 lines

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 313

Quoted 8 lines
pager.c +4 -2
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
  	return data.want;
  }

Nice catch, sorry for missing that!

Regards, yuchen

#47 lore raw

Re: [PATCH v8 4/9] environment: move pager_program into repo_config_values

Quoted 27 lines · Tian Yuchen
Quoted 27 lines

pager.c static void wait_for_pager_signal(int signo) · line 84

Quoted 3 lines

if (!strcmp(var, "core.pager"))

pager.c +1 -1
-		return git_config_string(&pager_program, var, value);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);

Isn't this still overwriting what was in the .pager_program member of the config values struct? In check_pager_config() below, there is a free() to avoid such a leak, but wouldn't this have the same issue?

Quoted 18 lines
pager.c +3 -3
@@ -91,10 +94,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
  	pager = getenv("GIT_PAGER");
  	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
  			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
  	}
  	if (!pager)
  		pager = getenv("PAGER");
@@ -302,7 +305,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
  	read_early_config(r, pager_command_config, &data);
 
Quoted 10 lines
Quoted 8 lines

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 313

Quoted 8 lines
pager.c +4 -2
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
  	return data.want;
  }

Nice catch, sorry for missing that!

You do not have to be or say sorry. This is a team effort, and I am reasonably sure that I did not catch _all_ similar bugs in this iteration. So before you send an updated version, please make sure that you just do not fix this one only and be content with it. Instead try to see if there are other similar issues and fix them, too.

Thanks.

#48 lore raw

PATCH v8 5/9 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +4 -2
index a1204fdcb2..3782bf68aa 100644
@@ -462,8 +462,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -724,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -756,4 +757,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -0
index 22f6697c52..d55b1ba073 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#49 lore raw

PATCH v8 6/9 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 20 ++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 18 insertions(+), 12 deletions(-)
apply.c +12 -8
index 249248d4f2..66db9b7678 100644
@@ -47,11 +47,13 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &repo_config_values(repo)->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &repo_config_values(repo)->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +128,12 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+	if (repo_config_values(repo)->apply_default_whitespace &&
+	    parse_whitespace_option(state, repo_config_values(repo)->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (repo_config_values(repo)->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, repo_config_values(repo)->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +196,7 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 3782bf68aa..8744790219 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -725,6 +723,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -758,4 +758,6 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index d55b1ba073..9aecd64152 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#50 lore raw

PATCH v8 7/9 environment: move push_default into repo_config_values

In reply to Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c |  8 ++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 30 insertions(+), 22 deletions(-)
builtin/push.c +4 -4
index 6021b71d66..6dc3224b60 100644
@@ -88,7 +88,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (repo_config_values(the_repository)->push_default == PUSH_DEFAULT_UPSTREAM &&
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +160,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -232,7 +232,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	const char *dst;
 	int same_remote;
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +252,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 8744790219..09de2fee87 100644
@@ -59,7 +59,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -619,21 +618,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -725,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index 9aecd64152..72859b5d76 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +1 -1
index 00723b385e..d48c01d375 100644
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 
Signature · 1 line
2.43.0
#51 lore raw

PATCH v8 8/9 environment: move autorebase into repo_config_values

In reply to Tian Yuchen

The global variable 'autorebase' dictates whether a newly created branch should be configured to automatically rebase by default. Move it into 'struct repo_config_values' to continue the libification effort.

The 'enum rebase_setup_type' definition is moved higher up in 'environment.h' so that it is visible to the repository-specific structure. The default state AUTOREBASE_NEVER is now correctly initialized in 'repo_config_values_init()'.

Configuration parsing in 'git_default_branch_config()' is updated to write directly to the repository's configuration instance.

---
 branch.c      |  2 +-
 environment.c | 10 +++++-----
 environment.h | 16 ++++++++--------
 3 files changed, 14 insertions(+), 14 deletions(-)
branch.c +1 -1
index 243db7d0fc..e1c1f8c89d 100644
@@ -61,7 +61,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
 static int should_setup_rebase(const char *origin)
 {
-	switch (autorebase) {
+	switch (repo_config_values(the_repository)->autorebase) {
 	case AUTOREBASE_NEVER:
 		return 0;
 	case AUTOREBASE_LOCAL:
environment.c +5 -5
index 09de2fee87..7701aa3bc0 100644
@@ -58,7 +58,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
-enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -600,13 +599,13 @@ static int git_default_branch_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "never"))
-			autorebase = AUTOREBASE_NEVER;
+			cfg->autorebase = AUTOREBASE_NEVER;
 		else if (!strcmp(value, "local"))
-			autorebase = AUTOREBASE_LOCAL;
+			cfg->autorebase = AUTOREBASE_LOCAL;
 		else if (!strcmp(value, "remote"))
-			autorebase = AUTOREBASE_REMOTE;
+			cfg->autorebase = AUTOREBASE_REMOTE;
 		else if (!strcmp(value, "always"))
-			autorebase = AUTOREBASE_ALWAYS;
+			cfg->autorebase = AUTOREBASE_ALWAYS;
 		else
 			return error(_("malformed value for %s"), var);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
+	cfg->autorebase = AUTOREBASE_NEVER;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +8 -8
index 72859b5d76..464ff73136 100644
@@ -102,6 +102,13 @@ enum push_default_type {
 	PUSH_DEFAULT_UNSPECIFIED
 };
 
+enum rebase_setup_type {
+	AUTOREBASE_NEVER = 0,
+	AUTOREBASE_LOCAL,
+	AUTOREBASE_REMOTE,
+	AUTOREBASE_ALWAYS
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -112,6 +119,7 @@ struct repo_config_values {
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
+	enum rebase_setup_type autorebase;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -205,14 +213,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum rebase_setup_type {
-	AUTOREBASE_NEVER = 0,
-	AUTOREBASE_LOCAL,
-	AUTOREBASE_REMOTE,
-	AUTOREBASE_ALWAYS
-};
-extern enum rebase_setup_type autorebase;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
Signature · 1 line
2.43.0
#52 lore raw

PATCH v8 9/9 environment: move object_creation_mode into repo_config_values

In reply to Tian Yuchen

The global variable 'object_creation_mode' controls how Git creates object files, specifically determining whether to use hardlinks or renames when moving temporary files into the object database. Move it into 'struct repo_config_values' to continue the libification effort.

Move the 'enum object_creation_mode' definition higher up in 'environment.h' to ensure it is visible to the structure. Initialize the per-repository value to its default macro value OBJECT_CREATION_MODE inside 'repo_config_values_init()'.

Update configuration parsing in 'git_default_core_config()' to write directly to the repository-specific configuration structure.

---
 environment.c |  6 +++---
 environment.h | 12 ++++++------
 object-file.c |  2 +-
 3 files changed, 10 insertions(+), 10 deletions(-)
environment.c +3 -3
index 7701aa3bc0..e50beda918 100644
@@ -61,7 +61,6 @@ char *check_roundtrip_encoding;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
-enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
 unsigned long pack_size_limit_cfg;
 
@@ -511,9 +510,9 @@ int git_default_core_config(const char *var, const char *value,
 		if (!value)
 			return config_error_nonbool(var);
 		if (!strcmp(value, "rename"))
-			object_creation_mode = OBJECT_CREATION_USES_RENAMES;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_RENAMES;
 		else if (!strcmp(value, "link"))
-			object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
 		else
 			die(_("invalid mode for object creation: %s"), value);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->autorebase = AUTOREBASE_NEVER;
+	cfg->object_creation_mode = OBJECT_CREATION_MODE;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +6 -6
index 464ff73136..eaa0aba7bc 100644
@@ -109,6 +109,11 @@ enum rebase_setup_type {
 	AUTOREBASE_ALWAYS
 };
 
+enum object_creation_mode {
+	OBJECT_CREATION_USES_HARDLINKS = 0,
+	OBJECT_CREATION_USES_RENAMES = 1
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -120,6 +125,7 @@ struct repo_config_values {
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
 	enum rebase_setup_type autorebase;
+	enum object_creation_mode object_creation_mode;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -213,12 +219,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum object_creation_mode {
-	OBJECT_CREATION_USES_HARDLINKS = 0,
-	OBJECT_CREATION_USES_RENAMES = 1
-};
-extern enum object_creation_mode object_creation_mode;
-
 extern int grafts_keep_true_parents;
 
 const char *get_log_output_encoding(void);
object-file.c +1 -1
index 9afa842da2..cbbfc8f1dc 100644
@@ -415,7 +415,7 @@ int finalize_object_file_flags(struct repository *repo,
 retry:
 	ret = 0;
 
-	if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
+	if (repo_config_values(repo)->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
 		goto try_rename;
 	else if (link(tmpfile, filename))
 		ret = errno;
Signature · 1 line
2.43.0
#53 lore raw

v9 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Change since v8:

Fixed a memory leak in pager.c.

Thanks!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

 apply.c        | 20 +++++++-----
 branch.c       |  2 +-
 builtin/push.c |  8 ++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 87 +++++++++++++++++++++++++++++++++++---------------
 environment.h  | 75 +++++++++++++++++++++++++++----------------
 object-file.c  |  2 +-
 pager.c        | 26 +++++++++------
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 12 files changed, 152 insertions(+), 82 deletions(-)
Signature · 1 line
2.43.0
#54 lore raw

PATCH v9 1/9 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
 environment.c | 19 +++++++++++++++++++
 environment.h |  9 +++++++++
 repository.c  |  1 +
 3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#55 lore raw

Re: [PATCH v9 1/9] repository: introduce repo_config_values_clear()

Quoted 9 lines · Tian Yuchen

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Makes sense.

Quoted 51 lines

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
 environment.c | 19 +++++++++++++++++++
 environment.h |  9 +++++++++
 repository.c  |  1 +
 3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +0 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 

environment.h int git_default_core_config(const char *var, const char *value, · line 145

Quoted 10 lines
environment.h +8 -0
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);

I think that I'm not comfortable having the _init() and the _clear() functions with different signatures.

_clear() takes struct repository to dodge a BUG().

I would like to have both signatures equal, why can't we just do directly:

  void repo_config_values_clear(struct repo_config_values *cfg)
  {
  	FREE_AND_NULL(cfg->attributes_file);
  }

and call from repo_clear():

repo_config_values_clear(&repo->config_values_private_)

I get that the workaround might be to not access directly to &repo->config_values_private_ which repo_config_values() returns but for example initialize_repository() access this _private_ field directly as well.

Even with the NEEDSWORK it is a silent return, what will happen when submodules are supported? If no one remembers to change it we will leak the submodules silently.

Also at repo_init(), initialize_repository() sets repo->initialized before anything can fail and call repo_clear() but repo_config_values_clear() should be able to free attributes_file even just after a memset() (which happens before initialize_repository()). But calling repo_config_values has a BUG() in case of !repo->initialized are we comfortable with this assert?

Quoted 9 lines
environment.h +1 -0
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +0 -0
index 187dd471c4..b31f1b7852 100644

repository.c void repo_clear(struct repository *repo) · line 394

Quoted 7 lines
repository.c +1 -0
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);

Regards,
Pablo

#56 lore raw

Re: [PATCH v9 1/9] repository: introduce repo_config_values_clear()

Hii Pablo,

Quoted 82 lines · Pablo Sabater
Quoted 9 lines · Tian Yuchen

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Makes sense.

Quoted 51 lines

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
  environment.c | 19 +++++++++++++++++++
  environment.h |  9 +++++++++
  repository.c  |  1 +
  3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
  	cfg->sparse_expect_files_outside_of_patterns = 0;
  	cfg->warn_on_object_refname_ambiguity = 1;
  }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +0 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 

environment.h int git_default_core_config(const char *var, const char *value, · line 145

Quoted 10 lines
environment.h +8 -0
  void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);

I think that I'm not comfortable having the _init() and the _clear() functions with different signatures.

_clear() takes struct repository to dodge a BUG().

I would like to have both signatures equal, why can't we just do directly:

Quoted 9 lines
   void repo_config_values_clear(struct repo_config_values *cfg)
   {
   	FREE_AND_NULL(cfg->attributes_file);
   }

and call from repo_clear():

   repo_config_values_clear(&repo->config_values_private_)

I particularly agree with your point that the signatures of these two functions should be consistent. I missed it tbh...I will change it in the next reroll.

However, I think it makes more sense to refactor to pass in 'struct repository', which is consistent with repo-settings.

Quoted 4 lines

I get that the workaround might be to not access directly to &repo->config_values_private_ which repo_config_values() returns but for example initialize_repository() access this _private_ field directly as well.

Now that we have used the _private_ suffix, if we can just define a _clear() to bypass the assertion of repo_config_values(), wouldn't this be self-deception? I'm not saying that the original lines are necessarily correct... but I do think that semantically speaking, it is inappropriate to pass in config_values_private_ to _clear().

Quoted 6 lines

Also at repo_init(), initialize_repository() sets repo->initialized before anything can fail and call repo_clear() but repo_config_values_clear() should be able to free attributes_file even just after a memset() (which happens before initialize_repository()). But calling repo_config_values has a BUG() in case of !repo->initialized are we comfortable with this assert?

This goes back to the previous topic: Who is responsible for the call to _clear()? Who is responsible for filtering all those invalid usage of repo instances? Faced with a repo instance that was not initialized but was handed over to _clear() in some way, we have two concepts:

- It doesn't matter. Since we always handle config_values_private_, it's NULL at this point, so we don't BUG() and continue.

- The very existence of such a repo is a mistake. It shouldn't have appeared and shouldn't have been passed on to me. However, since this situation is relatively common at this point, we choose to return instead of BUG()ing it directly to temporarily avoid it. We will gradually tighten the conditions. When the invalid calls are eliminated in the end, such checks will no longer exist.

Our consensus should at least be that this 'use of uninitialized repos' is bad, so they are just two different ways to solve unexpected situations. However, in my opinion, the difference between these two concepts lies in whether we are consciously moving the assertion downward. I think the latter line of thinking does this better.

Quoted 5 lines

Even with the NEEDSWORK it is a silent return, what will happen when submodules are supported? If no one remembers to change it we will leak the submodules silently.

I will remember to change it ;)

Regards, yuchen

Quoted 19 lines
Quoted 9 lines
environment.h +1 -0
+
  /*
   * TODO: All the below state either explicitly or implicitly relies on
   * `the_repository`. We should eventually get rid of these and make the
repository.c +0 -0
index 187dd471c4..b31f1b7852 100644

repository.c void repo_clear(struct repository *repo) · line 394

Quoted 7 lines
repository.c +1 -0
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
  	FREE_AND_NULL(repo->parsed_objects);
 
  	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
  	if (repo->config) {
  		git_configset_clear(repo->config);

Regards,
Pablo

#57 lore raw

Re: [PATCH v9 1/9] repository: introduce repo_config_values_clear()

Quoted 95 lines · Tian Yuchen

Hii Pablo,

Quoted 82 lines · Pablo Sabater
Quoted 9 lines · Tian Yuchen

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Makes sense.

Quoted 51 lines

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
  environment.c | 19 +++++++++++++++++++
  environment.h |  9 +++++++++
  repository.c  |  1 +
  3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
  	cfg->sparse_expect_files_outside_of_patterns = 0;
  	cfg->warn_on_object_refname_ambiguity = 1;
  }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +0 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 

environment.h int git_default_core_config(const char *var, const char *value, · line 145

Quoted 10 lines
environment.h +8 -0
  void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);

I think that I'm not comfortable having the _init() and the _clear() functions with different signatures.

_clear() takes struct repository to dodge a BUG().

I would like to have both signatures equal, why can't we just do directly:

Quoted 9 lines
   void repo_config_values_clear(struct repo_config_values *cfg)
   {
   	FREE_AND_NULL(cfg->attributes_file);
   }

and call from repo_clear():

   repo_config_values_clear(&repo->config_values_private_)
Quoted 6 lines

I particularly agree with your point that the signatures of these two functions should be consistent. I missed it tbh...I will change it in the next reroll.

However, I think it makes more sense to refactor to pass in 'struct repository', which is consistent with repo-settings.

Whatever the signature is as long as it's symmetrical, is fine by me. However having 'struct repository' on _init() just passes the problem that _clear() had.

Let's look at initialize_repository():

	void initialize_repository(struct repository *repo)
	{
		if (repo->initialized)
			BUG("repository initialized already");
		repo->initialized = true;
	[snip]
		repo_config_values_init(&repo->config_values_private_);
	[snip]

if we change the _init() signature to receive 'struct repository', how do we access our _private_ field? We cannot call repo_config_values() on repo_config_values_init() because initialize_repository() is also called for submodules (repo_init() -> initialize_repository()) so we would BUG() out. We would have to access the _private_ field directly at _init().

With _clear() accessing directly the _private_ field we can forget about the NEEDSWORK, it works for every repository.

Quoted 11 lines
Quoted 5 lines

I get that the workaround might be to not access directly to &repo->config_values_private_ which repo_config_values() returns but for example initialize_repository() access this _private_ field directly as well.

Now that we have used the _private_ suffix, if we can just define a _clear() to bypass the assertion of repo_config_values(), wouldn't this be self-deception? I'm not saying that the original lines are necessarily correct... but I do think that semantically speaking, it is inappropriate to pass in config_values_private_ to _clear().

I don't think it would be "self-deception": _init() and _clear() are the start and end of this _private_ field's lifecycle so having them receive 'struct repo_config_values' directly sounds reasonable to me.

Quoted 23 lines
Quoted 7 lines

Also at repo_init(), initialize_repository() sets repo->initialized before anything can fail and call repo_clear() but repo_config_values_clear() should be able to free attributes_file even just after a memset() (which happens before initialize_repository()). But calling repo_config_values has a BUG() in case of !repo->initialized are we comfortable with this assert?

This goes back to the previous topic: Who is responsible for the call to _clear()? Who is responsible for filtering all those invalid usage of repo instances? Faced with a repo instance that was not initialized but was handed over to _clear() in some way, we have two concepts:

- It doesn't matter. Since we always handle config_values_private_, it's NULL at this point, so we don't BUG() and continue.

- The very existence of such a repo is a mistake. It shouldn't have appeared and shouldn't have been passed on to me. However, since this situation is relatively common at this point, we choose to return instead of BUG()ing it directly to temporarily avoid it. We will gradually tighten the conditions. When the invalid calls are eliminated in the end, such checks will no longer exist.

Quoted 5 lines

Our consensus should at least be that this 'use of uninitialized repos' is bad, so they are just two different ways to solve unexpected situations. However, in my opinion, the difference between these two concepts lies in whether we are consciously moving the assertion downward. I think the latter line of thinking does this better.

Sounds reasonable, but I think I would go with the first thought of "It doesn't matter...".

Having _clear() access directly removes the need for tightening the conditions, because there would be none. The _clear() works for any repo as long as it has gone through a memset().

I think that the ones that should ensure that we don't have a "bad repository" should be the rest of the code and leave _clear() to just clear.

Quoted 31 lines
Quoted 7 lines

Even with the NEEDSWORK it is a silent return, what will happen when submodules are supported? If no one remembers to change it we will leak the submodules silently.

I will remember to change it ;)

Regards, yuchen

Quoted 19 lines
Quoted 9 lines
environment.h +1 -0
+
  /*
   * TODO: All the below state either explicitly or implicitly relies on
   * `the_repository`. We should eventually get rid of these and make the
repository.c +0 -0
index 187dd471c4..b31f1b7852 100644

repository.c void repo_clear(struct repository *repo) · line 394

Quoted 7 lines
repository.c +1 -0
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
  	FREE_AND_NULL(repo->parsed_objects);
 
  	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
  	if (repo->config) {
  		git_configset_clear(repo->config);

Regards,
Pablo

Regards,
Pablo

#58 lore raw

Re: [PATCH v9 1/9] repository: introduce repo_config_values_clear()

Hey Pablo,

Quoted 209 lines · Pablo Sabater
Quoted 95 lines · Tian Yuchen

Hii Pablo,

Quoted 82 lines · Pablo Sabater
Quoted 9 lines · Tian Yuchen

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

Makes sense.

Quoted 51 lines

Note:

Submodules are currently not supported by repo_config_values(), which explicitly BUG()s out if 'repo != the_repository'. Since repo_clear() cleans up all repository instances, we must bypass them to prevent crashing.

---
   environment.c | 19 +++++++++++++++++++
   environment.h |  9 +++++++++
   repository.c  |  1 +
   3 files changed, 29 insertions(+)
environment.c +19 -0
index ba2c60103f..13677484de 100644
@@ -726,3 +726,22 @@ void repo_config_values_init(struct repo_config_values *cfg)
   	cfg->sparse_expect_files_outside_of_patterns = 0;
   	cfg->warn_on_object_refname_ambiguity = 1;
   }
+
+void repo_config_values_clear(struct repository *repo)
+{
+	struct repo_config_values *cfg;
+
+	/*
+	 * NEEDSWORK: Submodules are currently not supported by
+	 * repo_config_values(), which explicitly BUG()s out if
+	 * repo != the_repository. Since repo_clear() cleans up all
+	 * repository instances, we must bypass them here to prevent
+	 * crashing.
+	 */
+	if (repo != the_repository)
+		return;
+
+	cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +0 -0
index 6f18286955..c4a6a45704 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 

environment.h int git_default_core_config(const char *var, const char *value, · line 145

Quoted 10 lines
environment.h +8 -0
   void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repository *repo);

I think that I'm not comfortable having the _init() and the _clear() functions with different signatures.

_clear() takes struct repository to dodge a BUG().

I would like to have both signatures equal, why can't we just do directly:

Quoted 9 lines
    void repo_config_values_clear(struct repo_config_values *cfg)
    {
    	FREE_AND_NULL(cfg->attributes_file);
    }

and call from repo_clear():

    repo_config_values_clear(&repo->config_values_private_)
Quoted 6 lines

I particularly agree with your point that the signatures of these two functions should be consistent. I missed it tbh...I will change it in the next reroll.

However, I think it makes more sense to refactor to pass in 'struct repository', which is consistent with repo-settings.

Whatever the signature is as long as it's symmetrical, is fine by me. However having 'struct repository' on _init() just passes the problem that _clear() had.

Let's look at initialize_repository():

	void initialize_repository(struct repository *repo)
	{
		if (repo->initialized)
			BUG("repository initialized already");
		repo->initialized = true;
	[snip]
		repo_config_values_init(&repo->config_values_private_);
	[snip]

if we change the _init() signature to receive 'struct repository', how do we access our _private_ field? We cannot call repo_config_values() on repo_config_values_init() because initialize_repository() is also called for submodules (repo_init() -> initialize_repository()) so we would BUG() out. We would have to access the _private_ field directly at _init().

With _clear() accessing directly the _private_ field we can forget about the NEEDSWORK, it works for every repository.

Quoted 11 lines
Quoted 5 lines

I get that the workaround might be to not access directly to &repo->config_values_private_ which repo_config_values() returns but for example initialize_repository() access this _private_ field directly as well.

Now that we have used the _private_ suffix, if we can just define a _clear() to bypass the assertion of repo_config_values(), wouldn't this be self-deception? I'm not saying that the original lines are necessarily correct... but I do think that semantically speaking, it is inappropriate to pass in config_values_private_ to _clear().

I don't think it would be "self-deception": _init() and _clear() are the start and end of this _private_ field's lifecycle so having them receive 'struct repo_config_values' directly sounds reasonable to me.

Quoted 23 lines
Quoted 7 lines

Also at repo_init(), initialize_repository() sets repo->initialized before anything can fail and call repo_clear() but repo_config_values_clear() should be able to free attributes_file even just after a memset() (which happens before initialize_repository()). But calling repo_config_values has a BUG() in case of !repo->initialized are we comfortable with this assert?

This goes back to the previous topic: Who is responsible for the call to _clear()? Who is responsible for filtering all those invalid usage of repo instances? Faced with a repo instance that was not initialized but was handed over to _clear() in some way, we have two concepts:

- It doesn't matter. Since we always handle config_values_private_, it's NULL at this point, so we don't BUG() and continue.

- The very existence of such a repo is a mistake. It shouldn't have appeared and shouldn't have been passed on to me. However, since this situation is relatively common at this point, we choose to return instead of BUG()ing it directly to temporarily avoid it. We will gradually tighten the conditions. When the invalid calls are eliminated in the end, such checks will no longer exist.

Quoted 5 lines

Our consensus should at least be that this 'use of uninitialized repos' is bad, so they are just two different ways to solve unexpected situations. However, in my opinion, the difference between these two concepts lies in whether we are consciously moving the assertion downward. I think the latter line of thinking does this better.

Sounds reasonable, but I think I would go with the first thought of "It doesn't matter...".

Having _clear() access directly removes the need for tightening the conditions, because there would be none. The _clear() works for any repo as long as it has gone through a memset().

I think that the ones that should ensure that we don't have a "bad repository" should be the rest of the code and leave _clear() to just clear.

Quoted 26 lines
Quoted 7 lines

Even with the NEEDSWORK it is a silent return, what will happen when submodules are supported? If no one remembers to change it we will leak the submodules silently.

I will remember to change it ;)

Regards, yuchen

Quoted 14 lines
Quoted 14 lines
environment.h +1 -0
+
   /*
    * TODO: All the below state either explicitly or implicitly relies on
    * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..b31f1b7852 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
   	FREE_AND_NULL(repo->parsed_objects);
 
   	repo_settings_clear(repo);
+	repo_config_values_clear(repo);
 
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

repository.c void repo_clear(struct repository *repo) · line 394

Quoted 2 lines
repository.c +0 -0
   	if (repo->config) {
   		git_configset_clear(repo->config);

Regards,
Pablo

Regards,
Pablo

I thought about it again, and okay, I have to admit your approach is indeed more reasonable. Maybe it was just my stubbornness stemming from staying up late thinking last night. :D

Thanks! yuchen

#59 lore raw

PATCH v9 2/9 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()'
in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced repo_config_values_clear() function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 15 ++++++++++++---
 environment.h |  4 +++-
 3 files changed, 17 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +12 -3
index 13677484de..5950592d63 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,14 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +752,5 @@ void repo_config_values_clear(struct repository *repo)
 	cfg = repo_config_values(repo);
 
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index c4a6a45704..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#60 lore raw
Quoted 3 lines · Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()'

Nit: Strange line break here.

Quoted 52 lines

in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced repo_config_values_clear() function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 15 ++++++++++++---
 environment.h |  4 +++-
 3 files changed, 17 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +0 -1
index 13677484de..5950592d63 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;

environment.c int is_bare_repository(void) · line 142

Quoted 10 lines
environment.c +7 -0
@@ -134,6 +133,14 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}

repo_config_values() returns a pointer so there should be no need to call the function 3 times.

We could have the function be called once and use it then:

const char *repo_excludes_file(struct repository *repo)

  {
	  struct repo_config_values *cfg = repo_config_values(repo);

	  if (!cfg->excludes_file)
		  cfg->excludes_file = xdg_config_home("ignore");

	  return cfg->excludes_file;

}

Quoted 51 lines
environment.c +5 -2
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +752,5 @@ void repo_config_values_clear(struct repository *repo)
 	cfg = repo_config_values(repo);
 
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -0
index c4a6a45704..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*

environment.h extern char *git_log_output_encoding; · line 225

Quoted 7 lines
environment.h +0 -1
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file

The rest looks fine.

Regards,
Pablo

#61 lore raw
Quoted 75 lines · Pablo Sabater
Quoted 3 lines · Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()'

Nit: Strange line break here.

Quoted 52 lines

in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced repo_config_values_clear() function when the repository is torn down.

---
  dir.c         |  4 ++--
  environment.c | 15 ++++++++++++---
  environment.h |  4 +++-
  3 files changed, 17 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
  void setup_standard_excludes(struct dir_struct *dir)
  {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
  	dir->exclude_per_dir = ".gitignore";
 
  	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
  	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
  		add_patterns_from_file_1(dir, excludes_file,
  					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +0 -1
index 13677484de..5950592d63 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
  enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
  char *editor_program;
  char *askpass_program;
-char *excludes_file;
  enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
  enum eol core_eol = EOL_UNSET;
  int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;

environment.c int is_bare_repository(void) · line 142

Quoted 10 lines
environment.c +7 -0
@@ -134,6 +133,14 @@ int is_bare_repository(void)
  	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
  }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	if (!repo_config_values(repo)->excludes_file)
+		repo_config_values(repo)->excludes_file = xdg_config_home("ignore");
+
+	return repo_config_values(repo)->excludes_file;
+}

repo_config_values() returns a pointer so there should be no need to call the function 3 times.

We could have the function be called once and use it then:

Quoted 10 lines
   const char *repo_excludes_file(struct repository *repo)
   {
	  struct repo_config_values *cfg = repo_config_values(repo);

	  if (!cfg->excludes_file)
		  cfg->excludes_file = xdg_config_home("ignore");

	  return cfg->excludes_file;
   }

Makes sense, thanks.

Quoted 56 lines
Quoted 56 lines
environment.c +5 -2
+
  int have_git_dir(void)
  {
  	return startup_info->have_repository
@@ -461,8 +468,8 @@ int git_default_core_config(const char *var, const char *value,
  	}
 
  	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
  	}
 
  	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +722,7 @@ int git_default_config(const char *var, const char *value,
  void repo_config_values_init(struct repo_config_values *cfg)
  {
  	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
  	cfg->apply_sparse_checkout = 0;
  	cfg->branch_track = BRANCH_TRACK_REMOTE;
  	cfg->trust_ctime = 1;
@@ -744,4 +752,5 @@ void repo_config_values_clear(struct repository *repo)
  	cfg = repo_config_values(repo);
 
  	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
  }
environment.h +3 -1
index c4a6a45704..2e8352de7f 100644
@@ -90,6 +90,7 @@ struct repository;
  struct repo_config_values {
  	/* section "core" config values */
  	char *attributes_file;
+	char *excludes_file;
  	int apply_sparse_checkout;
  	int trust_ctime;
  	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
  int git_default_core_config(const char *var, const char *value,
  			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
  void repo_config_values_init(struct repo_config_values *cfg);
 
  /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
  extern char *editor_program;
  extern char *askpass_program;
-extern char *excludes_file;
 
Quoted 7 lines

environment.h extern char *git_log_output_encoding; · line 225

Quoted 2 lines
environment.h +0 -0
  /*
   * The character that begins a commented line in user-editable file

The rest looks fine.

Regards,
Pablo

Regards, yuchen

#62 lore raw

PATCH v9 3/9 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..07d264cba0 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor && repo_config_values(the_repository)->editor_program)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 5950592d63..0a01f4761a 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -435,8 +434,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -723,6 +722,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -753,4 +753,5 @@ void repo_config_values_clear(struct repository *repo)
 
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 2e8352de7f..1ec19149cb 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#63 lore raw

Re: [PATCH v9 3/9] environment: move editor_program into repo_config_values

[Resending review against v9, I accidentally sent the review in-reply-to v7. The patch seems to not have changed and it applies to v9 as well.]

Quoted 9 lines · Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

Quoted 7 lines

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes managing multiple repositories concurrently must not overwrite each other's program configurations.

Nit:
"As the codebase moves toward becoming long-running processes, managing multiple repositories concurrently must not overwrite each other's program configurations"

Quoted 4 lines

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

Why no getter function here? in patch 2/9 repo_excludes_file() is introduced and also access the field via repo_config_values().

Super nit: on a previous commit you used backquotes for repo_config_values_clear() but now you're using single quotes.

Quoted 16 lines
---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +0 -0
index fd174e6a03..07d264cba0 100644

editor.c const char *git_editor(void) · line 33

Quoted 7 lines
editor.c +2 -2
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor && repo_config_values(the_repository)->editor_program)
+		editor = repo_config_values(the_repository)->editor_program;

Same as the previous patch, we can store repo_config_values() and avoid re-calling the function.
Also, do we need the right-side condition?

Quoted 53 lines
editor.c +0 -0
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 5950592d63..0a01f4761a 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -435,8 +434,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -723,6 +722,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -753,4 +753,5 @@ void repo_config_values_clear(struct repository *repo)
 
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -0
index 2e8352de7f..1ec19149cb 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;

environment.h const char *get_commit_output_encoding(void); · line 224

Quoted 7 lines
environment.h +0 -1
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*

Regards,
Pablo

#64 lore raw

PATCH v9 4/9 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix a memory leak in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 26 +++++++++++++++++---------
 3 files changed, 20 insertions(+), 9 deletions(-)
environment.c +2 -0
index 0a01f4761a..a1204fdcb2 100644
@@ -723,6 +723,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -754,4 +755,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 1ec19149cb..22f6697c52 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +17 -9
index 35b210e048..bc55546670 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,15 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}
+
 	return 0;
 }
 
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
 	return data.want;
 }
Signature · 1 line
2.43.0
#65 lore raw

PATCH v9 5/9 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +4 -2
index a1204fdcb2..3782bf68aa 100644
@@ -462,8 +462,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -724,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -756,4 +757,5 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -0
index 22f6697c52..d55b1ba073 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#66 lore raw

PATCH v9 6/9 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 20 ++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 18 insertions(+), 12 deletions(-)
apply.c +12 -8
index 249248d4f2..66db9b7678 100644
@@ -47,11 +47,13 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &repo_config_values(repo)->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &repo_config_values(repo)->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +128,12 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+	if (repo_config_values(repo)->apply_default_whitespace &&
+	    parse_whitespace_option(state, repo_config_values(repo)->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (repo_config_values(repo)->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, repo_config_values(repo)->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +196,7 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 3782bf68aa..8744790219 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -725,6 +723,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -758,4 +758,6 @@ void repo_config_values_clear(struct repository *repo)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index d55b1ba073..9aecd64152 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#67 lore raw

PATCH v9 7/9 environment: move push_default into repo_config_values

In reply to Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c |  8 ++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 30 insertions(+), 22 deletions(-)
builtin/push.c +4 -4
index 6021b71d66..6dc3224b60 100644
@@ -88,7 +88,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (repo_config_values(the_repository)->push_default == PUSH_DEFAULT_UPSTREAM &&
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +160,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -232,7 +232,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	const char *dst;
 	int same_remote;
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +252,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 8744790219..09de2fee87 100644
@@ -59,7 +59,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -619,21 +618,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -725,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index 9aecd64152..72859b5d76 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +1 -1
index 00723b385e..d48c01d375 100644
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 
Signature · 1 line
2.43.0
#68 lore raw

PATCH v9 8/9 environment: move autorebase into repo_config_values

In reply to Tian Yuchen

The global variable 'autorebase' dictates whether a newly created branch should be configured to automatically rebase by default. Move it into 'struct repo_config_values' to continue the libification effort.

The 'enum rebase_setup_type' definition is moved higher up in 'environment.h' so that it is visible to the repository-specific structure. The default state AUTOREBASE_NEVER is now correctly initialized in 'repo_config_values_init()'.

Configuration parsing in 'git_default_branch_config()' is updated to write directly to the repository's configuration instance.

---
 branch.c      |  2 +-
 environment.c | 10 +++++-----
 environment.h | 16 ++++++++--------
 3 files changed, 14 insertions(+), 14 deletions(-)
branch.c +1 -1
index 243db7d0fc..e1c1f8c89d 100644
@@ -61,7 +61,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
 static int should_setup_rebase(const char *origin)
 {
-	switch (autorebase) {
+	switch (repo_config_values(the_repository)->autorebase) {
 	case AUTOREBASE_NEVER:
 		return 0;
 	case AUTOREBASE_LOCAL:
environment.c +5 -5
index 09de2fee87..7701aa3bc0 100644
@@ -58,7 +58,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
-enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -600,13 +599,13 @@ static int git_default_branch_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "never"))
-			autorebase = AUTOREBASE_NEVER;
+			cfg->autorebase = AUTOREBASE_NEVER;
 		else if (!strcmp(value, "local"))
-			autorebase = AUTOREBASE_LOCAL;
+			cfg->autorebase = AUTOREBASE_LOCAL;
 		else if (!strcmp(value, "remote"))
-			autorebase = AUTOREBASE_REMOTE;
+			cfg->autorebase = AUTOREBASE_REMOTE;
 		else if (!strcmp(value, "always"))
-			autorebase = AUTOREBASE_ALWAYS;
+			cfg->autorebase = AUTOREBASE_ALWAYS;
 		else
 			return error(_("malformed value for %s"), var);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
+	cfg->autorebase = AUTOREBASE_NEVER;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +8 -8
index 72859b5d76..464ff73136 100644
@@ -102,6 +102,13 @@ enum push_default_type {
 	PUSH_DEFAULT_UNSPECIFIED
 };
 
+enum rebase_setup_type {
+	AUTOREBASE_NEVER = 0,
+	AUTOREBASE_LOCAL,
+	AUTOREBASE_REMOTE,
+	AUTOREBASE_ALWAYS
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -112,6 +119,7 @@ struct repo_config_values {
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
+	enum rebase_setup_type autorebase;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -205,14 +213,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum rebase_setup_type {
-	AUTOREBASE_NEVER = 0,
-	AUTOREBASE_LOCAL,
-	AUTOREBASE_REMOTE,
-	AUTOREBASE_ALWAYS
-};
-extern enum rebase_setup_type autorebase;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
Signature · 1 line
2.43.0
#69 lore raw

PATCH v9 9/9 environment: move object_creation_mode into repo_config_values

In reply to Tian Yuchen

The global variable 'object_creation_mode' controls how Git creates object files, specifically determining whether to use hardlinks or renames when moving temporary files into the object database. Move it into 'struct repo_config_values' to continue the libification effort.

Move the 'enum object_creation_mode' definition higher up in 'environment.h' to ensure it is visible to the structure. Initialize the per-repository value to its default macro value OBJECT_CREATION_MODE inside 'repo_config_values_init()'.

Update configuration parsing in 'git_default_core_config()' to write directly to the repository-specific configuration structure.

---
 environment.c |  6 +++---
 environment.h | 12 ++++++------
 object-file.c |  2 +-
 3 files changed, 10 insertions(+), 10 deletions(-)
environment.c +3 -3
index 7701aa3bc0..e50beda918 100644
@@ -61,7 +61,6 @@ char *check_roundtrip_encoding;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
-enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
 unsigned long pack_size_limit_cfg;
 
@@ -511,9 +510,9 @@ int git_default_core_config(const char *var, const char *value,
 		if (!value)
 			return config_error_nonbool(var);
 		if (!strcmp(value, "rename"))
-			object_creation_mode = OBJECT_CREATION_USES_RENAMES;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_RENAMES;
 		else if (!strcmp(value, "link"))
-			object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
 		else
 			die(_("invalid mode for object creation: %s"), value);
 		return 0;
@@ -727,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->autorebase = AUTOREBASE_NEVER;
+	cfg->object_creation_mode = OBJECT_CREATION_MODE;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +6 -6
index 464ff73136..eaa0aba7bc 100644
@@ -109,6 +109,11 @@ enum rebase_setup_type {
 	AUTOREBASE_ALWAYS
 };
 
+enum object_creation_mode {
+	OBJECT_CREATION_USES_HARDLINKS = 0,
+	OBJECT_CREATION_USES_RENAMES = 1
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -120,6 +125,7 @@ struct repo_config_values {
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
 	enum rebase_setup_type autorebase;
+	enum object_creation_mode object_creation_mode;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -213,12 +219,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum object_creation_mode {
-	OBJECT_CREATION_USES_HARDLINKS = 0,
-	OBJECT_CREATION_USES_RENAMES = 1
-};
-extern enum object_creation_mode object_creation_mode;
-
 extern int grafts_keep_true_parents;
 
 const char *get_log_output_encoding(void);
object-file.c +1 -1
index 9afa842da2..cbbfc8f1dc 100644
@@ -415,7 +415,7 @@ int finalize_object_file_flags(struct repository *repo,
 retry:
 	ret = 0;
 
-	if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
+	if (repo_config_values(repo)->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
 		goto try_rename;
 	else if (link(tmpfile, filename))
 		ret = errno;
Signature · 1 line
2.43.0
#70 lore raw

Re: [PATCH v9 0/9] migrate more variables into repo_config_values

In reply to Tian Yuchen
Quoted 56 lines · Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Change since v8:

Fixed a memory leak in pager.c.

Thanks!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

Quoted 13 lines
 apply.c        | 20 +++++++-----
 branch.c       |  2 +-
 builtin/push.c |  8 ++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 87 +++++++++++++++++++++++++++++++++++---------------
 environment.h  | 75 +++++++++++++++++++++++++++----------------
 object-file.c  |  2 +-
 pager.c        | 26 +++++++++------
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 12 files changed, 152 insertions(+), 82 deletions(-)

Hi!

I missed a base-commit to easily apply this locally, could we add one?

Thanks!
Pablo

#71 lore raw

Re: [PATCH v9 0/9] migrate more variables into repo_config_values

Hi Pablo,

Quoted 71 lines · Pablo Sabater
Quoted 56 lines · Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Change since v8:

Fixed a memory leak in pager.c.

Thanks!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

Quoted 13 lines
  apply.c        | 20 +++++++-----
  branch.c       |  2 +-
  builtin/push.c |  8 ++---
  dir.c          |  4 +--
  editor.c       |  4 +--
  environment.c  | 87 +++++++++++++++++++++++++++++++++++---------------
  environment.h  | 75 +++++++++++++++++++++++++++----------------
  object-file.c  |  2 +-
  pager.c        | 26 +++++++++------
  prompt.c       |  3 +-
  remote.c       |  2 +-
  repository.c   |  1 +
  12 files changed, 152 insertions(+), 82 deletions(-)
Quoted 7 lines

Hi!

I missed a base-commit to easily apply this locally, could we add one?

Thanks!
Pablo

Thanks for pointing out.

The base commit is 8d96f09e9245ddf80c1981476fcbac8c4bb4125f. I will put it on the cover letter in the next reroll (if any)!

Regards, yuchen

#72 lore raw

Re: [PATCH v9 0/9] migrate more variables into repo_config_values

Quoted 6 lines · Tian Yuchen
Quoted 5 lines

I missed a base-commit to easily apply this locally, could we add one?

Thanks!
Pablo

Quoted 6 lines

Thanks for pointing out.

The base commit is 8d96f09e9245ddf80c1981476fcbac8c4bb4125f. I will put it on the cover letter in the next reroll (if any)!

Regards, yuchen

FWIW, I think I queued it on top of ab776a62a7 (Git 2.55-rc2, 2026-06-22).

Others can find it out by looking at the output from

    $ git log --oneline origin/master....origin/seen

and finding the commit that merges the topic.

#73 lore raw

Re: [PATCH v9 0/9] migrate more variables into repo_config_values

In reply to Pablo Sabater
Quoted 9 lines · Pablo Sabater
Quoted 1 line · Tian Yuchen

...

Hi!

I missed a base-commit to easily apply this locally, could we add one?

Thanks!
Pablo

FYI, a topic that is in 'seen' can be extracted from my tree by inspecting "git log --oneline origin/master..origin/seen" and finding the commit that merges the series.

#74 lore raw

Re: [PATCH v9 0/9] migrate more variables into repo_config_values

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Pablo Sabater
Quoted 1 line · Tian Yuchen

...

Hi!

I missed a base-commit to easily apply this locally, could we add one?

Quoted 6 lines
Quoted 2 lines

Thanks!
Pablo

FYI, a topic that is in 'seen' can be extracted from my tree by inspecting "git log --oneline origin/master..origin/seen" and finding the commit that merges the series.

I hadn't thought of that.

Thanks, -Pablo

#75 lore raw

v10 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Changes since v9:

- Fixed a few typos.

- Drop an unnecessary branching in editor.c

- Use repo_config_values *cfg to avoid multiple calls to repo_config_values() in repo_excludes_file().

- Let repo_config_values_clear() receive 'struct repo_config_values' and do not check (repo != the_repository) anymore. In repo_clear(), pass in config_values_private_ instead so that we don't need to filter calls with uninitialized repos. Therefore, drop the NEEDSWORK comment.

Special thanks to Pablo!

Tian Yuchen (9):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values

 apply.c        | 20 ++++++++------
 branch.c       |  2 +-
 builtin/push.c |  8 +++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 75 +++++++++++++++++++++++++++++++++-----------------
 environment.h  | 75 +++++++++++++++++++++++++++++++-------------------
 object-file.c  |  2 +-
 pager.c        | 26 +++++++++++------
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 12 files changed, 140 insertions(+), 82 deletions(-)
Signature · 1 line
2.43.0
#76 lore raw

PATCH v10 1/9 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

---
 environment.c | 5 +++++
 environment.h | 9 +++++++++
 repository.c  | 1 +
 3 files changed, 15 insertions(+)
environment.c +5 -0
index ba2c60103f..ae05f16d04 100644
@@ -726,3 +726,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repo_config_values *cfg)
+{
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..9169d7f62d 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repo_config_values *cfg);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..669e2d1200 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(&repo->config_values_private_);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#77 lore raw

PATCH v10 2/9 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced 'repo_config_values_clear()' function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 17 ++++++++++++++---
 environment.h |  4 +++-
 3 files changed, 19 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +14 -3
index ae05f16d04..275931c213 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,16 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	if (!cfg->excludes_file)
+		cfg->excludes_file = xdg_config_home("ignore");
+
+	return cfg->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +470,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +724,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -730,4 +740,5 @@ void repo_config_values_init(struct repo_config_values *cfg)
 void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index 9169d7f62d..4776ccc657 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#78 lore raw

PATCH v10 3/9 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes, managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..0d1cb8768d 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 275931c213..a65d575af4 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -437,8 +436,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -725,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -741,4 +741,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 4776ccc657..8178ebab76 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#79 lore raw

PATCH v10 4/9 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix a memory leak in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 26 +++++++++++++++++---------
 3 files changed, 20 insertions(+), 9 deletions(-)
environment.c +2 -0
index a65d575af4..975c9cb9eb 100644
@@ -725,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -742,4 +743,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 8178ebab76..39b6691b47 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +17 -9
index 35b210e048..bc55546670 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,15 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}
+
 	return 0;
 }
 
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}
 	return data.want;
 }
Signature · 1 line
2.43.0
#80 lore raw

Re: [PATCH v10 4/9] environment: move pager_program into repo_config_values

Quoted 76 lines · Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix a memory leak in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 26 +++++++++++++++++---------
 3 files changed, 20 insertions(+), 9 deletions(-)
environment.c +2 -0
index a65d575af4..975c9cb9eb 100644
@@ -725,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -742,4 +743,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 8178ebab76..39b6691b47 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +2 -1
index 35b210e048..bc55546670 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */

pager.c static void wait_for_pager_signal(int signo) · line 79

Quoted 5 lines
pager.c +1 -1
@@ -75,10 +76,15 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)

Could this change behaviour that a caller expects?

(looking at the hunk below) we are now using repo_config_values() which contains the condition 'repo != the_repository'. This means that if there is a caller that sends anything but the_repository, it will BUG() out.

Before this patch it would have worked, it worked because callers were sending the correct repository. Now we enforce it.

If we check the callers we can see that everyone sends the_repository, so this new assert is fine and prevents sending submodules by mistake.

Makes sense.

I think it's worth mentioning that on the commit body/function.

pager.c static void wait_for_pager_signal(int signo) · line 86

Quoted 9 lines
pager.c +6 -2
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}

Ok. Now that pager_program is not file-scoped we drop the UNUSED and pager_program now lives in the per-repo field.
Then we change the address where ->pager_program (which we access through repo_config_values()) points to.

FREE_AND_NULL() is new, before this patch it must have been leaking, good job.

Similar to previous patches, let's change the pattern to only call repo_config_values() once and use the pointer it returns.

Quoted 5 lines
pager.c +1 -0
+
 	return 0;
 }
 

pager.c const char *git_pager(struct repository *r, int stdout_is_tty) · line 103

Quoted 10 lines
pager.c +3 -3
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;

Same as above, let's call repo_config_values() once.

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 313

Quoted 12 lines
pager.c +3 -2
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;

Same pattern. This also frees, but the log says "a" memory leak is fixed in this patch, should we change it to two?

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 316

Quoted 3 lines
pager.c +1 -0
+	}
 	return data.want;
 }

I peeked at later patches of this series and the multiple calls of repo_config_values() pattern keeps appearing.
I haven't finished reviewing 5-9. If you reroll before I get to those patches, it may be worth fixing this pattern across the whole series.

Regards,
Pablo

#81 lore raw

Re: [PATCH v10 4/9] environment: move pager_program into repo_config_values

Quoted 89 lines · Pablo Sabater
Quoted 76 lines · Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix a memory leak in pager.c while we are at it.

---
  environment.c |  2 ++
  environment.h |  1 +
  pager.c       | 26 +++++++++++++++++---------
  3 files changed, 20 insertions(+), 9 deletions(-)
environment.c +2 -0
index a65d575af4..975c9cb9eb 100644
@@ -725,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
  	cfg->attributes_file = NULL;
  	cfg->excludes_file = NULL;
  	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
  	cfg->apply_sparse_checkout = 0;
  	cfg->branch_track = BRANCH_TRACK_REMOTE;
  	cfg->trust_ctime = 1;
@@ -742,4 +743,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
  	FREE_AND_NULL(cfg->attributes_file);
  	FREE_AND_NULL(cfg->excludes_file);
  	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
  }
environment.h +1 -0
index 8178ebab76..39b6691b47 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
  	char *attributes_file;
  	char *excludes_file;
  	char *editor_program;
+	char *pager_program;
  	int apply_sparse_checkout;
  	int trust_ctime;
  	int check_stat;
pager.c +2 -1
index 35b210e048..bc55546670 100644
@@ -5,6 +5,8 @@
  #include "run-command.h"
  #include "sigchain.h"
  #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
  int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
  #endif
 
  static struct child_process pager_process;
-static char *pager_program;
  static int old_fd1 = -1, old_fd2 = -1;
 
  /* Is the value coming back from term_columns() just a guess? */

pager.c static void wait_for_pager_signal(int signo) · line 79

Quoted 5 lines
pager.c +1 -1
@@ -75,10 +76,15 @@ static void wait_for_pager_signal(int signo)
 
  static int core_pager_config(const char *var, const char *value,
  			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)

Could this change behaviour that a caller expects?

(looking at the hunk below) we are now using repo_config_values() which contains the condition 'repo != the_repository'. This means that if there is a caller that sends anything but the_repository, it will BUG() out.

Quoted 8 lines

Before this patch it would have worked, it worked because callers were sending the correct repository. Now we enforce it.

If we check the callers we can see that everyone sends the_repository, so this new assert is fine and prevents sending submodules by mistake.

Makes sense.

;)

Quoted 2 lines

I think it's worth mentioning that on the commit body/function.

Okay, will add a line to briefly explain this. Something like what Junio said:

All current callers of git_pager() and check_pager_config() indeed pass 'the_repository', so this new enforcement does not harm them.

Quoted 46 lines

pager.c static void wait_for_pager_signal(int signo) · line 86

Quoted 9 lines
pager.c +6 -2
  {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}

Ok. Now that pager_program is not file-scoped we drop the UNUSED and pager_program now lives in the per-repo field.
Then we change the address where ->pager_program (which we access through repo_config_values()) points to.

FREE_AND_NULL() is new, before this patch it must have been leaking, good job.

Similar to previous patches, let's change the pattern to only call repo_config_values() once and use the pointer it returns.

Quoted 5 lines
pager.c +1 -0
+
  	return 0;
  }
 

pager.c const char *git_pager(struct repository *r, int stdout_is_tty) · line 103

Quoted 10 lines
pager.c +3 -3
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
  	pager = getenv("GIT_PAGER");
  	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
  			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;

Same as above, let's call repo_config_values() once.

Quoted 7 lines
pager.c +0 -0
  	}
  	if (!pager)
  		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
  	read_early_config(r, pager_command_config, &data);
 
Quoted 8 lines

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 313

Quoted 5 lines
pager.c +3 -2
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;

Same pattern. This also frees, but the log says "a" memory leak is fixed in this patch, should we change it to two?

Nice catch.

Quoted 10 lines

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 316

Quoted 4 lines
pager.c +1 -0
+	}
  	return data.want;
  }

I peeked at later patches of this series and the multiple calls of repo_config_values() pattern keeps appearing.
I haven't finished reviewing 5-9. If you reroll before I get to those patches, it may be worth fixing this pattern across the whole series.

Quoted 2 lines

Regards,
Pablo

For those cases where multiple calls to repo_config_values() appear in a single function body, I will fix such pattern by using cfg.

Thanks, yuchen

#82 lore raw

Re: [PATCH v10 4/9] environment: move pager_program into repo_config_values

In reply to Tian Yuchen
Quoted 7 lines · Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

By redirecting to repo_config_values(r), we now enforce that the passed repository must be 'the_repository' (due to the assertion in repo_config_values()). All current callers of git_pager() and check_pager_config() indeed pass 'the_repository', so this new enforcement does not harm them. However, it paves the way to later lift the assertion and allow us to configure different pagers for different repositories, which is a welcome improvement.

pager.c static void wait_for_pager_signal(int signo) · line 86

Quoted 13 lines

static int core_pager_config(const char *var, const char *value,

pager.c +7 -3
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}

It may be just me, but I would have preferred to see this written more like

	if (!strcmp(var, "core.pager")) {
		struct repo_config_values *values = repo_config_values(r);

		FREE_AND_NULL(values->pager_program);
		return git_config_string(&values->pager_program, var, value);
	}

which will make it easier to see that we are freeing the same thing immediately before we overwrite it. It also shortens the lines. For a temporary variable with a very short scope like this one that is introduced solely for readability, it is OK to use even shorter name like 'v' if you want to ('r' certainly has a much longer lifespan that it, and I would probably have preferred to see it called 'repo').

Side note: we might want to give a hint in the coding guidelines document that a variable with larger lifespan should get longer names, or something.

Quoted 5 lines
pager.c +0 -0
 	return 0;
 }
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 

pager.c const char *git_pager(struct repository *r, int stdout_is_tty) · line 104

Quoted 10 lines
pager.c +3 -3
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
 	}

Same here.

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 314

Quoted 12 lines
pager.c +4 -2
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}

Same here.

#83 lore raw

Re: [PATCH v10 4/9] environment: move pager_program into repo_config_values

Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'.

Quoted 8 lines

By redirecting to repo_config_values(r), we now enforce that the passed repository must be 'the_repository' (due to the assertion in repo_config_values()). All current callers of git_pager() and check_pager_config() indeed pass 'the_repository', so this new enforcement does not harm them. However, it paves the way to later lift the assertion and allow us to configure different pagers for different repositories, which is a welcome improvement.

Exactly.

Quoted 18 lines

pager.c static void wait_for_pager_signal(int signo) · line 86

Quoted 13 lines

static int core_pager_config(const char *var, const char *value,
const struct config_context *ctx UNUSED,

pager.c +7 -3
-			     void *data UNUSED)
+			     void *data)
  {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		FREE_AND_NULL(repo_config_values(r)->pager_program);
+		return git_config_string(&repo_config_values(r)->pager_program, var, value);
+	}

It may be just me, but I would have preferred to see this written more like

Quoted 7 lines
	if (!strcmp(var, "core.pager")) {
		struct repo_config_values *values = repo_config_values(r);

		FREE_AND_NULL(values->pager_program);
		return git_config_string(&values->pager_program, var, value);
	}

I see. I think it's better to name the struct 'cfg' so that it is consistent with what we did before.

Quoted 36 lines

which will make it easier to see that we are freeing the same thing immediately before we overwrite it. It also shortens the lines. For a temporary variable with a very short scope like this one that is introduced solely for readability, it is OK to use even shorter name like 'v' if you want to ('r' certainly has a much longer lifespan that it, and I would probably have preferred to see it called 'repo').

Side note: we might want to give a hint in the coding guidelines document that a variable with larger lifespan should get longer names, or something.

Quoted 5 lines
  	return 0;
  }
pager.c +0 -0
@@ -91,10 +97,10 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 

pager.c const char *git_pager(struct repository *r, int stdout_is_tty) · line 104

Quoted 10 lines
pager.c +3 -3
  	pager = getenv("GIT_PAGER");
  	if (!pager) {
-		if (!pager_program)
+		if (!repo_config_values(r)->pager_program)
  			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = repo_config_values(r)->pager_program;
  	}

Same here.

Quoted 6 lines
pager.c +0 -0
  	if (!pager)
  		pager = getenv("PAGER");
@@ -302,7 +308,9 @@ int check_pager_config(struct repository *r, const char *cmd)
 
  	read_early_config(r, pager_command_config, &data);
 
Quoted 8 lines

pager.c int check_pager_config(struct repository *r, const char *cmd) · line 314

Quoted 6 lines
pager.c +4 -2
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		free(repo_config_values(r)->pager_program);
+		repo_config_values(r)->pager_program = data.value;
+	}

Same here.

Thanks, will change all these parts.

Regard, yuchen

#84 lore raw

PATCH v10 5/9 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +4 -2
index 975c9cb9eb..1a26c9c6d6 100644
@@ -464,8 +464,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -726,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +745,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -0
index 39b6691b47..a2e9def89d 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#85 lore raw

Re: [PATCH v10 5/9] environment: move askpass_program into repo_config_values

environment.c

Quoted 27 lines · Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +0 -0
index 975c9cb9eb..1a26c9c6d6 100644

I think that the drop of the global variable is missing.

Quoted 50 lines
environment.c +4 -2
@@ -464,8 +464,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -726,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +745,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -0
index 39b6691b47..a2e9def89d 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
prompt.c +1 -0
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"

prompt.c char *git_prompt(const char *prompt, int flags) · line 58

Quoted 8 lines
prompt.c +1 -1
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)

The rest LGTM.

Regards,
Pablo

#86 lore raw

Re: [PATCH v10 5/9] environment: move askpass_program into repo_config_values

In reply to Tian Yuchen
Quoted 27 lines · Tian Yuchen
 environment.c | 6 ++++--
 environment.h | 1 +
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)
environment.c +3 -2
index 975c9cb9eb..1a26c9c6d6 100644
@@ -464,8 +464,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
@@ -726,6 +726,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;

environment.c void repo_config_values_clear(struct repo_config_values *cfg) · line 749

Quoted 5 lines
environment.c +1 -0
@@ -744,4 +745,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }

The 'askpass_program' global variable has been removed. Instead, the member in repo_config_values is correctly initialized to NULL and properly cleaned up when finished.

environment.h struct repo_config_values { · line 99

Quoted 12 lines
environment.h +1 -0
index 39b6691b47..a2e9def89d 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;

Wait, wasn't there an extern declaration for that global variable in a header file somewhere? There must also be an actual definition for it. Both should be removed to ensure no one accesses a stale variable; doing so allows the compiler to help you spot any leftover users.

Thanks.

#87 lore raw

PATCH v10 6/9 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 20 ++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 18 insertions(+), 12 deletions(-)
apply.c +12 -8
index 249248d4f2..66db9b7678 100644
@@ -47,11 +47,13 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &repo_config_values(repo)->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &repo_config_values(repo)->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +128,12 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+	if (repo_config_values(repo)->apply_default_whitespace &&
+	    parse_whitespace_option(state, repo_config_values(repo)->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (repo_config_values(repo)->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, repo_config_values(repo)->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +196,7 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 1a26c9c6d6..41ba013c86 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -727,6 +725,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -746,4 +746,6 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index a2e9def89d..553f87adee 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#88 lore raw

Re: [PATCH v10 6/9] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Quoted 32 lines · Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 20 ++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 18 insertions(+), 12 deletions(-)
apply.c +0 -0
index 249248d4f2..66db9b7678 100644
@@ -47,11 +47,13 @@ struct gitdiff_data {
 	int p_value;
 };
 

apply.c struct gitdiff_data { · line 55

Quoted 10 lines
apply.c +5 -4
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &repo_config_values(repo)->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &repo_config_values(repo)->apply_default_ignorewhitespace);

Same pattern, let's call repo_config_values() once. Also, similar to the previous patches, shouldn't be here a FREE_AND_NULL() before each repo_config_get_string() call?

Quoted 21 lines
apply.c +6 -3
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +128,12 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+	if (repo_config_values(repo)->apply_default_whitespace &&
+	    parse_whitespace_option(state, repo_config_values(repo)->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (repo_config_values(repo)->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, repo_config_values(repo)->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }

apply.c int check_apply_state(struct apply_state *state, int force_apply) · line 199

Quoted 5 lines
apply.c +1 -1
@@ -192,7 +196,7 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !repo_config_values(state->repo)->apply_default_whitespace)

We should extract cfg from repo_config_values() to avoid this overly long line.

Quoted 46 lines
apply.c +0 -0
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 1a26c9c6d6..41ba013c86 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -727,6 +725,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -746,4 +746,6 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -0
index a2e9def89d..553f87adee 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;

environment.h extern int has_symlinks; · line 189

Quoted 8 lines
environment.h +0 -2
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;

The rest LGTM.

Regards,
Pablo

#89 lore raw

PATCH v10 7/9 environment: move push_default into repo_config_values

In reply to Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c |  8 ++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 30 insertions(+), 22 deletions(-)
builtin/push.c +4 -4
index 6021b71d66..6dc3224b60 100644
@@ -88,7 +88,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (repo_config_values(the_repository)->push_default == PUSH_DEFAULT_UPSTREAM &&
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +160,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -232,7 +232,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	const char *dst;
 	int same_remote;
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +252,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 41ba013c86..0080012f31 100644
@@ -59,7 +59,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -621,21 +620,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -727,6 +728,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index 553f87adee..6a5c8bd06f 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +1 -1
index 00723b385e..d48c01d375 100644
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 
Signature · 1 line
2.43.0
#90 lore raw

Re: [PATCH v10 7/9] environment: move push_default into repo_config_values

Quoted 32 lines · Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c |  8 ++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 30 insertions(+), 22 deletions(-)
builtin/push.c +0 -0
index 6021b71d66..6dc3224b60 100644

builtin/push.c static void refspec_append_mapped(struct refspec *refspec, const char *ref, · line 91

Quoted 5 lines
builtin/push.c +1 -1
@@ -88,7 +88,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (repo_config_values(the_repository)->push_default == PUSH_DEFAULT_UPSTREAM &&

Can we extract cfg from repo_config_values() to shorten this line? If we look at the hunk below, we can see this pattern. let's do the same.

Quoted 136 lines
builtin/push.c +3 -3
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +160,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -232,7 +232,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	const char *dst;
 	int same_remote;
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +252,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (repo_config_values(the_repository)->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 41ba013c86..0080012f31 100644
@@ -59,7 +59,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -621,21 +620,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -727,6 +728,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index 553f87adee..6a5c8bd06f 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +0 -0
index 00723b385e..d48c01d375 100644

remote.c static char *branch_get_push_1(struct repository *repo, · line 1939

Quoted 8 lines
remote.c +1 -1
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 

I haven't checked how doable fixing the dependency cycle of the NEEDSWORK is.

Similar to other patches where we add repo_config_values() to functions that didn't have it before. We need to check that there's no caller that could pass a repository different from the_repository. I haven't checked it this time.

Regards,
Pablo

#91 lore raw

PATCH v10 8/9 environment: move autorebase into repo_config_values

In reply to Tian Yuchen

The global variable 'autorebase' dictates whether a newly created branch should be configured to automatically rebase by default. Move it into 'struct repo_config_values' to continue the libification effort.

The 'enum rebase_setup_type' definition is moved higher up in 'environment.h' so that it is visible to the repository-specific structure. The default state AUTOREBASE_NEVER is now correctly initialized in 'repo_config_values_init()'.

Configuration parsing in 'git_default_branch_config()' is updated to write directly to the repository's configuration instance.

---
 branch.c      |  2 +-
 environment.c | 10 +++++-----
 environment.h | 16 ++++++++--------
 3 files changed, 14 insertions(+), 14 deletions(-)
branch.c +1 -1
index 243db7d0fc..e1c1f8c89d 100644
@@ -61,7 +61,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
 static int should_setup_rebase(const char *origin)
 {
-	switch (autorebase) {
+	switch (repo_config_values(the_repository)->autorebase) {
 	case AUTOREBASE_NEVER:
 		return 0;
 	case AUTOREBASE_LOCAL:
environment.c +5 -5
index 0080012f31..42829a9c7a 100644
@@ -58,7 +58,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
-enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -602,13 +601,13 @@ static int git_default_branch_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "never"))
-			autorebase = AUTOREBASE_NEVER;
+			cfg->autorebase = AUTOREBASE_NEVER;
 		else if (!strcmp(value, "local"))
-			autorebase = AUTOREBASE_LOCAL;
+			cfg->autorebase = AUTOREBASE_LOCAL;
 		else if (!strcmp(value, "remote"))
-			autorebase = AUTOREBASE_REMOTE;
+			cfg->autorebase = AUTOREBASE_REMOTE;
 		else if (!strcmp(value, "always"))
-			autorebase = AUTOREBASE_ALWAYS;
+			cfg->autorebase = AUTOREBASE_ALWAYS;
 		else
 			return error(_("malformed value for %s"), var);
 		return 0;
@@ -729,6 +728,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
+	cfg->autorebase = AUTOREBASE_NEVER;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +8 -8
index 6a5c8bd06f..deabc5ef30 100644
@@ -102,6 +102,13 @@ enum push_default_type {
 	PUSH_DEFAULT_UNSPECIFIED
 };
 
+enum rebase_setup_type {
+	AUTOREBASE_NEVER = 0,
+	AUTOREBASE_LOCAL,
+	AUTOREBASE_REMOTE,
+	AUTOREBASE_ALWAYS
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -112,6 +119,7 @@ struct repo_config_values {
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
+	enum rebase_setup_type autorebase;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -205,14 +213,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum rebase_setup_type {
-	AUTOREBASE_NEVER = 0,
-	AUTOREBASE_LOCAL,
-	AUTOREBASE_REMOTE,
-	AUTOREBASE_ALWAYS
-};
-extern enum rebase_setup_type autorebase;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
Signature · 1 line
2.43.0
#92 lore raw

PATCH v10 9/9 environment: move object_creation_mode into repo_config_values

In reply to Tian Yuchen

The global variable 'object_creation_mode' controls how Git creates object files, specifically determining whether to use hardlinks or renames when moving temporary files into the object database. Move it into 'struct repo_config_values' to continue the libification effort.

Move the 'enum object_creation_mode' definition higher up in 'environment.h' to ensure it is visible to the structure. Initialize the per-repository value to its default macro value OBJECT_CREATION_MODE inside 'repo_config_values_init()'.

Update configuration parsing in 'git_default_core_config()' to write directly to the repository-specific configuration structure.

---
 environment.c |  6 +++---
 environment.h | 12 ++++++------
 object-file.c |  2 +-
 3 files changed, 10 insertions(+), 10 deletions(-)
environment.c +3 -3
index 42829a9c7a..e882e4ada2 100644
@@ -61,7 +61,6 @@ char *check_roundtrip_encoding;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
-enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
 unsigned long pack_size_limit_cfg;
 
@@ -513,9 +512,9 @@ int git_default_core_config(const char *var, const char *value,
 		if (!value)
 			return config_error_nonbool(var);
 		if (!strcmp(value, "rename"))
-			object_creation_mode = OBJECT_CREATION_USES_RENAMES;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_RENAMES;
 		else if (!strcmp(value, "link"))
-			object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
 		else
 			die(_("invalid mode for object creation: %s"), value);
 		return 0;
@@ -729,6 +728,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->autorebase = AUTOREBASE_NEVER;
+	cfg->object_creation_mode = OBJECT_CREATION_MODE;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +6 -6
index deabc5ef30..de6e80cce2 100644
@@ -109,6 +109,11 @@ enum rebase_setup_type {
 	AUTOREBASE_ALWAYS
 };
 
+enum object_creation_mode {
+	OBJECT_CREATION_USES_HARDLINKS = 0,
+	OBJECT_CREATION_USES_RENAMES = 1
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -120,6 +125,7 @@ struct repo_config_values {
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
 	enum rebase_setup_type autorebase;
+	enum object_creation_mode object_creation_mode;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -213,12 +219,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum object_creation_mode {
-	OBJECT_CREATION_USES_HARDLINKS = 0,
-	OBJECT_CREATION_USES_RENAMES = 1
-};
-extern enum object_creation_mode object_creation_mode;
-
 extern int grafts_keep_true_parents;
 
 const char *get_log_output_encoding(void);
object-file.c +1 -1
index 9afa842da2..cbbfc8f1dc 100644
@@ -415,7 +415,7 @@ int finalize_object_file_flags(struct repository *repo,
 retry:
 	ret = 0;
 
-	if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
+	if (repo_config_values(repo)->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
 		goto try_rename;
 	else if (link(tmpfile, filename))
 		ret = errno;
Signature · 1 line
2.43.0
#93 lore raw

v11 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

edit comment (commit 10):
Adjust the comment for config_values_private_ in repository.h.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Changes since v10:

- use repo_config_values *cfg to avoid multiple calls to repo_config_values() and avoid overly long lines.

- drop the extern declarations for askpass_program.

- in the commit message of pager_program migration, mention that the new assertion is fine since current callers pass the_repository only.

- add FREE_AND_NULL()s before repo_config_get_strings() calls.

- create a new commit to adjust the comment for config_values_private_ since it was no longer true.

Special thanks to Pablo and Junio!

Tian Yuchen (10):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values
repository: adjust the comment of config_values_private_

 apply.c        | 28 ++++++++++++------
 branch.c       |  2 +-
 builtin/push.c | 10 ++++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 76 ++++++++++++++++++++++++++++++++-----------------
 environment.h  | 77 ++++++++++++++++++++++++++++++--------------------
 object-file.c  |  3 +-
 pager.c        | 32 +++++++++++++++------
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 repository.h   |  2 +-
 13 files changed, 158 insertions(+), 86 deletions(-)
Signature · 1 line
2.43.0
#94 lore raw

PATCH v11 1/10 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

---
 environment.c | 5 +++++
 environment.h | 9 +++++++++
 repository.c  | 1 +
 3 files changed, 15 insertions(+)
environment.c +5 -0
index ba2c60103f..ae05f16d04 100644
@@ -726,3 +726,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repo_config_values *cfg)
+{
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..9169d7f62d 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repo_config_values *cfg);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..669e2d1200 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(&repo->config_values_private_);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#95 lore raw

PATCH v11 2/10 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced 'repo_config_values_clear()' function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 17 ++++++++++++++---
 environment.h |  4 +++-
 3 files changed, 19 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +14 -3
index ae05f16d04..275931c213 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,16 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	if (!cfg->excludes_file)
+		cfg->excludes_file = xdg_config_home("ignore");
+
+	return cfg->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +470,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +724,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -730,4 +740,5 @@ void repo_config_values_init(struct repo_config_values *cfg)
 void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index 9169d7f62d..4776ccc657 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#96 lore raw

PATCH v11 3/10 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes, managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..0d1cb8768d 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 275931c213..a65d575af4 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -437,8 +436,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -725,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -741,4 +741,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 4776ccc657..8178ebab76 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#97 lore raw

PATCH v11 4/10 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'. All current callers indeed pass 'the_repository', so this new enforcement does not harm them.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix memory leaks in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 32 +++++++++++++++++++++++---------
 3 files changed, 26 insertions(+), 9 deletions(-)
environment.c +2 -0
index a65d575af4..975c9cb9eb 100644
@@ -725,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -742,4 +743,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 8178ebab76..39b6691b47 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +23 -9
index 35b210e048..543ef12936 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,17 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		FREE_AND_NULL(cfg->pager_program);
+		return git_config_string(&cfg->pager_program, var, value);
+	}
+
 	return 0;
 }
 
@@ -91,10 +99,12 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		if (!cfg->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = cfg->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +312,11 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		free(cfg->pager_program);
+		cfg->pager_program = data.value;
+	}
 	return data.want;
 }
Signature · 1 line
2.43.0
#98 lore raw

PATCH v11 5/10 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 7 ++++---
 environment.h | 3 +--
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 6 deletions(-)
environment.c +4 -3
index 975c9cb9eb..3857818da3 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -464,8 +463,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -726,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +744,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -2
index 39b6691b47..856dc70cc4 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -220,8 +221,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *askpass_program;
-
 /*
  * The character that begins a commented line in user-editable file
  * that is subject to stripspace.
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#99 lore raw

PATCH v11 6/10 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 28 ++++++++++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 26 insertions(+), 12 deletions(-)
apply.c +20 -8
index 249248d4f2..f0cfd76190 100644
@@ -47,11 +47,17 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &cfg->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -126,10 +132,15 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	if (cfg->apply_default_whitespace &&
+	    parse_whitespace_option(state, cfg->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (cfg->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, cfg->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +203,8 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option &&
+	    !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 3857818da3..20500658a2 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -726,6 +724,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -745,4 +745,6 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index 856dc70cc4..f450242ac0 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#100 lore raw

Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Quoted 1 line · Tian Yuchen

Subject: Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Are there patches 7..10/10 posted somewhere else? I didn't see them in the thread (neither did "b4").

Quoted 7 lines
apply.c +1 -4
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);

apply.c struct gitdiff_data { · line 61

Quoted 10 lines
apply.c +9 -0
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &cfg->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }

OK.

apply.c int init_apply_state(struct apply_state *state, · line 137

Quoted 10 lines

static int parse_whitespace_option(struct apply_state *state, const char *option)

apply.c +3 -2
@@ -126,10 +132,15 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+
+	struct repo_config_values *cfg = repo_config_values(repo);

Doesn't "-Wdeclaration-after-statement" complain on this, declaring cfg after calling "git_apply_config(repo)" on the line before?

#101 lore raw

Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Quoted 7 lines · Junio C Hamano
Quoted 1 line · Tian Yuchen

Subject: Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Are there patches 7..10/10 posted somewhere else? I didn't see them in the thread (neither did "b4").

Oh, I didn't notice that:

Died at /usr/lib/git-core/git-send-email line 1665.

Will resend very soon.

Quoted 25 lines
Quoted 7 lines
apply.c +1 -4
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
  {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);

apply.c struct gitdiff_data { · line 61

Quoted 10 lines
apply.c +9 -0
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &cfg->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
  }

OK.

Quoted 5 lines

static int parse_whitespace_option(struct apply_state *state, const char *option)

apply.c +0 -0
@@ -126,10 +132,15 @@ int init_apply_state(struct apply_state *state,
  	strset_init(&state->kept_symlinks);
  	strbuf_init(&state->root, 0);
 
Quoted 9 lines

apply.c int init_apply_state(struct apply_state *state, · line 137

Quoted 5 lines
apply.c +3 -2
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+
+	struct repo_config_values *cfg = repo_config_values(repo);

Doesn't "-Wdeclaration-after-statement" complain on this, declaring cfg after calling "git_apply_config(repo)" on the line before?

Nice catch, thanks!

Regards, yuchen

#102 lore raw

Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Quoted 9 lines · Tian Yuchen
Quoted 7 lines · Junio C Hamano
Quoted 1 line · Tian Yuchen

Subject: Re: [PATCH v11 06/10] environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

Are there patches 7..10/10 posted somewhere else? I didn't see them in the thread (neither did "b4").

Quoted 5 lines

Oh, I didn't notice that:

Died at /usr/lib/git-core/git-send-email line 1665.

Will resend very soon.

Thanks.

#103 lore raw

v12 cover migrate more variables into repo_config_values

In reply to Tian Yuchen

Hi everyone,

This patch series continues the ongoing libification effort by migrating a batch of global configuration variables into struct repo_config_values.

What does this series do:

infrastructure & strings (commits 1-6):
Introduce 'repo_config_values_clear()' to manage the lifecycle of heap-allocated configuration strings. This infrastructure is utilized to migrate string variables, including 'excludes_file', 'apply' whitespace configs, and external programs including 'editor', 'pager', 'askpass'.

enums (commits 7-9):
Migrate enumerations 'push_default', 'autorebase', and 'object_creation_mode'. Care was taken to make these types available to the configuration structure without triggering circular header dependencies.

edit comment (commit 10):
Adjust the comment for config_values_private_ in repository.h.

RFC:

Commit 3~5. Is it really necessary to migrate _program variables?
https://lore.kernel.org/git/[email protected]/

Commit 6~9. Previous related discussions on 'git_branch_track'.
https://lore.kernel.org/git/CAD=f0L-mPX+KECUjXk-WBzEbTP7wCa8sB56GySQT0yh9mfUOWw@mail.gmail.com/

Note:

Since a new getter 'repo_excludes_file()' is introduced, as previously promised, once it is finally merged into 'master', there will be a patch to update and squash the comments.

Similarly, I've noticed that the classification and sorting of variables in 'repo_config_values' don't seem to be correct. There will also be a patch to fix this, and I think it will form a commit series along with the comment patch?

Changes since v11:

- Resending commit 7~10/10, which were not sent in V11 due to network issue.

- In commit 6/10, fix a declaration-after-statement error in apply.c

Special thanks to Pablo and Junio!

Tian Yuchen (10):
repository: introduce repo_config_values_clear()
environment: move excludes_file into repo_config_values
environment: move editor_program into repo_config_values
environment: move pager_program into repo_config_values
environment: move askpass_program into repo_config_values
environment: migrate apply_default_whitespace and
apply_default_ignorewhitespace
environment: move push_default into repo_config_values
environment: move autorebase into repo_config_values
environment: move object_creation_mode into repo_config_values
repository: adjust the comment of config_values_private_

 apply.c        | 28 ++++++++++++------
 branch.c       |  2 +-
 builtin/push.c | 10 ++++---
 dir.c          |  4 +--
 editor.c       |  4 +--
 environment.c  | 76 ++++++++++++++++++++++++++++++++-----------------
 environment.h  | 77 ++++++++++++++++++++++++++++++--------------------
 object-file.c  |  3 +-
 pager.c        | 32 +++++++++++++++------
 prompt.c       |  3 +-
 remote.c       |  2 +-
 repository.c   |  1 +
 repository.h   |  2 +-
 13 files changed, 158 insertions(+), 86 deletions(-)
Signature · 1 line
2.43.0
#104 lore raw

PATCH v12 1/10 repository: introduce repo_config_values_clear()

As part of the ongoing libification effort, dynamically allocated global configuration variables are being moved into 'struct repo_config_values'. To prevent memory leaks, we need a destructor to free these heap-allocated variables when a repository instance is torn down.

Introduce 'repo_config_values_clear()' in environment.c and invoke it from 'repo_clear()' in repository.c. As a starting point, update this new function to handle the cleanup of 'attributes_file'.

---
 environment.c | 5 +++++
 environment.h | 9 +++++++++
 repository.c  | 1 +
 3 files changed, 15 insertions(+)
environment.c +5 -0
index ba2c60103f..ae05f16d04 100644
@@ -726,3 +726,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->sparse_expect_files_outside_of_patterns = 0;
 	cfg->warn_on_object_refname_ambiguity = 1;
 }
+
+void repo_config_values_clear(struct repo_config_values *cfg)
+{
+	FREE_AND_NULL(cfg->attributes_file);
+}
environment.h +9 -0
index 6f18286955..9169d7f62d 100644
@@ -135,6 +135,15 @@ int git_default_core_config(const char *var, const char *value,
 
 void repo_config_values_init(struct repo_config_values *cfg);
 
+/*
+ * Frees memory allocated for dynamically loaded configuration values
+ * inside `repo_config_values`.
+ *
+ * As dynamically allocated variables are migrated into this struct,
+ * their FREE_AND_NULL() calls should be appended here.
+ */
+void repo_config_values_clear(struct repo_config_values *cfg);
+
 /*
  * TODO: All the below state either explicitly or implicitly relies on
  * `the_repository`. We should eventually get rid of these and make the
repository.c +1 -0
index 187dd471c4..669e2d1200 100644
@@ -388,6 +388,7 @@ void repo_clear(struct repository *repo)
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
+	repo_config_values_clear(&repo->config_values_private_);
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
Signature · 1 line
2.43.0
#105 lore raw

PATCH v12 2/10 environment: move excludes_file into repo_config_values

In reply to Tian Yuchen

The global variable 'excludes_file' is used to track the path to the global ignore file. If this variable is NULL, 'setup_standard_excludes()' in 'dir.c' forcefully evaluates and assigns the XDG default path to it.

Continue the libification effort by encapsulating this lazy-loading fallback logic into a proper getter and moving the variable into 'struct repo_config_values'.

Since 'excludes_file' is a dynamically allocated string, it requires proper heap memory management. It is safely freed using the newly introduced 'repo_config_values_clear()' function when the repository is torn down.

---
 dir.c         |  4 ++--
 environment.c | 17 ++++++++++++++---
 environment.h |  4 +++-
 3 files changed, 19 insertions(+), 6 deletions(-)
dir.c +2 -2
index 7a73690fbc..4f87a52b3c 100644
@@ -3481,11 +3481,11 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
 
 void setup_standard_excludes(struct dir_struct *dir)
 {
+	const char *excludes_file = repo_excludes_file(the_repository);
+
 	dir->exclude_per_dir = ".gitignore";
 
 	/* core.excludesfile defaulting to $XDG_CONFIG_HOME/git/ignore */
-	if (!excludes_file)
-		excludes_file = xdg_config_home("ignore");
 	if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
 		add_patterns_from_file_1(dir, excludes_file,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
environment.c +14 -3
index ae05f16d04..275931c213 100644
@@ -57,7 +57,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
 char *editor_program;
 char *askpass_program;
-char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -134,6 +133,16 @@ int is_bare_repository(void)
 	return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
 }
 
+const char *repo_excludes_file(struct repository *repo)
+{
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	if (!cfg->excludes_file)
+		cfg->excludes_file = xdg_config_home("ignore");
+
+	return cfg->excludes_file;
+}
+
 int have_git_dir(void)
 {
 	return startup_info->have_repository
@@ -461,8 +470,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
-		FREE_AND_NULL(excludes_file);
-		return git_config_pathname(&excludes_file, var, value);
+		FREE_AND_NULL(cfg->excludes_file);
+		return git_config_pathname(&cfg->excludes_file, var, value);
 	}
 
 	if (!strcmp(var, "core.whitespace")) {
@@ -715,6 +724,7 @@ int git_default_config(const char *var, const char *value,
 void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
+	cfg->excludes_file = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -730,4 +740,5 @@ void repo_config_values_init(struct repo_config_values *cfg)
 void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
+	FREE_AND_NULL(cfg->excludes_file);
 }
environment.h +3 -1
index 9169d7f62d..4776ccc657 100644
@@ -90,6 +90,7 @@ struct repository;
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
+	char *excludes_file;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -133,6 +134,8 @@ int git_default_config(const char *, const char *,
 int git_default_core_config(const char *var, const char *value,
 			    const struct config_context *ctx, void *cb);
 
+const char *repo_excludes_file(struct repository *repo);
+
 void repo_config_values_init(struct repo_config_values *cfg);
 
 /*
@@ -217,7 +220,6 @@ extern char *git_log_output_encoding;
 
 extern char *editor_program;
 extern char *askpass_program;
-extern char *excludes_file;
 
 /*
  * The character that begins a commented line in user-editable file
Signature · 1 line
2.43.0
#106 lore raw

PATCH v12 3/10 environment: move editor_program into repo_config_values

In reply to Tian Yuchen

The global variable 'editor_program' holds the path to the user's preferred editor. Move 'editor_program' into
'struct repo_config_values' to continue the libification effort.

There have been discussions on whether external programs like editors truly need to be configured on a per-repository basis within the same process. While a single process might rarely invoke different editors, this migration is necessary for two reasons:

1. Developers frequently use different toolchains for different projects. Per-repo configuration respects this.

2. Moving this string into 'repo_config_values' eliminates mutable global state. As the codebase moves toward becoming a long-running processes, managing multiple repositories concurrently must not overwrite each other's program configurations.

No standalone getter function is introduced. Callers directly access the field via 'repo_config_values()'. Heap memory is safely reclaimed in 'repo_config_values_clear()'.

---
 editor.c      | 4 ++--
 environment.c | 7 ++++---
 environment.h | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
editor.c +2 -2
index fd174e6a03..0d1cb8768d 100644
@@ -29,8 +29,8 @@ const char *git_editor(void)
 	const char *editor = getenv("GIT_EDITOR");
 	int terminal_is_dumb = is_terminal_dumb();
 
-	if (!editor && editor_program)
-		editor = editor_program;
+	if (!editor)
+		editor = repo_config_values(the_repository)->editor_program;
 	if (!editor && !terminal_is_dumb)
 		editor = getenv("VISUAL");
 	if (!editor)
environment.c +4 -3
index 275931c213..a65d575af4 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *editor_program;
 char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
@@ -437,8 +436,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.editor")) {
-		FREE_AND_NULL(editor_program);
-		return git_config_string(&editor_program, var, value);
+		FREE_AND_NULL(cfg->editor_program);
+		return git_config_string(&cfg->editor_program, var, value);
 	}
 
 	if (!strcmp(var, "core.commentchar") ||
@@ -725,6 +724,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 {
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
+	cfg->editor_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -741,4 +741,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 {
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
+	FREE_AND_NULL(cfg->editor_program);
 }
environment.h +1 -1
index 4776ccc657..8178ebab76 100644
@@ -91,6 +91,7 @@ struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
 	char *excludes_file;
+	char *editor_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -218,7 +219,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *editor_program;
 extern char *askpass_program;
 
 /*
Signature · 1 line
2.43.0
#107 lore raw

PATCH v12 4/10 environment: move pager_program into repo_config_values

In reply to Tian Yuchen

The 'pager_program' variable is currently defined as a file-scoped static string in pager.c. Move it into 'struct repo_config_values'.

The configuration parsing logic remains strictly within pager.c to respect subsystem boundaries. The read/write operations are simply redirected to the repository-specific structure using 'repo_config_values()'. All current callers indeed pass 'the_repository', so this new enforcement does not harm them.

Similar to the recent editor_program migration, no standalone getter is introduced to keep the code minimal. The dynamically allocated memory is now managed by 'repo_config_values_clear()'.

On top of that, fix memory leaks in pager.c while we are at it.

---
 environment.c |  2 ++
 environment.h |  1 +
 pager.c       | 32 +++++++++++++++++++++++---------
 3 files changed, 26 insertions(+), 9 deletions(-)
environment.c +2 -0
index a65d575af4..975c9cb9eb 100644
@@ -725,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->attributes_file = NULL;
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
+	cfg->pager_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -742,4 +743,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->attributes_file);
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
+	FREE_AND_NULL(cfg->pager_program);
 }
environment.h +1 -0
index 8178ebab76..39b6691b47 100644
@@ -92,6 +92,7 @@ struct repo_config_values {
 	char *attributes_file;
 	char *excludes_file;
 	char *editor_program;
+	char *pager_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
pager.c +23 -9
index 35b210e048..543ef12936 100644
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "sigchain.h"
 #include "alias.h"
+#include "repository.h"
+#include "environment.h"
 
 int pager_use_color = 1;
 
@@ -13,7 +15,6 @@ int pager_use_color = 1;
 #endif
 
 static struct child_process pager_process;
-static char *pager_program;
 static int old_fd1 = -1, old_fd2 = -1;
 
 /* Is the value coming back from term_columns() just a guess? */
@@ -75,10 +76,17 @@ static void wait_for_pager_signal(int signo)
 
 static int core_pager_config(const char *var, const char *value,
 			     const struct config_context *ctx UNUSED,
-			     void *data UNUSED)
+			     void *data)
 {
-	if (!strcmp(var, "core.pager"))
-		return git_config_string(&pager_program, var, value);
+	struct repository *r = data;
+
+	if (!strcmp(var, "core.pager")) {
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		FREE_AND_NULL(cfg->pager_program);
+		return git_config_string(&cfg->pager_program, var, value);
+	}
+
 	return 0;
 }
 
@@ -91,10 +99,12 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
 
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
-		if (!pager_program)
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		if (!cfg->pager_program)
 			read_early_config(r,
-					  core_pager_config, NULL);
-		pager = pager_program;
+					  core_pager_config, r);
+		pager = cfg->pager_program;
 	}
 	if (!pager)
 		pager = getenv("PAGER");
@@ -302,7 +312,11 @@ int check_pager_config(struct repository *r, const char *cmd)
 
 	read_early_config(r, pager_command_config, &data);
 
-	if (data.value)
-		pager_program = data.value;
+	if (data.value) {
+		struct repo_config_values *cfg = repo_config_values(r);
+
+		free(cfg->pager_program);
+		cfg->pager_program = data.value;
+	}
 	return data.want;
 }
Signature · 1 line
2.43.0
#108 lore raw

PATCH v12 5/10 environment: move askpass_program into repo_config_values

In reply to Tian Yuchen

The global variable 'askpass_program' stores the path to the program used to prompt the user for credentials. Move it into repo_config_values to continue the libification effort.

While it is uncommon for a single process to require different askpass programs for different repositories, maintaining this value as a mutable global string is a blocker for libification. Global heap-allocated strings introduce thread-safety issues in a multi-repo environment.

Move 'askpass_program' into 'struct repo_config_values' to eliminate this global state. The memory is now safely managed and freed via 'repo_config_values_clear()'.

---
 environment.c | 7 ++++---
 environment.h | 3 +--
 prompt.c      | 3 ++-
 3 files changed, 7 insertions(+), 6 deletions(-)
environment.c +4 -3
index 975c9cb9eb..3857818da3 100644
@@ -55,7 +55,6 @@ int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
-char *askpass_program;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
@@ -464,8 +463,8 @@ int git_default_core_config(const char *var, const char *value,
 	}
 
 	if (!strcmp(var, "core.askpass")) {
-		FREE_AND_NULL(askpass_program);
-		return git_config_string(&askpass_program, var, value);
+		FREE_AND_NULL(cfg->askpass_program);
+		return git_config_string(&cfg->askpass_program, var, value);
 	}
 
 	if (!strcmp(var, "core.excludesfile")) {
@@ -726,6 +725,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->excludes_file = NULL;
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
+	cfg->askpass_program = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -744,4 +744,5 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->excludes_file);
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
+	FREE_AND_NULL(cfg->askpass_program);
 }
environment.h +1 -2
index 39b6691b47..856dc70cc4 100644
@@ -93,6 +93,7 @@ struct repo_config_values {
 	char *excludes_file;
 	char *editor_program;
 	char *pager_program;
+	char *askpass_program;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -220,8 +221,6 @@ const char *get_commit_output_encoding(void);
 extern char *git_commit_encoding;
 extern char *git_log_output_encoding;
 
-extern char *askpass_program;
-
 /*
  * The character that begins a commented line in user-editable file
  * that is subject to stripspace.
prompt.c +2 -1
index 706fba2a50..d8d74c7e37 100644
@@ -3,6 +3,7 @@
 #include "git-compat-util.h"
 #include "parse.h"
 #include "environment.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
@@ -51,7 +52,7 @@ char *git_prompt(const char *prompt, int flags)
 
 		askpass = getenv("GIT_ASKPASS");
 		if (!askpass)
-			askpass = askpass_program;
+			askpass = repo_config_values(the_repository)->askpass_program;
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
Signature · 1 line
2.43.0
#109 lore raw

PATCH v12 6/10 environment: migrate apply_default_whitespace and apply_default_ignorewhitespace

In reply to Tian Yuchen

The global variables 'apply_default_whitespace' and 'apply_default_ignorewhitespace' are used to store the default whitespace configuration for 'git apply'. Move these variables into 'struct repo_config_values' to continue the libification effort.

Dynamically allocated strings fetched via 'repo_config_get_string()' are now tracked per-repository and safely freed in 'repo_config_values_clear()'.

As part of this transition, update 'git_apply_config()' to accept a 'struct repository *' argument rather than relying on the 'the_repository' global.

---
 apply.c       | 28 ++++++++++++++++++++--------
 environment.c |  6 ++++--
 environment.h |  4 ++--
 3 files changed, 26 insertions(+), 12 deletions(-)
apply.c +20 -8
index 249248d4f2..b1db1fe495 100644
@@ -47,11 +47,17 @@ struct gitdiff_data {
 	int p_value;
 };
 
-static void git_apply_config(void)
+static void git_apply_config(struct repository *repo)
 {
-	repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace);
-	repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	repo_config(the_repository, git_xmerge_config, NULL);
+	struct repo_config_values *cfg = repo_config_values(repo);
+
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	repo_config_get_string(repo, "apply.whitespace",
+			       &cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
+	repo_config_get_string(repo, "apply.ignorewhitespace",
+			       &cfg->apply_default_ignorewhitespace);
+	repo_config(repo, git_xmerge_config, NULL);
 }
 
 static int parse_whitespace_option(struct apply_state *state, const char *option)
@@ -109,6 +115,8 @@ int init_apply_state(struct apply_state *state,
 		     struct repository *repo,
 		     const char *prefix)
 {
+	struct repo_config_values *cfg = repo_config_values(repo);
+
 	memset(state, 0, sizeof(*state));
 	state->prefix = prefix;
 	state->repo = repo;
@@ -126,10 +134,13 @@ int init_apply_state(struct apply_state *state,
 	strset_init(&state->kept_symlinks);
 	strbuf_init(&state->root, 0);
 
-	git_apply_config();
-	if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
+	git_apply_config(repo);
+
+	if (cfg->apply_default_whitespace &&
+	    parse_whitespace_option(state, cfg->apply_default_whitespace))
 		return -1;
-	if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+	if (cfg->apply_default_ignorewhitespace &&
+	    parse_ignorewhitespace_option(state, cfg->apply_default_ignorewhitespace))
 		return -1;
 	return 0;
 }
@@ -192,7 +203,8 @@ int check_apply_state(struct apply_state *state, int force_apply)
 
 static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!state->whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option &&
+	    !repo_config_values(state->repo)->apply_default_whitespace)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
environment.c +4 -2
index 3857818da3..20500658a2 100644
@@ -49,8 +49,6 @@ int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
 char *git_commit_encoding;
 char *git_log_output_encoding;
-char *apply_default_whitespace;
-char *apply_default_ignorewhitespace;
 int fsync_object_files = -1;
 int use_fsync = -1;
 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
@@ -726,6 +724,8 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->editor_program = NULL;
 	cfg->pager_program = NULL;
 	cfg->askpass_program = NULL;
+	cfg->apply_default_whitespace = NULL;
+	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
@@ -745,4 +745,6 @@ void repo_config_values_clear(struct repo_config_values *cfg)
 	FREE_AND_NULL(cfg->editor_program);
 	FREE_AND_NULL(cfg->pager_program);
 	FREE_AND_NULL(cfg->askpass_program);
+	FREE_AND_NULL(cfg->apply_default_whitespace);
+	FREE_AND_NULL(cfg->apply_default_ignorewhitespace);
 }
environment.h +2 -2
index 856dc70cc4..f450242ac0 100644
@@ -94,6 +94,8 @@ struct repo_config_values {
 	char *editor_program;
 	char *pager_program;
 	char *askpass_program;
+	char *apply_default_whitespace;
+	char *apply_default_ignorewhitespace;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -182,8 +184,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern char *apply_default_whitespace;
-extern char *apply_default_ignorewhitespace;
 extern unsigned long pack_size_limit_cfg;
 
 extern int protect_hfs;
Signature · 1 line
2.43.0
#110 lore raw

PATCH v12 7/10 environment: move push_default into repo_config_values

In reply to Tian Yuchen

The global variable 'push_default' specifies the default behavior of 'git push' when no explicit refspec is provided. Move 'push_default' into 'struct repo_config_values' to continue the libification effort.

While 'enum push_default_type' ideally belongs in 'remote.h', moving it there introduces a circular dependency chain:

remote.h -> hash.h -> repository.h -> environment.h.

Therefore, the enum definition is kept in 'environment.h' just above 'struct repo_config_values' with a NEEDSWORK comment for future cleanup.

Modify the configuration parsing in environment.c to update the per-repository structure directly, and update caller across the codebase to access the value via 'repo_config_values()'.

---
 builtin/push.c | 10 ++++++----
 environment.c  | 16 +++++++++-------
 environment.h  | 26 ++++++++++++++++----------
 remote.c       |  2 +-
 4 files changed, 32 insertions(+), 22 deletions(-)
builtin/push.c +6 -4
index 6021b71d66..7578ff38c4 100644
@@ -73,6 +73,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 				  struct remote *remote, struct ref *matched)
 {
 	const char *branch_name;
+	struct repo_config_values *cfg = repo_config_values(the_repository);
 
 	if (remote->push.nr) {
 		struct refspec_item query = {
@@ -88,7 +89,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
 		}
 	}
 
-	if (push_default == PUSH_DEFAULT_UPSTREAM &&
+	if (cfg->push_default == PUSH_DEFAULT_UPSTREAM &&
 	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
 		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
@@ -160,7 +161,7 @@ static NORETURN void die_push_simple(struct branch *branch,
 	 * Don't show advice for people who explicitly set
 	 * push.default.
 	 */
-	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
+	if (cfg->push_default == PUSH_DEFAULT_UNSPECIFIED)
 		advice_pushdefault_maybe = _("\n"
 				 "To choose either option permanently, "
 				 "see push.default in 'git help config'.\n");
@@ -231,8 +232,9 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	struct branch *branch;
 	const char *dst;
 	int same_remote;
+	struct repo_config_values *cfg = repo_config_values(the_repository);
 
-	switch (push_default) {
+	switch (cfg->push_default) {
 	case PUSH_DEFAULT_MATCHING:
 		refspec_append(&rs, ":");
 		return;
@@ -252,7 +254,7 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote)
 	dst = branch->refname;
 	same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL));
 
-	switch (push_default) {
+	switch (cfg->push_default) {
 	default:
 	case PUSH_DEFAULT_UNSPECIFIED:
 	case PUSH_DEFAULT_SIMPLE:
environment.c +9 -7
index 20500658a2..66c1ac1ab8 100644
@@ -58,7 +58,6 @@ enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
-enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -620,21 +619,23 @@ static int git_default_branch_config(const char *var, const char *value)
 
 static int git_default_push_config(const char *var, const char *value)
 {
+	struct repo_config_values *cfg = repo_config_values(the_repository);
+
 	if (!strcmp(var, "push.default")) {
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "nothing"))
-			push_default = PUSH_DEFAULT_NOTHING;
+			cfg->push_default = PUSH_DEFAULT_NOTHING;
 		else if (!strcmp(value, "matching"))
-			push_default = PUSH_DEFAULT_MATCHING;
+			cfg->push_default = PUSH_DEFAULT_MATCHING;
 		else if (!strcmp(value, "simple"))
-			push_default = PUSH_DEFAULT_SIMPLE;
+			cfg->push_default = PUSH_DEFAULT_SIMPLE;
 		else if (!strcmp(value, "upstream"))
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "tracking")) /* deprecated */
-			push_default = PUSH_DEFAULT_UPSTREAM;
+			cfg->push_default = PUSH_DEFAULT_UPSTREAM;
 		else if (!strcmp(value, "current"))
-			push_default = PUSH_DEFAULT_CURRENT;
+			cfg->push_default = PUSH_DEFAULT_CURRENT;
 		else {
 			error(_("malformed value for %s: %s"), var, value);
 			return error(_("must be one of nothing, matching, simple, "
@@ -726,6 +727,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->askpass_program = NULL;
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
+	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +16 -10
index f450242ac0..17a3a628d2 100644
@@ -87,6 +87,21 @@ extern const char * const local_repo_env[];
 struct strvec;
 
 struct repository;
+
+/*
+ * NEEDSWORK: It would be better if these definitions could be moved to
+ * other more specific files, but care is needed to avoid circular
+ * inclusion issues.
+ */
+enum push_default_type {
+	PUSH_DEFAULT_NOTHING = 0,
+	PUSH_DEFAULT_MATCHING,
+	PUSH_DEFAULT_SIMPLE,
+	PUSH_DEFAULT_UPSTREAM,
+	PUSH_DEFAULT_CURRENT,
+	PUSH_DEFAULT_UNSPECIFIED
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -96,6 +111,7 @@ struct repo_config_values {
 	char *askpass_program;
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
+	enum push_default_type push_default;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -197,16 +213,6 @@ enum rebase_setup_type {
 };
 extern enum rebase_setup_type autorebase;
 
-enum push_default_type {
-	PUSH_DEFAULT_NOTHING = 0,
-	PUSH_DEFAULT_MATCHING,
-	PUSH_DEFAULT_SIMPLE,
-	PUSH_DEFAULT_UPSTREAM,
-	PUSH_DEFAULT_CURRENT,
-	PUSH_DEFAULT_UNSPECIFIED
-};
-extern enum push_default_type push_default;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
remote.c +1 -1
index 00723b385e..d48c01d375 100644
@@ -1933,7 +1933,7 @@ static char *branch_get_push_1(struct repository *repo,
 	if (remote->mirror)
 		return tracking_for_push_dest(remote, branch->refname, err);
 
-	switch (push_default) {
+	switch (repo_config_values(repo)->push_default) {
 	case PUSH_DEFAULT_NOTHING:
 		return error_buf(err, _("push has no destination (push.default is 'nothing')"));
 
Signature · 1 line
2.43.0
#111 lore raw

PATCH v12 8/10 environment: move autorebase into repo_config_values

In reply to Tian Yuchen

The global variable 'autorebase' dictates whether a newly created branch should be configured to automatically rebase by default. Move it into 'struct repo_config_values' to continue the libification effort.

The 'enum rebase_setup_type' definition is moved higher up in 'environment.h' so that it is visible to the repository-specific structure. The default state AUTOREBASE_NEVER is now correctly initialized in 'repo_config_values_init()'.

Configuration parsing in 'git_default_branch_config()' is updated to write directly to the repository's configuration instance.

---
 branch.c      |  2 +-
 environment.c | 10 +++++-----
 environment.h | 16 ++++++++--------
 3 files changed, 14 insertions(+), 14 deletions(-)
branch.c +1 -1
index 243db7d0fc..e1c1f8c89d 100644
@@ -61,7 +61,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 
 static int should_setup_rebase(const char *origin)
 {
-	switch (autorebase) {
+	switch (repo_config_values(the_repository)->autorebase) {
 	case AUTOREBASE_NEVER:
 		return 0;
 	case AUTOREBASE_LOCAL:
environment.c +5 -5
index 66c1ac1ab8..c0bf7577b7 100644
@@ -57,7 +57,6 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
 char *check_roundtrip_encoding;
-enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
@@ -601,13 +600,13 @@ static int git_default_branch_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		else if (!strcmp(value, "never"))
-			autorebase = AUTOREBASE_NEVER;
+			cfg->autorebase = AUTOREBASE_NEVER;
 		else if (!strcmp(value, "local"))
-			autorebase = AUTOREBASE_LOCAL;
+			cfg->autorebase = AUTOREBASE_LOCAL;
 		else if (!strcmp(value, "remote"))
-			autorebase = AUTOREBASE_REMOTE;
+			cfg->autorebase = AUTOREBASE_REMOTE;
 		else if (!strcmp(value, "always"))
-			autorebase = AUTOREBASE_ALWAYS;
+			cfg->autorebase = AUTOREBASE_ALWAYS;
 		else
 			return error(_("malformed value for %s"), var);
 		return 0;
@@ -728,6 +727,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_whitespace = NULL;
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
+	cfg->autorebase = AUTOREBASE_NEVER;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +8 -8
index 17a3a628d2..46b2f0d861 100644
@@ -102,6 +102,13 @@ enum push_default_type {
 	PUSH_DEFAULT_UNSPECIFIED
 };
 
+enum rebase_setup_type {
+	AUTOREBASE_NEVER = 0,
+	AUTOREBASE_LOCAL,
+	AUTOREBASE_REMOTE,
+	AUTOREBASE_ALWAYS
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -112,6 +119,7 @@ struct repo_config_values {
 	char *apply_default_whitespace;
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
+	enum rebase_setup_type autorebase;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -205,14 +213,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum rebase_setup_type {
-	AUTOREBASE_NEVER = 0,
-	AUTOREBASE_LOCAL,
-	AUTOREBASE_REMOTE,
-	AUTOREBASE_ALWAYS
-};
-extern enum rebase_setup_type autorebase;
-
 enum object_creation_mode {
 	OBJECT_CREATION_USES_HARDLINKS = 0,
 	OBJECT_CREATION_USES_RENAMES = 1
Signature · 1 line
2.43.0
#112 lore raw

PATCH v12 9/10 environment: move object_creation_mode into repo_config_values

In reply to Tian Yuchen

The global variable 'object_creation_mode' controls how Git creates object files, specifically determining whether to use hardlinks or renames when moving temporary files into the object database. Move it into 'struct repo_config_values' to continue the libification effort.

Move the 'enum object_creation_mode' definition higher up in 'environment.h' to ensure it is visible to the structure. Initialize the per-repository value to its default macro value OBJECT_CREATION_MODE inside 'repo_config_values_init()'.

Update configuration parsing in 'git_default_core_config()' to write directly to the repository-specific configuration structure.

---
 environment.c |  6 +++---
 environment.h | 12 ++++++------
 object-file.c |  3 ++-
 3 files changed, 11 insertions(+), 10 deletions(-)
environment.c +3 -3
index c0bf7577b7..ef3c032e0c 100644
@@ -60,7 +60,6 @@ char *check_roundtrip_encoding;
 #ifndef OBJECT_CREATION_MODE
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
-enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 int grafts_keep_true_parents;
 unsigned long pack_size_limit_cfg;
 
@@ -512,9 +511,9 @@ int git_default_core_config(const char *var, const char *value,
 		if (!value)
 			return config_error_nonbool(var);
 		if (!strcmp(value, "rename"))
-			object_creation_mode = OBJECT_CREATION_USES_RENAMES;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_RENAMES;
 		else if (!strcmp(value, "link"))
-			object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
+			cfg->object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
 		else
 			die(_("invalid mode for object creation: %s"), value);
 		return 0;
@@ -728,6 +727,7 @@ void repo_config_values_init(struct repo_config_values *cfg)
 	cfg->apply_default_ignorewhitespace = NULL;
 	cfg->push_default = PUSH_DEFAULT_UNSPECIFIED;
 	cfg->autorebase = AUTOREBASE_NEVER;
+	cfg->object_creation_mode = OBJECT_CREATION_MODE;
 	cfg->apply_sparse_checkout = 0;
 	cfg->branch_track = BRANCH_TRACK_REMOTE;
 	cfg->trust_ctime = 1;
environment.h +6 -6
index 46b2f0d861..a47a5c83db 100644
@@ -109,6 +109,11 @@ enum rebase_setup_type {
 	AUTOREBASE_ALWAYS
 };
 
+enum object_creation_mode {
+	OBJECT_CREATION_USES_HARDLINKS = 0,
+	OBJECT_CREATION_USES_RENAMES = 1
+};
+
 struct repo_config_values {
 	/* section "core" config values */
 	char *attributes_file;
@@ -120,6 +125,7 @@ struct repo_config_values {
 	char *apply_default_ignorewhitespace;
 	enum push_default_type push_default;
 	enum rebase_setup_type autorebase;
+	enum object_creation_mode object_creation_mode;
 	int apply_sparse_checkout;
 	int trust_ctime;
 	int check_stat;
@@ -213,12 +219,6 @@ extern unsigned long pack_size_limit_cfg;
 extern int protect_hfs;
 extern int protect_ntfs;
 
-enum object_creation_mode {
-	OBJECT_CREATION_USES_HARDLINKS = 0,
-	OBJECT_CREATION_USES_RENAMES = 1
-};
-extern enum object_creation_mode object_creation_mode;
-
 extern int grafts_keep_true_parents;
 
 const char *get_log_output_encoding(void);
object-file.c +2 -1
index 9afa842da2..c00dd3afca 100644
@@ -411,11 +411,12 @@ int finalize_object_file_flags(struct repository *repo,
 {
 	unsigned retries = 0;
 	int ret;
+	struct repo_config_values *cfg = repo_config_values(repo);
 
 retry:
 	ret = 0;
 
-	if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
+	if (cfg->object_creation_mode == OBJECT_CREATION_USES_RENAMES)
 		goto try_rename;
 	else if (link(tmpfile, filename))
 		ret = errno;
Signature · 1 line
2.43.0
#113 lore raw

PATCH v12 10/10 repository: adjust the comment of config_values_private_

In reply to Tian Yuchen

The configurations in 'struct config_values_private_' are not all parsed in 'git_default_config()'. For example, 'pager_program' is now parsed in 'pager.c'. Therefore, update the comment.

---
 repository.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
repository.h +1 -1
index 36e2db2633..9093e6af93 100644
@@ -152,7 +152,7 @@ struct repository {
 	/* Repository's compatibility hash algorithm. */
 	const struct git_hash_algo *compat_hash_algo;
 
-	/* Repository's config values parsed by git_default_config() */
+	/* Repository-specific configuration values. */
 	struct repo_config_values config_values_private_;
 
 	/* Repository's reference storage format, as serialized on disk. */
Signature · 1 line
2.43.0