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

[PATCH RFC 14/24] ref_filter: add is_rest_atom_used function

From
Olga Telezhnaya <olyatelezhnaya@gmail.com>
Date
Jan 26, 2018, 19:43 UTC
Message-ID
<0102016133ff3b3d-58cb3635-e54a-4473-a12c-78351f613c2c-000000@eu-west-1.amazonses.com>
In-Reply-To
<0102016133ff3a86-44d354ec-13c6-4c38-bc75-1ba4422db5a7-000000@eu-west-1.amazonses.com>

Delete all items related to split_on_whitespace from ref-filter and add new function for handling the logic. Now cat-file could invoke that function to implementing its logic.

Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored by: Jeff King <peff@peff.net>
---
 builtin/cat-file.c |  8 +++-----
 ref-filter.c       | 17 +++++++++++++++--
 ref-filter.h       | 10 +++-------
 3 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 8f34d085962ed..601a87d9b5f7c 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -381,8 +381,7 @@ static int batch_objects(struct batch_options *opt)
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct expand_data data;
-	int save_warning;
-	int retval = 0;
+	int save_warning, is_rest, retval = 0;
 
 	if (!opt->format.format)
 		opt->format.format = "%(objectname) %(objecttype) %(objectsize)";
@@ -396,8 +395,6 @@ static int batch_objects(struct batch_options *opt)
 	opt->format.cat_file_data = &data;
 	opt->format.is_cat = 1;
 	verify_ref_format(&opt->format);
-	if (opt->cmdmode)
-		data.split_on_whitespace = 1;
 
 	if (opt->all_objects) {
 		struct object_info empty = OBJECT_INFO_INIT;
@@ -436,9 +433,10 @@ static int batch_objects(struct batch_options *opt)
 	 */
 	save_warning = warn_on_object_refname_ambiguity;
 	warn_on_object_refname_ambiguity = 0;
+	is_rest = opt->cmdmode || is_rest_atom_used(&opt->format);
 
 	while (strbuf_getline(&buf, stdin) != EOF) {
-		if (data.split_on_whitespace) {
+		if (is_rest) {
 			/*
 			 * Split at first whitespace, tying off the beginning
 			 * of the string and saving the remainder (or NULL) in
diff --git a/ref-filter.c b/ref-filter.c
index 3b61e790e90d1..51da76dc21136 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -494,8 +494,6 @@ 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 (is_cat && !strcmp(valid_atom[i].name, "rest"))
-		cat_file_info->split_on_whitespace = 1;
 	return at;
 }
 
@@ -731,6 +729,21 @@ static const char *find_next(const char *cp)
 	return NULL;
 }
 
+/* Search for atom "rest" in given format. */
+int is_rest_atom_used(const struct ref_format *format)
+{
+	const char *cp, *sp;
+	for (cp = format->format; *cp && (sp = find_next(cp)); ) {
+		const char *ep = strchr(sp, ')');
+		int atom_len = ep - sp - 2;
+		sp += 2;
+		if (atom_len == 4 && !memcmp(sp, "rest", atom_len))
+			return 1;
+		cp = ep + 1;
+	}
+	return 0;
+}
+
 /*
  * Make sure the format string is well formed, and parse out
  * the used atoms.
diff --git a/ref-filter.h b/ref-filter.h
index c848370bed84a..276de387f3bd0 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -87,13 +87,6 @@ struct expand_data {
 	const char *rest;
 	struct object_id delta_base_oid;
 
-	/*
-	 * Whether to split the input on whitespace before feeding it to
-	 * get_sha1; this is decided during the mark_query phase based on
-	 * whether we have a %(rest) token in our format.
-	 */
-	int split_on_whitespace;
-
 	/*
 	 * After a mark_query run, this object_info is set up to be
 	 * passed to sha1_object_info_extended. It will point to the data
@@ -182,4 +175,7 @@ void pretty_print_ref(const char *name, const unsigned char *sha1,
 /* Fill the values of request and prepare all data for final string creation */
 int populate_value(struct ref_array_item *ref);
 
+/* Search for atom "rest" in given format. */
+int is_rest_atom_used(const struct ref_format *format);
+
 #endif /*  REF_FILTER_H  */

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