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

[PATCH 4/8] [GSOC] ref-filter: clean up verify_ref_format()

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 25, 2021, 09:08 UTC
Message-ID
<54c95b0386ea7238b09931cecabc71dc1b9c0d21.1629882532.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1025.git.1629882532.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

Because we may not use the real ref_format when using parse_ref_filter_atom() before, we have to perform some atom checks in verify_ref_format() (instead of the parsing function of each atom), which is very inelegant.

Since we have eliminated dummy ref_format, we can freely put some of the check logic from verify_ref_format() to their corresponding atom parsing functions.

Introduce raw_atom_reject_quote_style() to check if there is any incompatibility quote style with the %(raw) atom.

This can make the logic of the program more concise.
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Hariom Verma <hariom18599@gmail.com>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 ref-filter.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index f4fa3102da6..e42cd738c32 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -242,6 +242,7 @@ static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
 	 */
 	if (!want_color(format->use_color))
 		color_parse("", atom->u.color);
+	format->need_color_reset_at_eol = !!strcmp(color_value, "reset");
 	return 0;
 }
 
@@ -432,12 +433,29 @@ static int contents_atom_parser(struct ref_format *format, struct used_atom *ato
 	return 0;
 }
 
+static int raw_atom_reject_quote_style(int quote_style) {
+	switch (quote_style) {
+	case QUOTE_PYTHON:
+	case QUOTE_SHELL:
+	case QUOTE_TCL:
+		return 1;
+	case QUOTE_PERL:
+	case 0:
+		return 0;
+	default:
+		BUG("unknown quote style %d", quote_style);
+	}
+}
+
 static int raw_atom_parser(struct ref_format *format, struct used_atom *atom,
 				const char *arg, struct strbuf *err)
 {
-	if (!arg)
+	if (!arg) {
 		atom->u.raw_data.option = RAW_BARE;
-	else if (!strcmp(arg, "size"))
+		if (raw_atom_reject_quote_style(format->quote_style))
+			return strbuf_addf_ret(err, -1, _("--format=%%(raw) cannot be used with"
+						   "--python, --shell, --tcl"));
+	} else if (!strcmp(arg, "size"))
 		atom->u.raw_data.option = RAW_LENGTH;
 	else
 		return strbuf_addf_ret(err, -1, _("unrecognized %%(raw) argument: %s"), arg);
@@ -567,7 +585,7 @@ static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
 	if (arg)
 		return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
 	format->use_rest = 1;
-	return 0;
+	return strbuf_addf_ret(err, -1, _("this command reject atom %%(rest)"));
 }
 
 static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
@@ -1004,11 +1022,6 @@ static const char *find_next(const char *cp)
 	return NULL;
 }
 
-static int reject_atom(enum atom_type atom_type)
-{
-	return atom_type == ATOM_REST;
-}
-
 /*
  * Make sure the format string is well formed, and parse out
  * the used atoms.
@@ -1020,7 +1033,7 @@ int verify_ref_format(struct ref_format *format)
 	format->need_color_reset_at_eol = 0;
 	for (cp = format->format; *cp && (sp = find_next(cp)); ) {
 		struct strbuf err = STRBUF_INIT;
-		const char *color, *ep = strchr(sp, ')');
+		const char *ep = strchr(sp, ')');
 		int at;
 
 		if (!ep)
@@ -1029,20 +1042,8 @@ int verify_ref_format(struct ref_format *format)
 		at = parse_ref_filter_atom(format, sp + 2, ep, &err);
 		if (at < 0)
 			die("%s", err.buf);
-		if (reject_atom(used_atom[at].atom_type))
-			die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2);
-
-		if ((format->quote_style == QUOTE_PYTHON ||
-		     format->quote_style == QUOTE_SHELL ||
-		     format->quote_style == QUOTE_TCL) &&
-		     used_atom[at].atom_type == ATOM_RAW &&
-		     used_atom[at].u.raw_data.option == RAW_BARE)
-			die(_("--format=%.*s cannot be used with"
-			      "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2);
 		cp = ep + 1;
 
-		if (skip_prefix(used_atom[at].name, "color:", &color))
-			format->need_color_reset_at_eol = !!strcmp(color, "reset");
 		strbuf_release(&err);
 	}
 	if (format->need_color_reset_at_eol && !want_color(format->use_color))
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: ZheNing Hu via GitGitGadget
Message 5 of 9 in “[GSOC] ref-filter: eliminate dummy ref_format to make the code cleaner”
  1. 0/8 [GSOC] ref-filter: eliminate dummy ref_format to make the code cleanerZheNing Hu via GitGitGadget, Aug 25, 2021
  2. 1/8 [GSOC] ref-filter: use list api to replace ref_sorting linked listZheNing Hu via GitGitGadget, Aug 25, 2021
  3. 2/8 [GSOC] ref-filter: let parse_sorting_atom() use real ref_formatZheNing Hu via GitGitGadget, Aug 25, 2021
  4. 3/8 [GSOC] ref-filter: add ref_format to ref_array_itemZheNing Hu via GitGitGadget, Aug 25, 2021
  5. 4/8 [GSOC] ref-filter: clean up verify_ref_format()ZheNing Hu via GitGitGadget, Aug 25, 2021
  6. 5/8 [GSOC] ref-filter: introduce symref_atom_parser()ZheNing Hu via GitGitGadget, Aug 25, 2021
  7. 6/8 [GSOC] ref-filter: remove grab_oid() functionZheNing Hu via GitGitGadget, Aug 25, 2021
  8. 7/8 [GSOC] ref-filter: add deref member to struct used_atomZheNing Hu via GitGitGadget, Aug 25, 2021
  9. 8/8 [GSOC] ref-filter: move need_symref, need_tagged into ref_formatZheNing Hu via GitGitGadget, Aug 25, 2021

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.