diff mbox series

[3/4] environment.c: convert excludes_file to struct strbuf

Message ID 657f1780-c813-459d-aecc-94a10f7a3b4f@gmail.com (mailing list archive)
State New
Headers show
Series fix a leak with excludes_file | expand

Commit Message

Rubén Justo April 6, 2024, 2:32 p.m. UTC
Make excludes_file a strbuf, so that we don't have to worry about freeing
it if it is set multiple times.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 config.c      |  2 +-
 dir.c         | 13 +++++++++----
 environment.c |  2 +-
 environment.h |  2 +-
 4 files changed, 12 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/config.c b/config.c
index 9beeb63b50..bc64af8efa 100644
--- a/config.c
+++ b/config.c
@@ -1595,7 +1595,7 @@  static int git_default_core_config(const char *var, const char *value,
 		return git_config_string(&askpass_program, var, value);
 
 	if (!strcmp(var, "core.excludesfile"))
-		return git_config_pathname(&excludes_file, var, value);
+		return git_config_strbuf_pathname(&excludes_file, var, value);
 
 	if (!strcmp(var, "core.whitespace")) {
 		if (!value)
diff --git a/dir.c b/dir.c
index 20ebe4cba2..e31ccd8e48 100644
--- a/dir.c
+++ b/dir.c
@@ -3386,10 +3386,15 @@  void setup_standard_excludes(struct dir_struct *dir)
 	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,
+	if (!excludes_file.len) {
+		char *str = xdg_config_home("ignore");
+		if (str) {
+			strbuf_addstr(&excludes_file, str);
+			free(str);
+		}
+	}
+	if (excludes_file.len && !access_or_warn(excludes_file.buf, R_OK, 0))
+		add_patterns_from_file_1(dir, excludes_file.buf,
 					 dir->untracked ? &dir->internal.ss_excludes_file : NULL);
 
 	/* per repository user preference */
diff --git a/environment.c b/environment.c
index a73ba9c12c..b4c66e7153 100644
--- a/environment.c
+++ b/environment.c
@@ -60,7 +60,7 @@  size_t delta_base_cache_limit = 96 * 1024 * 1024;
 unsigned long big_file_threshold = 512 * 1024 * 1024;
 const char *editor_program;
 const char *askpass_program;
-const char *excludes_file;
+struct strbuf excludes_file = STRBUF_INIT;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
diff --git a/environment.h b/environment.h
index 05fd94d7be..114e3dde99 100644
--- a/environment.h
+++ b/environment.h
@@ -222,7 +222,7 @@  extern const char *git_log_output_encoding;
 
 extern const char *editor_program;
 extern const char *askpass_program;
-extern const char *excludes_file;
+extern struct strbuf excludes_file;
 
 /*
  * Should we print an ellipsis after an abbreviated SHA-1 value