Subject: [PATCH v2 14/19] run_builtin(): save "-h" detection result for later use

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 21 Mar 2010 17:30:41 +0700

When run_builtin() sees "-h" as the first argument, it assumes that:
 - it's the call for help usage
 - the real git command will only print help usage then exit

So it skips all setup in this case. Unfortunately some commands may do
more things before calling parse_options(), which is where help usage
is printed. Some of those things may touch repository, which has yet
to be set up.

Until now, the only safe thing that can be called before
parse_options() is git_config(). This function is aware of repository
findind status, and will skip reading $GIT_DIR/config if no repository
is found.

Make real commands aware of this fast path so that they can handle it
properly (i.e. print help usage then exist immediately) if they do
more initialization than git_config().

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

diff --git a/cache.h b/cache.h
index dd27e11..e397e1d 100644
--- a/cache.h
+++ b/cache.h
@@ -1060,6 +1060,7 @@ int split_cmdline(char *cmdline, const char ***argv);
 struct startup_info {
 	const char *prefix;
 	int have_repository;
+	int help;
 };
 extern struct startup_info *startup_info;
 
diff --git a/git.c b/git.c
index 746470f..7e30498 100644
--- a/git.c
+++ b/git.c
@@ -237,13 +237,13 @@ struct cmd_struct {
 
 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 {
-	int status, help;
+	int status;
 	struct stat st;
 
 	memset(&git_startup_info, 0, sizeof(git_startup_info));
 	startup_info = &git_startup_info;
-	help = argc == 2 && !strcmp(argv[1], "-h");
-	if (!help) {
+	startup_info->help = argc == 2 && !strcmp(argv[1], "-h");
+	if (!startup_info->help) {
 		if (p->option & RUN_SETUP)
 			setup_git_directory();
 		if (p->option & RUN_SETUP_GENTLY) {
@@ -261,7 +261,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 	}
 	commit_pager_choice();
 
-	if (!help && p->option & NEED_WORK_TREE)
+	if (!startup_info->help && p->option & NEED_WORK_TREE)
 		setup_work_tree();
 
 	trace_argv_printf(argv, "trace: built-in: git");
-- 
1.7.0.2.425.gb99f1