Subject: [PATCH] builtins: reset startup_info->have_run_setup_gitdir when unsetting up repository

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sat, 27 Mar 2010 16:13:04 +0700

startup_info->have_run_setup_gitdir is used to guard unallowed access
to repository. When a repository has been set up and the real command
does not expect any setup, we should revert to the original "fresh"
state, including startup_info->have_run_setup_gitdir. Otherwise, the
next attempt to set up repository will fail.

While at it, also reset repository_format_version to zero. When
omitted, the format should be understood as the last supported
version, i.e. zero. This is probably only used by "git init" or "git
clone".

Fault caught by Jonathan Nieder (and demonstrated with a beautiful test case)

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Junio, I can split this up and put it to the original patches where the faults
 are introduced if you want.

 git.c           |   13 ++++++++-----
 setup.c         |    2 +-
 t/t0001-init.sh |   14 ++++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/git.c b/git.c
index 7ebfea5..5c249fd 100644
--- a/git.c
+++ b/git.c
@@ -242,11 +242,14 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 			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);
+		else if (startup_info->have_run_setup_gitdir) {
+			if (startup_info->have_repository) {
+				if (p->option & (RUN_SETUP_GENTLY | RUN_SETUP))
+					; /* done already */
+				else
+					unset_git_directory(startup_info->prefix);
+			}
+			startup_info->have_run_setup_gitdir = 0;
 		}
 
 		if (use_pager == -1 && p->option & RUN_SETUP)
diff --git a/setup.c b/setup.c
index d9bb616..43b529e 100644
--- a/setup.c
+++ b/setup.c
@@ -358,7 +358,7 @@ void unset_git_directory(const char *prefix)
 	inside_git_dir = -1;
 
 	/* Initialized in check_repository_format_version() */
-	repository_format_version = 0xFF;
+	repository_format_version = 0;
 	shared_repository = PERM_UMASK;
 	is_bare_repository_cfg = -1;
 	git_work_tree_cfg = NULL;
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 5386504..f93856b 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -33,6 +33,20 @@ test_expect_success 'plain' '
 	check_config plain/.git false unset
 '
 
+test_expect_success 'plain through aliased command' '
+	(
+		unset GIT_DIR GIT_WORK_TREE GIT_CONFIG_NOGLOBAL &&
+		HOME=$(pwd)/alias-config &&
+		export HOME &&
+		mkdir alias-config &&
+		echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
+		mkdir plain-aliased &&
+		cd plain-aliased &&
+		git aliasedinit
+	) &&
+	check_config plain-aliased/.git false unset
+'
+
 test_expect_success 'plain with GIT_WORK_TREE' '
 	if (
 		unset GIT_DIR
-- 
1.7.0.rc1.541.g2da82.dirty