Subject: [PATCH 01/18] builtin: introduce startup_info struct

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 7 Mar 2010 11:55:50 +0700

The purpose of this struct is to make it easier to extend parameters
passed to builtin commands from run_builtin(), and let libgit aware of
some global states in future.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 cache.h       |    6 ++++++
 environment.c |    1 +
 git.c         |    9 +++++----
 setup.c       |   12 +++++++++++-
 4 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/cache.h b/cache.h
index 89f6a40..30fddf1 100644
--- a/cache.h
+++ b/cache.h
@@ -1054,4 +1054,10 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 char *alias_lookup(const char *alias);
 int split_cmdline(char *cmdline, const char ***argv);
 
+/* git.c */
+struct startup_info {
+	const char *prefix;
+};
+extern struct startup_info *startup_info;
+
 #endif /* CACHE_H */
diff --git a/environment.c b/environment.c
index 876c5e5..c36c902 100644
--- a/environment.c
+++ b/environment.c
@@ -52,6 +52,7 @@ enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
 char *notes_ref_name;
 int grafts_replace_parents = 1;
 int core_apply_sparse_checkout;
+struct startup_info *startup_info;
 
 /* Parallel index stat data preload? */
 int core_preload_index = 0;
diff --git a/git.c b/git.c
index 6bae305..4be34e4 100644
--- a/git.c
+++ b/git.c
@@ -13,6 +13,7 @@ const char git_usage_string[] =
 const char git_more_info_string[] =
 	"See 'git help COMMAND' for more information on a specific command.";
 
+static struct startup_info git_startup_info;
 static int use_pager = -1;
 struct pager_config {
 	const char *cmd;
@@ -237,13 +238,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 {
 	int status, help;
 	struct stat st;
-	const char *prefix;
 
-	prefix = NULL;
+	memset(&git_startup_info, 0, sizeof(git_startup_info));
+	startup_info = &git_startup_info;
 	help = argc == 2 && !strcmp(argv[1], "-h");
 	if (!help) {
 		if (p->option & RUN_SETUP)
-			prefix = setup_git_directory();
+			setup_git_directory();
 
 		if (use_pager == -1 && p->option & RUN_SETUP)
 			use_pager = check_pager_config(p->cmd);
@@ -257,7 +258,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 	trace_argv_printf(argv, "trace: built-in: git");
 
-	status = p->fn(argc, argv, prefix);
+	status = p->fn(argc, argv, startup_info->prefix);
 	if (status)
 		return status;
 
diff --git a/setup.c b/setup.c
index 5716d90..cf1b37d 100644
--- a/setup.c
+++ b/setup.c
@@ -315,7 +315,7 @@ const char *read_gitfile_gently(const char *path)
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
  */
-const char *setup_git_directory_gently(int *nongit_ok)
+static const char *setup_git_directory_gently_1(int *nongit_ok)
 {
 	const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 	const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
@@ -443,6 +443,16 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	return cwd + offset;
 }
 
+const char *setup_git_directory_gently(int *nongit_ok)
+{
+	const char *prefix;
+
+	prefix = setup_git_directory_gently_1(nongit_ok);
+	if (startup_info)
+		startup_info->prefix = prefix;
+	return prefix;
+}
+
 int git_config_perm(const char *var, const char *value)
 {
 	int i;
-- 
1.7.0.195.g637a2