git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] built-in "git grep" (git grip) - quickfix

From
Junio C Hamano <junkio@cox.net>
Date
May 1, 2006, 07:30 UTC
Message-ID
<7vbquirymf.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<7v1wvetfuj.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> writes:
> Right now, it does not understand and/or obey many options grep
> should accept, and the pattern matcher using POSIX.2 regex seems
> to be excruciatingly slow...

I forgot to say that unlike the shell script version you need to give -e in front of the pattern with this version because of the way the option parser is structured. Obviously this needs to be fixed for usability's sake.

But I seem to have managed to fix the "excruciatingly slow" part trivially. regexec() is not re.match() but re.search(), and there is no point looking at each character on the line. Here is a patch.

-- >8 --
diff --git a/builtin-grep.c b/builtin-grep.c
index adcdbaa..6230f44 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -42,26 +42,18 @@ static int grep_buffer(struct grep_opt *
 
 	while (left) {
 		regmatch_t pmatch[10];
-		int flags = 0;
-		char *eol, *cp, ch;
+		char *eol, ch;
 		eol = end_of_line(bol, &left);
 		ch = *eol;
 		*eol = 0;
-		for (cp = bol; cp < eol; cp++) { 
-			int status = regexec(&opt->regexp, cp,
-					     ARRAY_SIZE(pmatch), pmatch,
-					     flags);
-			if (status == REG_NOMATCH)
-				flags |= REG_NOTBOL;
-			else if (status == 0) {
-				/* Hit at this line */
-				printf("%s:", name);
-				if (opt->linenum)
-					printf("%d:", lno);
-				printf("%.*s\n", eol-bol, bol);
-				hit = 1;
-				break;
-			}
+		if (!regexec(&opt->regexp, bol,
+			     ARRAY_SIZE(pmatch), pmatch, 0)) {
+			/* Hit at this line */
+			printf("%s:", name);
+			if (opt->linenum)
+				printf("%d:", lno);
+			printf("%.*s\n", eol-bol, bol);
+			hit = 1;
 		}
 		*eol = ch;
 		bol = eol + 1;
Previous: Junio C HamanoNext: Sam Ravnborg
Message 15 of 21 in “built-in "git grep" (git grip).”
  1. built-in "git grep" (git grip).Junio C Hamano, May 1, 2006
  2. Jakub NarebskiMay 1, 2006
  3. Junio C HamanoMay 1, 2006
  4. Jakub NarebskiMay 1, 2006
  5. Andreas EricssonMay 2, 2006
  6. Jakub NarebskiMay 2, 2006
  7. Junio C HamanoMay 2, 2006
  8. Jakub NarebskiMay 2, 2006
  9. Andreas EricssonMay 2, 2006
  10. Junio C HamanoMay 2, 2006
  11. Linus TorvaldsMay 2, 2006
  12. Junio C HamanoMay 2, 2006
  13. Linus TorvaldsMay 2, 2006
  14. Junio C HamanoMay 3, 2006
  15. built-in "git grep" (git grip) - quickfixJunio C Hamano, May 1, 2006
  16. Sam RavnborgMay 1, 2006
  17. Sam RavnborgMay 1, 2006
  18. Sam RavnborgMay 1, 2006
  19. Linus TorvaldsMay 1, 2006
  20. builtin-grep: wildcard pathspec fixesJunio C Hamano, May 1, 2006
  21. builtin-grep: do not use setup_revisions()Junio C Hamano, May 1, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.