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

[PATCH v2 06/18] ref-filter: reuse parse_ref_filter_atom

From
Olga Telezhnaya <olyatelezhnaya@gmail.com>
Date
Jan 10, 2018, 09:36 UTC
Message-ID
<01020160df6dc548-3213651c-f87e-40f6-80b3-4fe2ee9131be-000000@eu-west-1.amazonses.com>
In-Reply-To
<01020160df6dc499-0e6d11ec-1dcd-4a71-997b-ea231f33fae4-000000@eu-west-1.amazonses.com>

Continue migrating formatting logic from cat-file to ref-filter. Reuse parse_ref_filter_atom for unifying all processes in ref-filter and further reducing of expand_atom_into_fields function.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored by: Jeff King <peff@peff.net>
---
 ref-filter.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 6ca4a671086ee..bb09875e2dbf4 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -100,6 +100,7 @@ static struct used_atom {
 	} u;
 } *used_atom;
 static int used_atom_cnt, need_tagged, need_symref;
+struct expand_data *cat_file_info;
 
 static void color_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *color_value)
 {
@@ -251,6 +252,16 @@ static void objectname_atom_parser(const struct ref_format *format, struct used_
 		die(_("unrecognized %%(objectname) argument: %s"), arg);
 }
 
+static void objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
+{
+	if (!arg)
+		; /* default to normal object size */
+	else if (!strcmp(arg, "disk"))
+		cat_file_info->info.disk_sizep = &cat_file_info->disk_size;
+	else
+		die(_("urecognized %%(objectsize) argument: %s"), arg);
+}
+
 static void refname_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
 {
 	refname_atom_parser_internal(&atom->u.refname, arg, atom->name);
@@ -371,6 +382,14 @@ static struct valid_atom {
 	{ "else" },
 };
 
+static struct valid_atom valid_cat_file_atom[] = {
+	{ "objectname" },
+	{ "objecttype" },
+	{ "objectsize", FIELD_ULONG, objectsize_atom_parser },
+	{ "rest" },
+	{ "deltabase" },
+};
+
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
 
 struct ref_formatting_stack {
@@ -401,20 +420,14 @@ static int is_atom(const char *atom, const char *s, int slen)
 static void expand_atom_into_fields(const char *atom, int len,
 				    struct expand_data *data)
 {
-	if (is_atom("objectname", atom, len))
-		; /* do nothing */
-	else if (is_atom("objecttype", atom, len))
+	if (is_atom("objecttype", atom, len))
 		data->info.typep = &data->type;
 	else if (is_atom("objectsize", atom, len))
 		data->info.sizep = &data->size;
-	else if (is_atom("objectsize:disk", atom, len))
-		data->info.disk_sizep = &data->disk_size;
 	else if (is_atom("rest", atom, len))
 		data->split_on_whitespace = 1;
 	else if (is_atom("deltabase", atom, len))
 		data->info.delta_base_sha1 = data->delta_base_oid.hash;
-	else
-		die("unknown format element: %.*s", len, atom);
 }
 
 /*
@@ -483,6 +496,8 @@ static int parse_ref_filter_atom(const struct ref_format *format,
 		need_tagged = 1;
 	if (!strcmp(valid_atom[i].name, "symref"))
 		need_symref = 1;
+	if (cat_file_info)
+		expand_atom_into_fields(atom, atom_len, cat_file_info);
 	return at;
 }
 
@@ -726,6 +741,7 @@ int verify_ref_format(struct ref_format *format)
 {
 	const char *cp, *sp;
 
+	cat_file_info = format->cat_file_data;
 	format->need_color_reset_at_eol = 0;
 	for (cp = format->format; *cp && (sp = find_next(cp)); ) {
 		const char *color, *ep = strchr(sp, ')');
@@ -735,10 +751,10 @@ int verify_ref_format(struct ref_format *format)
 			return error(_("malformed format string %s"), sp);
 		/* sp points at "%(" and ep points at the closing ")" */
 
-		if (format->cat_file_data)
-			expand_atom_into_fields(sp + 2, ep - sp - 2,
-						format->cat_file_data);
-		else {
+		if (format->cat_file_data) {
+			at = parse_ref_filter_atom(format, valid_cat_file_atom,
+						   ARRAY_SIZE(valid_cat_file_atom), sp + 2, ep);
+		} else {
 			at = parse_ref_filter_atom(format, valid_atom,
 						   ARRAY_SIZE(valid_atom), sp + 2, ep);
 			if (skip_prefix(used_atom[at].name, "color:", &color))

--
https://github.com/git/git/pull/450
Previous: Olga TelezhnayaNext: Olga Telezhnaya
Message 17 of 47 in “cat-file: split expand_atom into 2 functions”
  1. 01/18 cat-file: split expand_atom into 2 functionsOlga Telezhnaya, Jan 10, 2018
  2. 14/18 ref-filter: get rid of expand_atom_into_fieldsOlga Telezhnaya, Jan 10, 2018
  3. 15/18 ref-filter: add is_cat flagOlga Telezhnaya, Jan 10, 2018
  4. 16/18 ref_format: add split_on_whitespace flagOlga Telezhnaya, Jan 10, 2018
  5. 08/18 ref-filter: get rid of gotoOlga Telezhnaya, Jan 10, 2018
  6. 18/18 ref-filter: make cat_file_info independentOlga Telezhnaya, Jan 10, 2018
  7. 02/18 cat-file: reuse struct ref_formatOlga Telezhnaya, Jan 10, 2018
  8. Jeff KingJan 15, 2018
  9. Оля ТележнаяJan 16, 2018
  10. 07/18 cat-file: remove unused codeOlga Telezhnaya, Jan 10, 2018
  11. 10/18 cat-file: get rid of duplicate checkingOlga Telezhnaya, Jan 10, 2018
  12. 17/18 cat-file: move skip_object_info into ref-filterOlga Telezhnaya, Jan 10, 2018
  13. 05/18 cat-file: start migrating to ref-filterOlga Telezhnaya, Jan 10, 2018
  14. 09/18 cat-file: simplify expand_atom functionOlga Telezhnaya, Jan 10, 2018
  15. 13/18 cat-file: start reusing populate_valueOlga Telezhnaya, Jan 10, 2018
  16. 11/18 cat-file: start use ref_array_item structOlga Telezhnaya, Jan 10, 2018
  17. 06/18 ref-filter: reuse parse_ref_filter_atomOlga Telezhnaya, Jan 10, 2018
  18. 12/18 ref-filter: make populate_value globalOlga Telezhnaya, Jan 10, 2018
  19. 04/18 cat-file: move struct expand_data into ref-filterOlga Telezhnaya, Jan 10, 2018
  20. Jeff KingJan 15, 2018
  21. Оля ТележнаяJan 16, 2018
  22. Jeff KingJan 17, 2018
  23. Оля ТележнаяJan 18, 2018
  24. 03/18 ref-filter: make valid_atom as function parameterOlga Telezhnaya, Jan 10, 2018
  25. Jeff KingJan 15, 2018
  26. Оля ТележнаяJan 16, 2018
  27. Jeff KingJan 17, 2018
  28. Christian CouderJan 17, 2018
  29. Оля ТележнаяJan 18, 2018
  30. Оля ТележнаяJan 18, 2018
  31. Christian CouderJan 18, 2018
  32. Оля ТележнаяJan 19, 2018
  33. Christian CouderJan 19, 2018
  34. Christian CouderJan 19, 2018
  35. Оля ТележнаяJan 19, 2018
  36. Christian CouderJan 19, 2018
  37. Jeff KingJan 19, 2018
  38. Christian CouderJan 19, 2018
  39. Junio C HamanoJan 19, 2018
  40. Jeff KingJan 19, 2018
  41. Jeff KingJan 15, 2018
  42. Jeff KingJan 15, 2018
  43. Оля ТележнаяJan 16, 2018
  44. Jeff KingJan 17, 2018
  45. Christian CouderJan 17, 2018
  46. Оля ТележнаяJan 18, 2018
  47. Christian CouderJan 18, 2018

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.