Skip to contentSkip to conversation

worktree setup: calculate prefix even if no worktree is found

PATCH New Nguyễn Thái Ngọc Duy 1 participant 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 worktree setup: calculate prefix even if no worktree is found

Commit a672ea6 (rehabilitate 'git index-pack' inside the object store - 2008-10-20) worked around a fault in setup_git_directory_gently(). When walking up from inside a git repository, we will return NULL as prefix. We should have returned a correct prefix anyway because cwd has been moved.

---
I missed index-pack in my RUN_SETUP_GENTLY series. So three more patches on top.

 setup.c              |   19 ++++++++-----------
 t/t1500-rev-parse.sh |    2 +-
 t/t1501-worktree.sh  |    5 +++++
 3 files changed, 14 insertions(+), 12 deletions(-)
setup.c +8 -11
index bb3648c..8796c6f 100644
@@ -392,13 +392,16 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	offset = len = strlen(cwd);
 	for (;;) {
 		gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-		if (gitfile_dir) {
-			if (set_git_dir(gitfile_dir))
+		if (gitfile_dir || is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
+			if (gitfile_dir && set_git_dir(gitfile_dir))
 				die("Repository setup failed");
+			inside_git_dir = 0;
+			if (!work_tree_env)
+				inside_work_tree = 1;
+			root_len = offset_1st_component(cwd);
+			git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
 			break;
 		}
-		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
-			break;
 		if (is_git_directory(".")) {
 			inside_git_dir = 1;
 			if (!work_tree_env)
@@ -409,8 +412,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 				set_git_dir(cwd);
 			} else
 				set_git_dir(".");
-			check_repository_format_gently(nongit_ok);
-			return NULL;
+			break;
 		}
 		while (--offset > ceil_offset && cwd[offset] != '/');
 		if (offset <= ceil_offset) {
@@ -426,11 +428,6 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 			die_errno("Cannot change to '%s/..'", cwd);
 	}
 
-	inside_git_dir = 0;
-	if (!work_tree_env)
-		inside_work_tree = 1;
-	root_len = offset_1st_component(cwd);
-	git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
 	if (check_repository_format_gently(nongit_ok))
 		return NULL;
 	if (offset == len)
t/t1500-rev-parse.sh +1 -1
index 48ee077..4ee92c7 100755
@@ -42,7 +42,7 @@ test_rev_parse toplevel false false true '' .git
 cd .git || exit 1
 test_rev_parse .git/ false true false '' .
 cd objects || exit 1
-test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
+test_rev_parse .git/objects/ false true false 'objects/' "$ROOT/.git"
 cd ../.. || exit 1
 
 mkdir -p sub/dir || exit 1
t/t1501-worktree.sh +5 -0
index 9df3012..c72b578 100755
@@ -28,6 +28,11 @@ test_rev_parse() {
 	[ $# -eq 0 ] && return
 }
 
+cd .git/objects || exit 1
+say "subdirectory inside .git"
+test_rev_parse 'inside'       false true false objects/
+cd ../.. || exit 1
+
 EMPTY_TREE=$(git write-tree)
 mkdir -p work/sub/dir || exit 1
 mv .git repo.git || exit 1
Signature · 1 line
1.7.0.1.384.g6abcaa
#1 lore raw

PATCH 2/3 index-pack: trust the prefix returned by setup_git_directory_gently()

---
 builtin/index-pack.c |   21 ++++++---------------
 1 files changed, 6 insertions(+), 15 deletions(-)
builtin/index-pack.c +6 -15
index b4cf8c5..2a6359d 100644
@@ -879,29 +879,20 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	char *index_name_buf = NULL, *keep_name_buf = NULL;
 	struct pack_idx_entry **idx_objects;
 	unsigned char pack_sha1[20];
+	int nongit;
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage(index_pack_usage);
 
 	/*
-	 * We wish to read the repository's config file if any, and
-	 * for that it is necessary to call setup_git_directory_gently().
+	 * We wish to read the repository's config file if any.
 	 * However if the cwd was inside .git/objects/pack/ then we need
 	 * to go back there or all the pack name arguments will be wrong.
-	 * And in that case we cannot rely on any prefix returned by
-	 * setup_git_directory_gently() either.
 	 */
-	{
-		char cwd[PATH_MAX+1];
-		int nongit;
-
-		if (!getcwd(cwd, sizeof(cwd)-1))
-			die("Unable to get current working directory");
-		setup_git_directory_gently(&nongit);
-		git_config(git_index_pack_config, NULL);
-		if (chdir(cwd))
-			die("Cannot come back to cwd");
-	}
+	prefix = setup_git_directory_gently(&nongit);
+	git_config(git_index_pack_config, NULL);
+	if (prefix && chdir(prefix))
+		die("Cannot come back to cwd");
 
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
Signature · 1 line
1.7.0.1.384.g6abcaa
#2 lore raw

PATCH 3/3 index-pack: use RUN_SETUP_GENTLY

In reply to Nguyễn Thái Ngọc Duy
---
 builtin/index-pack.c |    2 --
 git.c                |    2 +-
 2 files changed, 1 insertions(+), 3 deletions(-)
builtin/index-pack.c +0 -2
index 2a6359d..9aa6a13 100644
@@ -879,7 +879,6 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	char *index_name_buf = NULL, *keep_name_buf = NULL;
 	struct pack_idx_entry **idx_objects;
 	unsigned char pack_sha1[20];
-	int nongit;
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage(index_pack_usage);
@@ -889,7 +888,6 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	 * However if the cwd was inside .git/objects/pack/ then we need
 	 * to go back there or all the pack name arguments will be wrong.
 	 */
-	prefix = setup_git_directory_gently(&nongit);
 	git_config(git_index_pack_config, NULL);
 	if (prefix && chdir(prefix))
 		die("Cannot come back to cwd");
git.c +1 -1
index 88408c8..746470f 100644
@@ -332,7 +332,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "grep", cmd_grep, RUN_SETUP_GENTLY | USE_PAGER },
 		{ "hash-object", cmd_hash_object, RUN_SETUP_GENTLY },
 		{ "help", cmd_help, RUN_SETUP_GENTLY },
-		{ "index-pack", cmd_index_pack },
+		{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
 		{ "init", cmd_init_db },
 		{ "init-db", cmd_init_db },
 		{ "log", cmd_log, RUN_SETUP | USE_PAGER },
Signature · 1 line
1.7.0.1.384.g6abcaa