threads / patch / 43497

patchgrep: do not skip unmerged entries when grepping in the working tree.

Subject: [PATCH] grep: do not skip unmerged entries when grepping in the working tree.

## tl;dr

3 messages between Nov 26, 2006 and Nov 26, 2006. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Johannes Sixt· Nov 26, 2006, 14:53 UTC · lore

git-grep misses an instance of a string (after conflict)

I've roughly had the following sequence of operations on a particular file in my project:

1. git-cherry-pick a commit, which resulted in a conflict in in that file
2. edit the file to insert a particular string (which wasn't there before)
3. then:
$ git-grep getSibling -- kdbg/exprwnd.h    # this file had a conflict
$ grep getSibling -- kdbg/exprwnd.h       
    { return static_cast<VarTree*>(getSibling()); }
$ git-update-index kdbg/exprwnd.h
$ git-grep getSibling -- kdbg/exprwnd.h       
kdbg/exprwnd.h:    { return static_cast<VarTree*>(getSibling()); }

As you can see, the first git-grep doesn't find the string, but after the update-index, it does find it.

This is unexpected behavior, in particular since the manual page talks about git-grep to search the working tree. I understand that the conflict may have influenced the behavior, but the manual page is not in line with the behavior. Am I missing something?

-- Hannes
Junio C Hamano· Nov 26, 2006, 20:49 UTC · re: Johannes Sixt · lore

We used to skip unmerged entries, which made sense for grepping in the cached copies, but not for grepping in the files on the working tree.

Noticed by Johannes Sixt.
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
  Johannes Sixt <johannes.sixt@telecom.at> writes:
  > $ git-grep getSibling -- kdbg/exprwnd.h    # this file had a conflict
  > $ grep getSibling -- kdbg/exprwnd.h       
  >     { return static_cast<VarTree*>(getSibling()); }
  > $ git-update-index kdbg/exprwnd.h
  > $ git-grep getSibling -- kdbg/exprwnd.h       
  > kdbg/exprwnd.h:    { return static_cast<VarTree*>(getSibling()); }
  This is because unmerged entries were ignored.
 builtin-grep.c |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)
Show changes to builtin-grep.c +21 −4
diff --git a/builtin-grep.c b/builtin-grep.c
index ad7dc00..9873e3d 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -268,7 +268,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
 	for (i = 0; i < active_nr; i++) {
 		struct cache_entry *ce = active_cache[i];
 		char *name;
-		if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+		if (!S_ISREG(ntohl(ce->ce_mode)))
 			continue;
 		if (!pathspec_matches(paths, ce->name))
 			continue;
@@ -280,12 +280,19 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
 			memcpy(name + 2, ce->name, len + 1);
 		}
 		argv[argc++] = name;
-		if (argc < MAXARGS)
+		if (argc < MAXARGS && !ce_stage(ce))
 			continue;
 		status = exec_grep(argc, argv);
 		if (0 < status)
 			hit = 1;
 		argc = nr;
+		if (ce_stage(ce)) {
+			do {
+				i++;
+			} while (i < active_nr &&
+				 !strcmp(ce->name, active_cache[i]->name));
+			i--; /* compensate for loop control */
+		}
 	}
 	if (argc > nr) {
 		status = exec_grep(argc, argv);
@@ -316,14 +323,24 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 
 	for (nr = 0; nr < active_nr; nr++) {
 		struct cache_entry *ce = active_cache[nr];
-		if (ce_stage(ce) || !S_ISREG(ntohl(ce->ce_mode)))
+		if (!S_ISREG(ntohl(ce->ce_mode)))
 			continue;
 		if (!pathspec_matches(paths, ce->name))
 			continue;
-		if (cached)
+		if (cached) {
+			if (ce_stage(ce))
+				continue;
 			hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
+		}
 		else
 			hit |= grep_file(opt, ce->name);
+		if (ce_stage(ce)) {
+			do {
+				nr++;
+			} while (nr < active_nr &&
+				 !strcmp(ce->name, active_cache[nr]->name));
+			nr--; /* compensate for loop control */
+		}
 	}
 	free_grep_patterns(opt);
 	return hit;
-- 
1.4.4.1.ge3fb
Johannes Sixt· Nov 26, 2006, 21:30 UTC · re: Junio C Hamano · lore

Re: [PATCH] grep: do not skip unmerged entries when grepping in the working tree.

On Sunday 26 November 2006 21:49, Junio C Hamano wrote:
Show 7 quoted lines
> We used to skip unmerged entries, which made sense for grepping
> in the cached copies, but not for grepping in the files on the
> working tree.
>
> Noticed by Johannes Sixt.
>
> Signed-off-by: Junio C Hamano <junkio@cox.net>
This fixes my problem. Thanks!

← back to recent threads