git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Thaeter <ct@pipapo.org>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Christian Thaeter <ct@pipapo.org>
Subject: [PATCH 2/5] First step, making setup (somewhat) reentrant
Date: Mon,  7 Jan 2008 06:47:32 +0100	[thread overview]
Message-ID: <1199684855-14246-2-git-send-email-ct@pipapo.org> (raw)
In-Reply-To: <1199684855-14246-1-git-send-email-ct@pipapo.org>

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@pipapo.org>
---
 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

  reply	other threads:[~2008-01-07  5:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-07  5:47 [PATCH 1/5] Add streq() to compat-util Christian Thaeter
2008-01-07  5:47 ` Christian Thaeter [this message]
2008-01-07  5:47   ` [PATCH 3/5] provide a reset_packed_git() function Christian Thaeter
2008-01-07  5:47     ` [PATCH 4/5] Export some more functions to enable resetting the git state Christian Thaeter
2008-01-07  5:47       ` [PATCH 5/5] Make setup_git_directory reentrant Christian Thaeter
2008-01-07  8:50   ` [PATCH 2/5] First step, making setup (somewhat) reentrant Johannes Schindelin
2008-01-07  9:22     ` Junio C Hamano
2008-01-07 11:28       ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1199684855-14246-2-git-send-email-ct@pipapo.org \
    --to=ct@pipapo.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).