git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] run_builtin(): save "-h" detection result for later use
@ 2010-10-19 13:35 Nguyễn Thái Ngọc Duy
  2010-10-19 13:35 ` [PATCH 2/3] builtins: utilize startup_info->help where possible Nguyễn Thái Ngọc Duy
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2010-10-19 13:35 UTC (permalink / raw)
  To: git, Junio C Hamano; +Cc: Nguyễn Thái Ngọc Duy

When run_builtin() sees "-h" as the first argument, it assumes:

 - this is 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 do
other things before calling parse_options(), which is often where the
help usage is printed.  Some of those things may try to access the
repository, which has yet to be set up.

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

git_config() is a bit of a special case: it is perfectly legitimate to
use it without access to a valid repository. In the future, we should
make git_config() aware of the repository search status so that it can
skip reading $GIT_DIR/config if no repository is found.
---
 cache.h |    1 +
 git.c   |    8 ++++----
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/cache.h b/cache.h
index 33decd9..43763c1 100644
--- a/cache.h
+++ b/cache.h
@@ -1117,6 +1117,7 @@ const char *split_cmdline_strerror(int cmdline_errno);
 /* git.c */
 struct startup_info {
 	int have_repository;
+	int help; /* print help and exit, except git_config(), repo must not be touched */
 };
 extern struct startup_info *startup_info;
 
diff --git a/git.c b/git.c
index 50a1401..5962cdd 100644
--- a/git.c
+++ b/git.c
@@ -246,13 +246,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;
 	const char *prefix;
 
 	prefix = NULL;
-	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)
 			prefix = setup_git_directory();
 		if (p->option & RUN_SETUP_GENTLY) {
@@ -267,7 +267,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.445.gcbdb3

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-10-20  1:22 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-19 13:35 [PATCH 1/3] run_builtin(): save "-h" detection result for later use Nguyễn Thái Ngọc Duy
2010-10-19 13:35 ` [PATCH 2/3] builtins: utilize startup_info->help where possible Nguyễn Thái Ngọc Duy
2010-10-19 17:29   ` Jonathan Nieder
2010-10-19 18:50     ` Jonathan Nieder
2010-10-19 19:10     ` Junio C Hamano
2010-10-20  1:13     ` Nguyen Thai Ngoc Duy
2010-10-20  1:18       ` Jonathan Nieder
2010-10-19 13:35 ` [PATCH 3/3] builtins: check for startup_info->help, print and exit early Nguyễn Thái Ngọc Duy
2010-10-19 16:33   ` Junio C Hamano
2010-10-20  1:18     ` Nguyen Thai Ngoc Duy
2010-10-19 17:55   ` Jonathan Nieder
2010-10-19 16:48 ` [PATCH 1/3] run_builtin(): save "-h" detection result for later use Jonathan Nieder

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).