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

[PATCH v2 4/9] [GSOC] ref-filter: add %(rest) atom

From
ZheNing Hu via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 15, 2021, 13:29 UTC
Message-ID
<5a5b5f78aeeac1f541852dc219d617530fbe87ea.1623763746.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.980.v2.git.1623763746.gitgitgadget@gmail.com>
From: ZheNing Hu <adlternative@gmail.com>

In order to let "cat-file --batch=%(rest)" use the ref-filter interface, add %(rest) atom for ref-filter. "git for-each-ref", "git branch", "git tag" and "git verify-tag" will reject %(rest) by default.

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             | 21 +++++++++++++++++++++
 ref-filter.h             |  5 ++++-
 t/t3203-branch-output.sh |  4 ++++
 t/t6300-for-each-ref.sh  |  4 ++++
 t/t7004-tag.sh           |  4 ++++
 t/t7030-verify-tag.sh    |  4 ++++
 6 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/ref-filter.c b/ref-filter.c
index af8c15aef44d..8868cf98f090 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -157,6 +157,7 @@ enum atom_type {
 	ATOM_IF,
 	ATOM_THEN,
 	ATOM_ELSE,
+	ATOM_REST,
 };
 
 /*
@@ -559,6 +560,15 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
 	return 0;
 }
 
+static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
+			    const char *arg, struct strbuf *err)
+{
+	if (arg)
+		return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
+	format->use_rest = 1;
+	return 0;
+}
+
 static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
 			    const char *arg, struct strbuf *unused_err)
 {
@@ -615,6 +625,7 @@ static struct {
 	[ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
 	[ATOM_THEN] = { "then", SOURCE_NONE },
 	[ATOM_ELSE] = { "else", SOURCE_NONE },
+	[ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
 	/*
 	 * Please update $__git_ref_fieldlist in git-completion.bash
 	 * when you add new atoms
@@ -1006,6 +1017,9 @@ 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 (used_atom[at].atom_type == ATOM_REST)
+			die("this command reject atom %%(%.*s)", (int)(ep - sp - 2), sp + 2);
+
 		if (format->quote_style && used_atom[at].atom_type == ATOM_RAW &&
 		    used_atom[at].u.raw_data.option == RAW_BARE)
 			die(_("--format=%.*s cannot be used with"
@@ -1920,6 +1934,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 			v->handler = else_atom_handler;
 			v->s = xstrdup("");
 			continue;
+		} else if (atom_type == ATOM_REST) {
+			if (ref->rest)
+				v->s = xstrdup(ref->rest);
+			else
+				v->s = xstrdup("");
+			continue;
 		} else
 			continue;
 
@@ -2137,6 +2157,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
 
 	FLEX_ALLOC_STR(ref, refname, refname);
 	oidcpy(&ref->objectname, oid);
+	ref->rest = NULL;
 
 	return ref;
 }
diff --git a/ref-filter.h b/ref-filter.h
index 74fb423fc89f..9dc07476a584 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -38,6 +38,7 @@ struct ref_sorting {
 
 struct ref_array_item {
 	struct object_id objectname;
+	const char *rest;
 	int flag;
 	unsigned int kind;
 	const char *symref;
@@ -76,14 +77,16 @@ struct ref_format {
 	 * verify_ref_format() afterwards to finalize.
 	 */
 	const char *format;
+	const char *rest;
 	int quote_style;
+	int use_rest;
 	int use_color;
 
 	/* Internal state to ref-filter */
 	int need_color_reset_at_eol;
 };
 
-#define REF_FORMAT_INIT { NULL, 0, -1 }
+#define REF_FORMAT_INIT { NULL, NULL, 0, 0, -1 }
 
 /*  Macros for checking --merged and --no-merged options */
 #define _OPT_MERGED_NO_MERGED(option, filter, h) \
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 5325b9f67a00..2780ec8803fd 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git branch with --format=%(rest) must failed' '
+	test_must_fail git branch --format="%(rest)" >actual
+'
+
 test_expect_success 'worktree colors correct' '
 	cat >expect <<-EOF &&
 	* <GREEN>(HEAD detached from fromtag)<RESET>
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index e2867de791e7..8c97c3b877c6 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -1187,6 +1187,10 @@ test_expect_success 'basic atom: head contents:trailers' '
 	test_cmp expect actual.clean
 '
 
+test_expect_success 'basic atom: rest must failed' '
+	test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
+'
+
 test_expect_success 'trailer parsing not fooled by --- line' '
 	git commit --allow-empty -F - <<-\EOF &&
 	this is the subject
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 2f72c5c6883e..9fc4c4323949 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '
 	test_cmp expect actual
 '
 
+test_expect_success 'git tag -l with --format="%(rest)" must failed' '
+	test_must_fail git tag -l --format="%(rest)" "v1*"
+'
+
 test_expect_success "set up color tests" '
 	echo "<RED>v1.0<RESET>" >expect.color &&
 	echo "v1.0" >expect.bare &&
diff --git a/t/t7030-verify-tag.sh b/t/t7030-verify-tag.sh
index 3cefde9602bf..785b32eb88f9 100755
--- a/t/t7030-verify-tag.sh
+++ b/t/t7030-verify-tag.sh
@@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '
 	test_cmp expect actual
 '
 
+test_expect_success GPG 'verifying tag with --format="%(rest)" must failed' '
+	test_must_fail git verify-tag --format="%(rest)" "fourth-signed"
+'
+
 test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
 	test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
 	test_must_be_empty actual-forged
-- 
gitgitgadget
Previous: ZheNing Hu via GitGitGadgetNext: ZheNing Hu via GitGitGadget
Message 30 of 121 in “[GSOC][RFC] cat-file: reuse ref-filter logic”
  1. 0/8 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 12, 2021
  2. 3/8 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 12, 2021
  3. 6/8 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 12, 2021
  4. 8/8 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 12, 2021
  5. Ævar Arnfjörð BjarmasonJun 17, 2021
  6. ZheNing HuJun 17, 2021
  7. 4/8 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 12, 2021
  8. 2/8 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 12, 2021
  9. Ævar Arnfjörð BjarmasonJun 17, 2021
  10. Junio C HamanoJun 17, 2021
  11. ZheNing HuJun 17, 2021
  12. Ævar Arnfjörð BjarmasonJun 17, 2021
  13. ZheNing HuJun 17, 2021
  14. Ævar Arnfjörð BjarmasonJun 18, 2021
  15. Christian CouderJun 18, 2021
  16. 1/8 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 12, 2021
  17. Ævar Arnfjörð BjarmasonJun 17, 2021
  18. Junio C HamanoJun 17, 2021
  19. 5/8 [GSOC] ref-filter: teach get_object() return useful valueZheNing Hu via GitGitGadget, Jun 12, 2021
  20. Christian CouderJun 12, 2021
  21. ZheNing HuJun 13, 2021
  22. Ævar Arnfjörð BjarmasonJun 17, 2021
  23. ZheNing HuJun 17, 2021
  24. 7/8 [GSOC] cat-file: reuse err buf in batch_objet_write()ZheNing Hu via GitGitGadget, Jun 12, 2021
  25. Ævar Arnfjörð BjarmasonJun 17, 2021
  26. ZheNing HuJun 17, 2021
  27. 0/9 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 15, 2021
  28. 1/9 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 15, 2021
  29. 2/9 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 15, 2021
  30. 4/9 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 15, 2021
  31. 3/9 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 15, 2021
  32. 6/9 [GSOC] ref-filter: introduce free_array_item_internal() functionZheNing Hu via GitGitGadget, Jun 15, 2021
  33. Junio C HamanoJun 16, 2021
  34. ZheNing HuJun 17, 2021
  35. 5/9 [GSOC] ref-filter: teach get_object() return useful valueZheNing Hu via GitGitGadget, Jun 15, 2021
  36. Junio C HamanoJun 16, 2021
  37. ZheNing HuJun 17, 2021
  38. 7/9 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 15, 2021
  39. 8/9 [GSOC] cat-file: reuse err buf in batch_objet_write()ZheNing Hu via GitGitGadget, Jun 15, 2021
  40. 9/9 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 15, 2021
  41. Junio C HamanoJun 16, 2021
  42. ZheNing HuJun 17, 2021
  43. Ævar Arnfjörð BjarmasonJun 17, 2021
  44. ZheNing HuJun 17, 2021
  45. 00/10 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 19, 2021
  46. 01/10 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 19, 2021
  47. 04/10 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 19, 2021
  48. 02/10 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 19, 2021
  49. 07/10 [GSOC] ref-filter: introduce free_ref_array_item_value() functionZheNing Hu via GitGitGadget, Jun 19, 2021
  50. 05/10 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 19, 2021
  51. 06/10 [GSOC] ref-filter: pass get_object() return value to their callersZheNing Hu via GitGitGadget, Jun 19, 2021
  52. 09/10 [GSOC] cat-file: reuse err buf in batch_object_write()ZheNing Hu via GitGitGadget, Jun 19, 2021
  53. 08/10 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 19, 2021
  54. Christian CouderJun 21, 2021
  55. ZheNing HuJun 21, 2021
  56. 03/10 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jun 19, 2021
  57. 10/10 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 19, 2021
  58. 00/14 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 22, 2021
  59. 01/14 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 22, 2021
  60. 03/14 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jun 22, 2021
  61. 02/14 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 22, 2021
  62. Bagas SanjayaJun 24, 2021
  63. ZheNing HuJun 24, 2021
  64. 04/14 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 22, 2021
  65. 06/14 [GSOC] ref-filter: pass get_object() return value to their callersZheNing Hu via GitGitGadget, Jun 22, 2021
  66. Bagas SanjayaJun 24, 2021
  67. 05/14 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 22, 2021
  68. 08/14 [GSOC] ref-filter: add cat_file_mode in struct ref_formatZheNing Hu via GitGitGadget, Jun 22, 2021
  69. 07/14 [GSOC] ref-filter: introduce free_ref_array_item_value() functionZheNing Hu via GitGitGadget, Jun 22, 2021
  70. 09/14 [GSOC] ref-filter: modify the error message and value in get_objectZheNing Hu via GitGitGadget, Jun 22, 2021
  71. 10/14 [GSOC] cat-file: add has_object_file() checkZheNing Hu via GitGitGadget, Jun 22, 2021
  72. 11/14 [GSOC] cat-file: change batch_objects parameter nameZheNing Hu via GitGitGadget, Jun 22, 2021
  73. Bagas SanjayaJun 24, 2021
  74. 12/14 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 22, 2021
  75. 13/14 [GSOC] cat-file: reuse err buf in batch_object_write()ZheNing Hu via GitGitGadget, Jun 22, 2021
  76. 14/14 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 22, 2021
  77. 00/15 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 25, 2021
  78. 02/15 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 25, 2021
  79. Bagas SanjayaJun 26, 2021
  80. ZheNing HuJun 27, 2021
  81. 01/15 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 25, 2021
  82. 03/15 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jun 25, 2021
  83. 05/15 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 25, 2021
  84. 04/15 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 25, 2021
  85. 06/15 [GSOC] ref-filter: pass get_object() return value to their callersZheNing Hu via GitGitGadget, Jun 25, 2021
  86. 08/15 [GSOC] ref-filter: add cat_file_mode in struct ref_formatZheNing Hu via GitGitGadget, Jun 25, 2021
  87. 07/15 [GSOC] ref-filter: introduce free_ref_array_item_value() functionZheNing Hu via GitGitGadget, Jun 25, 2021
  88. 10/15 [GSOC] cat-file: add has_object_file() checkZheNing Hu via GitGitGadget, Jun 25, 2021
  89. 09/15 [GSOC] ref-filter: modify the error message and value in get_objectZheNing Hu via GitGitGadget, Jun 25, 2021
  90. 11/15 [GSOC] cat-file: change batch_objects parameter nameZheNing Hu via GitGitGadget, Jun 25, 2021
  91. 12/15 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 25, 2021
  92. Hariom vermaJun 26, 2021
  93. ZheNing HuJun 27, 2021
  94. Hariom vermaJun 26, 2021
  95. ZheNing HuJun 27, 2021
  96. 13/15 [GSOC] cat-file: reuse err buf in batch_object_write()ZheNing Hu via GitGitGadget, Jun 25, 2021
  97. 14/15 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 25, 2021
  98. 15/15 [GSOC] ref-filter: remove grab_oid() functionZheNing Hu via GitGitGadget, Jun 25, 2021
  99. 00/15 [GSOC][RFC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 27, 2021
  100. 02/15 [GSOC] ref-filter: add %(raw) atomZheNing Hu via GitGitGadget, Jun 27, 2021
  101. Bagas SanjayaJun 28, 2021
  102. 01/15 [GSOC] ref-filter: add obj-type check in grab contentsZheNing Hu via GitGitGadget, Jun 27, 2021
  103. 03/15 [GSOC] ref-filter: --format=%(raw) re-support --perlZheNing Hu via GitGitGadget, Jun 27, 2021
  104. 07/15 [GSOC] ref-filter: introduce free_ref_array_item_value() functionZheNing Hu via GitGitGadget, Jun 27, 2021
  105. 04/15 [GSOC] ref-filter: use non-const ref_format in *_atom_parser()ZheNing Hu via GitGitGadget, Jun 27, 2021
  106. 08/15 [GSOC] ref-filter: add cat_file_mode in struct ref_formatZheNing Hu via GitGitGadget, Jun 27, 2021
  107. 05/15 [GSOC] ref-filter: add %(rest) atomZheNing Hu via GitGitGadget, Jun 27, 2021
  108. 06/15 [GSOC] ref-filter: pass get_object() return value to their callersZheNing Hu via GitGitGadget, Jun 27, 2021
  109. 09/15 [GSOC] ref-filter: modify the error message and value in get_objectZheNing Hu via GitGitGadget, Jun 27, 2021
  110. 12/15 [GSOC] cat-file: reuse ref-filter logicZheNing Hu via GitGitGadget, Jun 27, 2021
  111. Hariom vermaJun 28, 2021
  112. ZheNing HuJun 28, 2021
  113. 11/15 [GSOC] cat-file: change batch_objects parameter nameZheNing Hu via GitGitGadget, Jun 27, 2021
  114. 13/15 [GSOC] cat-file: reuse err buf in batch_object_write()ZheNing Hu via GitGitGadget, Jun 27, 2021
  115. 10/15 [GSOC] cat-file: add has_object_file() checkZheNing Hu via GitGitGadget, Jun 27, 2021
  116. 15/15 [GSOC] ref-filter: remove grab_oid() functionZheNing Hu via GitGitGadget, Jun 27, 2021
  117. 14/15 [GSOC] cat-file: re-implement --textconv, --filters optionsZheNing Hu via GitGitGadget, Jun 27, 2021
  118. Junio C HamanoJun 30, 2021
  119. ZheNing HuJul 1, 2021
  120. Junio C HamanoJul 1, 2021
  121. ZheNing HuJul 9, 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.