Skip to contentSkip to conversation

Remove .git auto detection from setup_git_env()

PATCH In review Nguyễn Thái Ngọc Duy 4 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Remove .git auto detection from setup_git_env()

When GIT_DIR environment variable is not specified, .git will be searched if a repository is needed. Currently this can be done in two
places: setup_git_directory_gently() and setup_git_env().

The one in setup_git_env() is no longer correct and should IMHO have been removed since the introduction of setup_git_directory_gently() in d288a70. Having two ways of auto detection may lead to obscure errors because .git may be misdetected by setup_git_env(), automatically called via git_path(), which is all over the place.

This patch makes setup_git_env() die if GIT_DIR is not explictly set. That's setup_git_directory_gently()'s job. If you ever want to touch things inside $GIT_DIR, you should have already called setup_git_directory_gently().

This patch breaks commands (in a good way) and obviously not for mainline. I still have to go through "make test" to see how many are impacted. But I think this is a good change. Am I missing something?

---
 environment.c |    4 +---
 setup.c       |    4 +++-
 2 files changed, 4 insertions(+), 4 deletions(-)
environment.c +1 -3
index 739ec27..b609569 100644
@@ -67,9 +67,7 @@ static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
 	if (!git_dir)
-		git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-	if (!git_dir)
-		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
+		die("GIT_DIR not properly set");
 	git_object_dir = getenv(DB_ENVIRONMENT);
 	if (!git_object_dir) {
 		git_object_dir = xmalloc(strlen(git_dir) + 9);
setup.c +3 -1
index 710e2f3..ae1ba52 100644
@@ -396,8 +396,10 @@ const char *setup_git_directory_gently(int *nongit_ok)
 				die("Repository setup failed");
 			break;
 		}
-		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
+		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
+			set_git_dir(DEFAULT_GIT_DIR_ENVIRONMENT);
 			break;
+		}
 		if (is_git_directory(".")) {
 			inside_git_dir = 1;
 			if (!work_tree_env)
Signature · 1 line
1.7.0.rc0.54.gd33ef
#1 lore raw
Quoted 10 lines · Nguyễn Thái Ngọc Duy

When GIT_DIR environment variable is not specified, .git will be searched if a repository is needed. Currently this can be done in two
places: setup_git_directory_gently() and setup_git_env().

The one in setup_git_env() is no longer correct and should IMHO have been removed since the introduction of setup_git_directory_gently() in d288a70. Having two ways of auto detection may lead to obscure errors because .git may be misdetected by setup_git_env(), automatically called via git_path(), which is all over the place.

Quoted 8 lines

This patch makes setup_git_env() die if GIT_DIR is not explictly set. That's setup_git_directory_gently()'s job. If you ever want to touch things inside $GIT_DIR, you should have already called setup_git_directory_gently().

This patch breaks commands (in a good way) and obviously not for mainline. I still have to go through "make test" to see how many are impacted. But I think this is a good change. Am I missing something?

It has been a while since I looked at this code, but I think this is a good direction forward. I remember trying something like this and getting discouraged at all of the ensuing breakage. So if you can track down all of the fallouts and fix them, I think we will be better off in the long run.

-Peff

#2 lore raw
Quoted 5 lines · Jeff King

It has been a while since I looked at this code, but I think this is a good direction forward. I remember trying something like this and getting discouraged at all of the ensuing breakage. So if you can track down all of the fallouts and fix them, I think we will be better off in the long run.

I agree.

This topic started from "We broke 'git grep' in .git directory" and I think it is the sanest to revert 3081623 (grep --no-index: allow use of "git grep" outside a git repository, 2010-01-15) which nobody has used so far in any released version of git, until we sort this out at least.

Every time we touch work-tree related codepath (like René's patch does) we seem to inadvertently break something else. It's too late for that kind of fixes for this cycle and "grep --no-index" should go.

-- >8 --
Subject: [PATCH] Revert 308162372d0aa202ff45743e02253e20a4fac4d7

It seems that we have bad interaction with the code related to GIT_WORK_TREE and "grep --no-index", and broke running grep inside the .git directory. For now, just revert it and resurrect it after 1.7.0 ships.

---
 Documentation/RelNotes-1.7.0.txt |    3 --
 builtin-grep.c                   |   33 ------------------------
 t/t7002-grep.sh                  |   52 --------------------------------------
 3 files changed, 0 insertions(+), 88 deletions(-)
Documentation/RelNotes-1.7.0.txt +0 -3
index 255666f..f632662 100644
@@ -133,9 +133,6 @@ Updates since v1.6.6
  * "git grep" does not rely on external grep anymore.  It can use more than
    one threads to accelerate the operation.
 
- * "git grep" learned "--no-index" option, to search inside contents that
-   are not managed by git.
-
  * "git grep" learned "--quiet" option.
 
  * "git log" and friends learned "--glob=heads/*" syntax that is a more
builtin-grep.c +0 -33
index 0ef849c..bff1e68 100644
@@ -14,7 +14,6 @@
 #include "userdiff.h"
 #include "grep.h"
 #include "quote.h"
-#include "dir.h"
 
 #ifndef NO_PTHREADS
 #include "thread-utils.h"
@@ -646,24 +645,6 @@ static int grep_object(struct grep_opt *opt, const char **paths,
 	die("unable to grep from object of type %s", typename(obj->type));
 }
 
-static int grep_directory(struct grep_opt *opt, const char **paths)
-{
-	struct dir_struct dir;
-	int i, hit = 0;
-
-	memset(&dir, 0, sizeof(dir));
-	setup_standard_excludes(&dir);
-
-	fill_directory(&dir, paths);
-	for (i = 0; i < dir.nr; i++) {
-		hit |= grep_file(opt, dir.entries[i]->name);
-		if (hit && opt->status_only)
-			break;
-	}
-	free_grep_patterns(opt);
-	return hit;
-}
-
 static int context_callback(const struct option *opt, const char *arg,
 			    int unset)
 {
@@ -762,8 +743,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		OPT_BOOLEAN(0, "cached", &cached,
 			"search in index instead of in the work tree"),
-		OPT_BOOLEAN(0, "index", &use_index,
-			"--no-index finds in contents not managed by git"),
 		OPT_GROUP(""),
 		OPT_BOOLEAN('v', "invert-match", &opt.invert,
 			"show non-matching lines"),
@@ -950,18 +929,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		paths[1] = NULL;
 	}
 
-	if (!use_index) {
-		int hit;
-		if (cached)
-			die("--cached cannot be used with --no-index.");
-		if (list.nr)
-			die("--no-index cannot be used with revs.");
-		hit = grep_directory(&opt, paths);
-		if (use_threads)
-			hit |= wait_all();
-		return !hit;
-	}
-
 	if (!list.nr) {
 		int hit;
 		if (!cached)
t/t7002-grep.sh +0 -52

56 lines not on this page · raw message

Signature · 1 line
1.7.0.rc1.204.gb96e5
#3 lore raw
Quoted 4 lines · Junio C Hamano

This topic started from "We broke 'git grep' in .git directory" and I think it is the sanest to revert 3081623 (grep --no-index: allow use of "git grep" outside a git repository, 2010-01-15) which nobody has used so far in any released version of git, until we sort this out at least.

Sorry; spoke too fast. We'd also need to revert the one to git.c (7e62265 (grep: prepare to run outside of a work tree, 2010-01-15) as well.

builtin-grep.c +0 -7
index bff1e68..26d4deb 100644
@@ -739,7 +739,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	const char **paths = NULL;
 	int i;
 	int dummy;
-	int nongit = 0, use_index = 1;
 	struct option options[] = {
 		OPT_BOOLEAN(0, "cached", &cached,
 			"search in index instead of in the work tree"),
@@ -825,8 +824,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	prefix = setup_git_directory_gently(&nongit);
-
 	/*
 	 * 'git grep -h', unlike 'git grep -h <pattern>', is a request
 	 * to show usage information and exit.
@@ -864,10 +861,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			     PARSE_OPT_STOP_AT_NON_OPTION |
 			     PARSE_OPT_NO_INTERNAL_HELP);
 
-	if (use_index && nongit)
-		/* die the same way as if we did it at the beginning */
-		setup_git_directory();
-
 	/* First unrecognized non-option token */
 	if (argc > 0 && !opt.pattern_list) {
 		append_grep_pattern(&opt, argv[0], "command line", 0,
git.c +1 -1
index b3e23f1..4c3028c 100644
@@ -317,7 +317,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "fsck-objects", cmd_fsck, RUN_SETUP },
 		{ "gc", cmd_gc, RUN_SETUP },
 		{ "get-tar-commit-id", cmd_get_tar_commit_id },
-		{ "grep", cmd_grep, USE_PAGER },
+		{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
 		{ "hash-object", cmd_hash_object },
 		{ "help", cmd_help },
 		{ "index-pack", cmd_index_pack },
#4 lore raw

Am 05.02.2010 18:27, schrieb Junio C Hamano:

Quoted 10 lines
Quoted 4 lines · Junio C Hamano

This topic started from "We broke 'git grep' in .git directory" and I think it is the sanest to revert 3081623 (grep --no-index: allow use of "git grep" outside a git repository, 2010-01-15) which nobody has used so far in any released version of git, until we sort this out at least.

Sorry; spoke too fast. We'd also need to revert the one to git.c (7e62265 (grep: prepare to run outside of a work tree, 2010-01-15) as well.

git.c static void handle_internal_command(int argc, const char **argv) · line 323

Quoted 8 lines
git.c +1 -1
 		{ "fsck-objects", cmd_fsck, RUN_SETUP },
 		{ "gc", cmd_gc, RUN_SETUP },
 		{ "get-tar-commit-id", cmd_get_tar_commit_id },
-		{ "grep", cmd_grep, USE_PAGER },
+		{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
 		{ "hash-object", cmd_hash_object },
 		{ "help", cmd_help },
 		{ "index-pack", cmd_index_pack },

There is one other command with only the USE_PAGER flag turned on, which also seems to have been broken by the change to remove the RUN_SETUP
flag: shortlog. But that was done in March 2008 (abe549e1, "shortlog:
do not require to run from inside a git repository"), so it's no regression.

René