[PATCH 2/5] First step, making setup (somewhat) reentrant

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



setup_git_env() just initialized some static data and then let it
leak. For reentrant operation, as in using a git process to access
serveral repositories in order, this data has to be freed and properly
reinitialized.

get_git_work_tree() used a once only initialization flag, we now provide
a reset_git_work_tree() to free allocated resources and let it initialize
itself on the next call.

create_default_files() in init-db needed to be fixed to compare strings
properly.

Signed-off-by: Christian Thaeter <ct@xxxxxxxxxx>
---
 builtin-init-db.c |    4 ++--
 environment.c     |   43 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/builtin-init-db.c b/builtin-init-db.c
index e1393b8..d5a5dd9 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -270,8 +270,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
 		git_config_set("core.bare", "false");
 		/* allow template config file to override the default */
 		if (log_all_ref_updates == -1)
-		    git_config_set("core.logallrefupdates", "true");
-		if (work_tree != git_work_tree_cfg)
+			git_config_set("core.logallrefupdates", "true");
+		if (!streq(work_tree, git_work_tree_cfg))
 			git_config_set("core.worktree", work_tree);
 	}
 
diff --git a/environment.c b/environment.c
index 18a1c4e..492d87c 100644
--- a/environment.c
+++ b/environment.c
@@ -38,31 +38,48 @@ int auto_crlf = 0;	/* 1: both ways, -1: only when adding git objects */
 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
-char *git_work_tree_cfg;
-static const char *work_tree;
+char *git_work_tree_cfg = NULL;
+static char *work_tree = NULL;
+static int work_tree_initialized = 0;
 
 static const char *git_dir;
-static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
+static char *git_object_dir = NULL, *git_index_file = NULL, *git_refs_dir = NULL, *git_graft_file = NULL;
 
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
 	if (!git_dir)
 		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
+	if (git_object_dir)
+		free(git_object_dir);
 	git_object_dir = getenv(DB_ENVIRONMENT);
-	if (!git_object_dir) {
+	if (git_object_dir) {
+		git_object_dir = xstrdup(git_object_dir);
+	}
+	else {
 		git_object_dir = xmalloc(strlen(git_dir) + 9);
 		sprintf(git_object_dir, "%s/objects", git_dir);
 	}
+	if (git_refs_dir)
+		free(git_refs_dir);
 	git_refs_dir = xmalloc(strlen(git_dir) + 6);
 	sprintf(git_refs_dir, "%s/refs", git_dir);
+	if (git_index_file)
+		free(git_index_file);
 	git_index_file = getenv(INDEX_ENVIRONMENT);
-	if (!git_index_file) {
+	if (git_index_file) {
+		git_index_file = xstrdup(git_index_file);
+	}
+	else {
 		git_index_file = xmalloc(strlen(git_dir) + 7);
 		sprintf(git_index_file, "%s/index", git_dir);
 	}
+	if (git_graft_file)
+		free (git_graft_file);
 	git_graft_file = getenv(GRAFT_ENVIRONMENT);
-	if (!git_graft_file)
+	if (git_graft_file)
+		git_graft_file = xstrdup(git_graft_file);
+	else
 		git_graft_file = xstrdup(git_path("info/grafts"));
 }
 
@@ -79,10 +96,16 @@ const char *get_git_dir(void)
 	return git_dir;
 }
 
+void reset_git_work_tree(void)
+{
+	work_tree_initialized = 0;
+	if (work_tree)
+		free (work_tree);
+}
+
 const char *get_git_work_tree(void)
 {
-	static int initialized = 0;
-	if (!initialized) {
+	if (!work_tree_initialized) {
 		work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
 		/* core.bare = true overrides implicit and config work tree */
 		if (!work_tree && is_bare_repository_cfg < 1) {
@@ -90,11 +113,13 @@ const char *get_git_work_tree(void)
 			/* make_absolute_path also normalizes the path */
 			if (work_tree && !is_absolute_path(work_tree))
 				work_tree = xstrdup(make_absolute_path(git_path(work_tree)));
+			else if (work_tree)
+				work_tree = xstrdup(work_tree);
 		} else if (work_tree)
 			work_tree = xstrdup(make_absolute_path(work_tree));
-		initialized = 1;
 		if (work_tree)
 			is_bare_repository_cfg = 0;
+		work_tree_initialized = 1;
 	}
 	return work_tree;
 }
-- 
1.5.3.7

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux