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

[PATCH 1/2] grep: stop using object_array

From
Jeff King <peff@peff.net>
Date
Aug 26, 2013, 19:54 UTC
Message-ID
<20130826195434.GA21074@sigill.intra.peff.net>
In-Reply-To
<20130826195331.GA21051@sigill.intra.peff.net>

We use an object_array to store the set of objects to grep that we received on the command-line. There is no particular reason to use object_array here except that its code was already written, and it contained the elements we needed (though we did not care about mode at all).

However, future patches will need to remember more about the arguments than object_array can provide. Let's use our own custom struct. Thanks to the ALLOC_GROW macro, this is really only a few lines longer (and we even save a few bytes of memory as we do not care about the mode, and we know that we do not have to copy the name strings, as they come from argv).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/grep.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index d3b3b1d..ee47d49 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -23,6 +23,11 @@ static char const * const grep_usage[] = {
 	NULL
 };
 
+struct object_to_grep {
+	struct object *item;
+	const char *name;
+};
+
 static int use_threads = 1;
 
 #ifndef NO_PTHREADS
@@ -493,16 +498,15 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
-			const struct object_array *list)
+			const struct object_to_grep *list, int nr)
 {
 	unsigned int i;
 	int hit = 0;
-	const unsigned int nr = list->nr;
 
 	for (i = 0; i < nr; i++) {
 		struct object *real_obj;
-		real_obj = deref_tag(list->objects[i].item, NULL, 0);
-		if (grep_object(opt, pathspec, real_obj, list->objects[i].name)) {
+		real_obj = deref_tag(list[i].item, NULL, 0);
+		if (grep_object(opt, pathspec, real_obj, list[i].name)) {
 			hit = 1;
 			if (opt->status_only)
 				break;
@@ -628,7 +632,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	int external_grep_allowed__ignored;
 	const char *show_in_pager = NULL, *default_pager = "dummy";
 	struct grep_opt opt;
-	struct object_array list = OBJECT_ARRAY_INIT;
+	struct object_to_grep *list = NULL;
+	int list_nr = 0, list_alloc = 0;
 	const char **paths = NULL;
 	struct pathspec pathspec;
 	struct string_list path_list = STRING_LIST_INIT_NODUP;
@@ -822,7 +827,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			struct object *object = parse_object_or_die(sha1, arg);
 			if (!seen_dashdash)
 				verify_non_filename(prefix, arg);
-			add_object_array(object, arg, &list);
+
+			ALLOC_GROW(list, list_nr+1, list_alloc);
+			list[list_nr].item = object;
+			list[list_nr].name = arg;
+			list_nr++;
 			continue;
 		}
 		if (!strcmp(arg, "--")) {
@@ -833,7 +842,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	}
 
 #ifndef NO_PTHREADS
-	if (list.nr || cached || online_cpus() == 1)
+	if (list_nr || cached || online_cpus() == 1)
 		use_threads = 0;
 #else
 	use_threads = 0;
@@ -861,7 +870,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	pathspec.max_depth = opt.max_depth;
 	pathspec.recursive = 1;
 
-	if (show_in_pager && (cached || list.nr))
+	if (show_in_pager && (cached || list_nr))
 		die(_("--open-files-in-pager only works on the worktree"));
 
 	if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
@@ -889,12 +898,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
 	if (!use_index || untracked) {
 		int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
-		if (list.nr)
+		if (list_nr)
 			die(_("--no-index or --untracked cannot be used with revs."));
 		hit = grep_directory(&opt, &pathspec, use_exclude);
 	} else if (0 <= opt_exclude) {
 		die(_("--[no-]exclude-standard cannot be used for tracked contents."));
-	} else if (!list.nr) {
+	} else if (!list_nr) {
 		if (!cached)
 			setup_work_tree();
 
@@ -902,7 +911,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	} else {
 		if (cached)
 			die(_("both --cached and trees are given."));
-		hit = grep_objects(&opt, &pathspec, &list);
+		hit = grep_objects(&opt, &pathspec, list, list_nr);
 	}
 
 	if (use_threads)
-- 
1.8.4.2.g87d4a77
Previous: Jeff KingNext: Jeff King
Message 4 of 11 in “[PATCHv2] grep: use slash for path delimiter, not colon”
  1. Phil HordAug 26, 2013
  2. Jeff KingAug 26, 2013
  3. Jeff KingAug 26, 2013
  4. 1/2 grep: stop using object_arrayJeff King, Aug 26, 2013
  5. 2/2 grep: use slash for path delimiter, not colonJeff King, Aug 26, 2013
  6. Johannes SixtAug 26, 2013
  7. Phil HordAug 26, 2013
  8. Jeff KingAug 26, 2013
  9. Phil HordAug 26, 2013
  10. Jeff KingAug 26, 2013
  11. Junio C HamanoAug 27, 2013

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.