Subject: [WIP PATCH 07/26] Support running setup_git_dir_gently() from the beginning for builtin commands

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Tue, 16 Feb 2010 23:04:58 +0700

Attempt to access gitdir is everywhere, even before cmd_*() is called.
The sooner we setup gitdir, the less trouble we may have to deal with.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 git.c |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/git.c b/git.c
index ea29cca..5391292 100644
--- a/git.c
+++ b/git.c
@@ -215,13 +215,14 @@ static int handle_alias(int *argcp, const char ***argv)
 
 const char git_version_string[] = GIT_VERSION;
 
-#define RUN_SETUP	(1<<0)
-#define USE_PAGER	(1<<1)
+#define RUN_SETUP		(1<<0)
+#define USE_PAGER		(1<<1)
 /*
  * require working tree to be present -- anything uses this needs
  * RUN_SETUP for reading from the configuration file.
  */
-#define NEED_WORK_TREE	(1<<2)
+#define NEED_WORK_TREE		(1<<2)
+#define RUN_SETUP_GENTLY	(1<<3)
 
 struct cmd_struct {
 	const char *cmd;
@@ -236,12 +237,17 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 
 	help = argc == 2 && !strcmp(argv[1], "-h");
 	if (!help) {
+		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) == (RUN_SETUP | RUN_SETUP_GENTLY))
+			die("Internal error: RUN_SETUP and RUN_SETUP_GENTLY are mutually exclusive");
+
 		/* handle_alias() may have set up gitdir for alias lookup */
 		if (startup_info->have_set_gitdir) {
 			if (p->option & RUN_SETUP) {
 				if (!startup_info->have_repository)
 					die("No repository found");
 			}
+			if (p->option & RUN_SETUP_GENTLY)
+				; /* all done */
 			else {
 				/* This is WRONG, but we can't get rid of it now */
 				startup_info->have_set_gitdir = 0;
@@ -252,6 +258,10 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 		else {
 			if (p->option & RUN_SETUP)
 				setup_git_directory();
+			if (p->option & RUN_SETUP_GENTLY) {
+				int nongit_ok;
+				setup_git_directory_gently(&nongit_ok);
+			}
 		}
 
 		if (use_pager == -1 && p->option & RUN_SETUP)
-- 
1.7.0.195.g637a2