Subject: [PATCH 15/16] alias: keep repository found while collecting aliases as long as possible

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 11 Mar 2010 20:22:33 +0700

In order to read aliases, $GIT_CONFIG/config must be read (if found).
Currently, after alias handling is done, we chdir() back to original
cwd, pretending no repository setup is done.

Given plain chdir() is not a proper way to undo
setup_git_directory_gently(), and 80% builtin commands will need to
search for a repository, we could keep the repository found in alias
handling code. Until we are clear, there's no need for a repository,
then we can undo the setup with unset_git_directory().

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

diff --git a/git.c b/git.c
index 6a40304..aa6d029 100644
--- a/git.c
+++ b/git.c
@@ -152,7 +152,8 @@ static int handle_alias(int *argcp, const char ***argv)
 	char *alias_string;
 	int unused_nongit;
 
-	setup_git_directory_gently(&unused_nongit);
+	if (!startup_info->have_repository)
+		setup_git_directory_gently(&unused_nongit);
 
 	alias_command = (*argv)[0];
 	alias_string = alias_lookup(alias_command);
@@ -209,8 +210,6 @@ static int handle_alias(int *argcp, const char ***argv)
 		ret = 1;
 	}
 
-	unset_git_directory(startup_info->prefix);
-
 	errno = saved_errno;
 
 	return ret;
@@ -240,12 +239,18 @@ 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)
+		if ((p->option & RUN_SETUP) && !startup_info->have_repository)
 			setup_git_directory();
-		if (p->option & RUN_SETUP_GENTLY) {
+		else if ((p->option & RUN_SETUP_GENTLY) && !startup_info->have_repository) {
 			int nongit_ok;
 			setup_git_directory_gently(&nongit_ok);
 		}
+		else if (startup_info->have_repository) {
+			if (p->option & (RUN_SETUP_GENTLY | RUN_SETUP))
+				; /* done already */
+			else
+				unset_git_directory(startup_info->prefix);
+		}
 
 		if (use_pager == -1 && p->option & RUN_SETUP)
 			use_pager = check_pager_config(p->cmd);
-- 
1.7.0.1.384.g6abcaa