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

[RFC/PATCH 4/4] grep: obey --textconv for the case rev:path

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Feb 6, 2013, 15:08 UTC
Message-ID
<a2808975f00bac5de0902e6830f254e4b064e8a5.1360162813.git.git@drmicha.warpmail.net>
In-Reply-To
<20130205201106.GA29248@sigill.intra.peff.net>

Make "grep" obey the "--textconv" option also for the object case, i.e. when used with an argument "rev:path".

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 builtin/grep.c | 11 ++++++-----
 object.c       | 26 ++++++++++++++++++++------
 object.h       |  2 ++
 3 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 915c8ef..0f3c4db 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -458,10 +458,10 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
-		       struct object *obj, const char *name)
+		       struct object *obj, const char *name, struct object_context *oc)
 {
 	if (obj->type == OBJ_BLOB)
-		return grep_sha1(opt, obj->sha1, name, 0, NULL);
+		return grep_sha1(opt, obj->sha1, name, 0, oc ? oc->path : NULL);
 	if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
 		struct tree_desc tree;
 		void *data;
@@ -503,7 +503,7 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 	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)) {
+		if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].context)) {
 			hit = 1;
 			if (opt->status_only)
 				break;
@@ -820,14 +820,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
 		unsigned char sha1[20];
+		struct object_context oc;
 		/* Is it a rev? */
-		if (!get_sha1(arg, sha1)) {
+		if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
 			struct object *object = parse_object(sha1);
 			if (!object)
 				die(_("bad object %s"), arg);
 			if (!seen_dashdash)
 				verify_non_filename(prefix, arg);
-			add_object_array(object, arg, &list);
+			add_object_array_with_context(object, arg, &list, xmemdupz(&oc, sizeof(struct object_context)));
 			continue;
 		}
 		if (!strcmp(arg, "--")) {
diff --git a/object.c b/object.c
index 4af3451..1b796c7 100644
--- a/object.c
+++ b/object.c
@@ -245,12 +245,7 @@ int object_list_contains(struct object_list *list, struct object *obj)
 	return 0;
 }
 
-void add_object_array(struct object *obj, const char *name, struct object_array *array)
-{
-	add_object_array_with_mode(obj, name, array, S_IFINVALID);
-}
-
-void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
+static void add_object_array_with_mode_context(struct object *obj, const char *name, struct object_array *array, unsigned mode, struct object_context *context)
 {
 	unsigned nr = array->nr;
 	unsigned alloc = array->alloc;
@@ -265,9 +260,28 @@ void add_object_array_with_mode(struct object *obj, const char *name, struct obj
 	objects[nr].item = obj;
 	objects[nr].name = name;
 	objects[nr].mode = mode;
+	objects[nr].context = context;
 	array->nr = ++nr;
 }
 
+void add_object_array(struct object *obj, const char *name, struct object_array *array)
+{
+	add_object_array_with_mode(obj, name, array, S_IFINVALID);
+}
+
+void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
+{
+	add_object_array_with_mode_context(obj, name, array, mode, NULL);
+}
+
+void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context)
+{
+	if (context)
+		add_object_array_with_mode_context(obj, name, array, context->mode, context);
+	else
+		add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
+}
+
 void object_array_remove_duplicates(struct object_array *array)
 {
 	unsigned int ref, src, dst;
diff --git a/object.h b/object.h
index 6a97b6b..a11d719 100644
--- a/object.h
+++ b/object.h
@@ -13,6 +13,7 @@ struct object_array {
 		struct object *item;
 		const char *name;
 		unsigned mode;
+		struct object_context *context;
 	} *objects;
 };
 
@@ -74,6 +75,7 @@ int object_list_contains(struct object_list *list, struct object *obj);
 /* Object array handling .. */
 void add_object_array(struct object *obj, const char *name, struct object_array *array);
 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode);
+void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context);
 void object_array_remove_duplicates(struct object_array *);
 
 void clear_object_flags(unsigned flags);
-- 
1.8.1.2.752.g32d147e
Previous: Jeff KingNext: Jeff King
Message 29 of 37 in “grep: allow to use textconv filters”
  1. grep: allow to use textconv filtersMichael J Gruber, Feb 4, 2013
  2. Junio C HamanoFeb 4, 2013
  3. Michael J GruberFeb 5, 2013
  4. Jeff KingFeb 5, 2013
  5. Michael J GruberFeb 5, 2013
  6. Jeff KingFeb 5, 2013
  7. 0/4 textconv for show and grepMichael J Gruber, Feb 6, 2013
  8. Junio C HamanoFeb 6, 2013
  9. 1/4 show: obey --textconv for blobsMichael J Gruber, Feb 6, 2013
  10. Junio C HamanoFeb 6, 2013
  11. Jeff KingFeb 6, 2013
  12. Junio C HamanoFeb 6, 2013
  13. Jeff KingFeb 7, 2013
  14. Junio C HamanoFeb 7, 2013
  15. Michael J GruberFeb 7, 2013
  16. Jeff KingFeb 6, 2013
  17. Michael J GruberFeb 7, 2013
  18. Jeff KingFeb 7, 2013
  19. Michael J GruberFeb 7, 2013
  20. Jeff KingFeb 7, 2013
  21. 2/4 cat-file: do not die on --textconv without textconv filtersMichael J Gruber, Feb 6, 2013
  22. Junio C HamanoFeb 6, 2013
  23. Jeff KingFeb 6, 2013
  24. Junio C HamanoFeb 6, 2013
  25. Jeff KingFeb 6, 2013
  26. 3/4 grep: allow to use textconv filtersMichael J Gruber, Feb 6, 2013
  27. Matthieu MoyFeb 6, 2013
  28. Jeff KingFeb 6, 2013
  29. 4/4 grep: obey --textconv for the case rev:pathMichael J Gruber, Feb 6, 2013
  30. Jeff KingFeb 6, 2013
  31. Michael J GruberFeb 7, 2013
  32. Jeff KingFeb 7, 2013
  33. Michael J GruberFeb 7, 2013
  34. Jeff KingFeb 7, 2013
  35. Michael J GruberFeb 7, 2013
  36. Junio C HamanoFeb 7, 2013
  37. Michael J GruberFeb 8, 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.