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

[PATCH 2/4] Unify code paths of threaded greps

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jun 12, 2010, 16:32 UTC
Message-ID
<20100612163211.GB5657@burratino>
In-Reply-To
<20100612162945.GB1406@burratino>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

There were three awfully similar code paths ending the threaded grep. It is better to avoid duplicated code, though.

This change might very well prevent a race, where the grep patterns were free()d before waiting that all threads finished.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/grep.c |   22 +++++-----------------
 1 files changed, 5 insertions(+), 17 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 5b8879f..2111212 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -590,7 +590,6 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 		if (hit && opt->status_only)
 			break;
 	}
-	free_grep_patterns(opt);
 	return hit;
 }
 
@@ -708,7 +707,6 @@ static int grep_directory(struct grep_opt *opt, const char **paths)
 		if (hit && opt->status_only)
 			break;
 	}
-	free_grep_patterns(opt);
 	return hit;
 }
 
@@ -1019,32 +1017,22 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	}
 
 	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;
+	} else if (!list.nr) {
 		if (!cached)
 			setup_work_tree();
 
 		hit = grep_cache(&opt, paths, cached);
-		if (use_threads)
-			hit |= wait_all();
-		return !hit;
+	} else {
+		if (cached)
+			die("both --cached and trees are given.");
+		hit = grep_objects(&opt, paths, &list);
 	}
 
-	if (cached)
-		die("both --cached and trees are given.");
-	hit = grep_objects(&opt, paths, &list);
-
 	if (use_threads)
 		hit |= wait_all();
 	free_grep_patterns(&opt);
-- 
1.7.1
Previous: Jonathan NiederNext: Jonathan Nieder
Message 10 of 20 in “Teach 'git grep' about --open-files-in-pager=[<pager>]”
  1. 0/2 Teach 'git grep' about --open-files-in-pager=[<pager>]Jonathan Nieder, Jun 5, 2010
  2. 1/2 grep: Add the option '--open-files-in-pager'Jonathan Nieder, Jun 5, 2010
  3. Jonathan NiederJun 5, 2010
  4. 2/2 grep -O: allow optional argument specifying the pager (or editor)Jonathan Nieder, Jun 5, 2010
  5. Johannes SchindelinJun 5, 2010
  6. Jonathan NiederJun 12, 2010
  7. Johannes SchindelinJun 12, 2010
  8. 0/4 Teach 'git grep' about --open-files-in-pager=[<pager>]Jonathan Nieder, Jun 12, 2010
  9. 1/4 grep: refactor grep_objects loop into its own functionJonathan Nieder, Jun 12, 2010
  10. 2/4 Unify code paths of threaded grepsJonathan Nieder, Jun 12, 2010
  11. 3/4 grep: Add the option '--open-files-in-pager'Jonathan Nieder, Jun 12, 2010
  12. 4/4 grep -O: allow optional argument specifying the pager (or editor)Jonathan Nieder, Jun 12, 2010
  13. Paolo BonziniJun 12, 2010
  14. Junio C HamanoJun 13, 2010
  15. Paolo BonziniJun 13, 2010
  16. Jonathan NiederJun 14, 2010
  17. Johannes SchindelinJun 12, 2010
  18. Jonathan NiederJun 12, 2010
  19. Paolo BonziniJun 12, 2010
  20. Jonathan NiederJun 12, 2010

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.