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

[PATCH RFC v2 18/25] ref-filter: make valid_atom general again

From
Olga Telezhnaya <olyatelezhnaya@gmail.com>
Date
Feb 5, 2018, 11:27 UTC
Message-ID
<0102016165b8b466-7ec0e6e3-0d33-4665-be2c-b1c12bcb37b5-000000@eu-west-1.amazonses.com>
In-Reply-To
<0102016165b8b3c4-54efe4c4-6d19-435d-b5b9-6c727771353b-000000@eu-west-1.amazonses.com>

Stop using valid_cat_file_atom, making the code more general. Further commits will contain some tests, docs and support of new features.

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 | 34 +++++++++-------------------------
 1 file changed, 9 insertions(+), 25 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index a65a90790fd2c..3f3583ac515a5 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -359,8 +359,8 @@ static struct valid_atom {
 	void (*parser)(const struct ref_format *format, struct used_atom *atom, const char *arg);
 } valid_atom[] = {
 	{ "refname" , FIELD_STR, refname_atom_parser },
-	{ "objecttype" },
-	{ "objectsize", FIELD_ULONG },
+	{ "objecttype", FIELD_STR, objecttype_atom_parser },
+	{ "objectsize", FIELD_ULONG, objectsize_atom_parser },
 	{ "objectname", FIELD_STR, objectname_atom_parser },
 	{ "tree" },
 	{ "parent" },
@@ -397,12 +397,6 @@ static struct valid_atom {
 	{ "if", FIELD_STR, if_atom_parser },
 	{ "then" },
 	{ "else" },
-};
-
-static struct valid_atom valid_cat_file_atom[] = {
-	{ "objectname" },
-	{ "objecttype", FIELD_STR, objecttype_atom_parser },
-	{ "objectsize", FIELD_ULONG, objectsize_atom_parser },
 	{ "rest" },
 	{ "deltabase", FIELD_STR, deltabase_atom_parser },
 };
@@ -432,7 +426,6 @@ struct atom_value {
  * Used to parse format string and sort specifiers
  */
 static int parse_ref_filter_atom(const struct ref_format *format,
-				 const struct valid_atom *valid_atom, int n_atoms,
 				 const char *atom, const char *ep)
 {
 	const char *sp;
@@ -462,13 +455,13 @@ static int parse_ref_filter_atom(const struct ref_format *format,
 	atom_len = (arg ? arg : ep) - sp;
 
 	/* Is the atom a valid one? */
-	for (i = 0; i < n_atoms; i++) {
+	for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
 		int len = strlen(valid_atom[i].name);
 		if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
 			break;
 	}
 
-	if (n_atoms <= i)
+	if (ARRAY_SIZE(valid_atom) <= i)
 		die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
 
 	/* Add it in, including the deref prefix */
@@ -762,15 +755,9 @@ 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 (is_cat)
-			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))
-				format->need_color_reset_at_eol = !!strcmp(color, "reset");
-		}
+		at = parse_ref_filter_atom(format, sp + 2, ep);
+		if (skip_prefix(used_atom[at].name, "color:", &color))
+			format->need_color_reset_at_eol = !!strcmp(color, "reset");
 
 		cp = ep + 1;
 	}
@@ -2232,9 +2219,7 @@ int format_ref_array_item(struct ref_array_item *info,
 		if (cp < sp)
 			append_literal(cp, sp, &state);
 		if (get_ref_atom_value(info,
-				       parse_ref_filter_atom(format, valid_atom,
-							     ARRAY_SIZE(valid_atom),
-							     sp + 2, ep),
+				       parse_ref_filter_atom(format, sp + 2, ep),
 				       &atomv))
 			return -1;
 		atomv->handler(atomv, &state);
@@ -2287,8 +2272,7 @@ static int parse_sorting_atom(const char *atom)
 	 */
 	struct ref_format dummy = REF_FORMAT_INIT;
 	const char *end = atom + strlen(atom);
-	return parse_ref_filter_atom(&dummy, valid_atom,
-				     ARRAY_SIZE(valid_atom), atom, end);
+	return parse_ref_filter_atom(&dummy, atom, end);
 }
 
 /*  If no sorting option is given, use refname to sort as default */

--
https://github.com/git/git/pull/452
Previous: Olga TelezhnayaNext: Olga Telezhnaya
Message 15 of 26 in “ref-filter: get rid of goto”
  1. 01/25 ref-filter: get rid of gotoOlga Telezhnaya, Feb 5, 2018
  2. 08/25 ref-filter: reuse parse_ref_filter_atom()Olga Telezhnaya, Feb 5, 2018
  3. 05/25 cat-file: move struct expand_data into ref-filterOlga Telezhnaya, Feb 5, 2018
  4. 22/25 ref-filter: work with objectsize:diskOlga Telezhnaya, Feb 5, 2018
  5. 13/25 ref-filter: get rid of mark_atom_in_object_info()Olga Telezhnaya, Feb 5, 2018
  6. 06/25 cat-file: split expand_atom() into 2 functionsOlga Telezhnaya, Feb 5, 2018
  7. 16/25 cat-file: move skip_object_info into ref-filterOlga Telezhnaya, Feb 5, 2018
  8. 11/25 ref-filter: rename field in ref_array_item stuctOlga Telezhnaya, Feb 5, 2018
  9. 04/25 ref-filter: make valid_atom as function parameterOlga Telezhnaya, Feb 5, 2018
  10. 03/25 cat-file: reuse struct ref_formatOlga Telezhnaya, Feb 5, 2018
  11. 25/25 cat-file: update of docsOlga Telezhnaya, Feb 5, 2018
  12. 24/25 cat-file: tests for new atoms addedOlga Telezhnaya, Feb 5, 2018
  13. 15/25 ref_filter: add is_atom_used functionOlga Telezhnaya, Feb 5, 2018
  14. 10/25 ref-filter: make populate_value() globalOlga Telezhnaya, Feb 5, 2018
  15. 18/25 ref-filter: make valid_atom general againOlga Telezhnaya, Feb 5, 2018
  16. 02/25 ref-filter: add return value to some functionsOlga Telezhnaya, Feb 5, 2018
  17. 20/25 ref-filter: make populate_value() internal againOlga Telezhnaya, Feb 5, 2018
  18. 19/25 cat-file: reuse printing logic from ref-filterOlga Telezhnaya, Feb 5, 2018
  19. 12/25 cat-file: start reusing populate_value()Olga Telezhnaya, Feb 5, 2018
  20. 14/25 ref-filter: add is_cat flagOlga Telezhnaya, Feb 5, 2018
  21. Junio C HamanoFeb 5, 2018
  22. 21/25 ref-filter: unifying formatting of cat-file optsOlga Telezhnaya, Feb 5, 2018
  23. 09/25 cat-file: start use ref_array_item structOlga Telezhnaya, Feb 5, 2018
  24. 17/25 ref-filter: make cat_file_info independentOlga Telezhnaya, Feb 5, 2018
  25. 23/25 for-each-ref: tests for new atoms addedOlga Telezhnaya, Feb 5, 2018
  26. 07/25 cat-file: start migrating formatting to ref-filterOlga Telezhnaya, Feb 5, 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.