ref-filter: add %(rest) atom

%(rest) is a atom used for cat-file batch mode, which can split the input lines at the first whitespace boundary, all characters before that whitespace are considered to be the object name; characters after that first run of whitespace (i.e., the "rest" of the line) are output in place of the %(rest) atom. In order to let "cat-file --batch=%(rest)" use the ref-filter interface, add %(rest) atom for ref-filter. Introduce the reject_atom() to reject the atom %(rest) for "git for-each-ref", "git branch", "git tag" and "git verify-tag". Reviewed-by: Jacob Keller <jacob.keller@gmail.com> Suggected-by: Jacob Keller <jacob.keller@gmail.com> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: ZheNing Hu <adlternative@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

ZheNing Hu committed Jul 26, 2021 at 03:26 UTC b9dee075eb07713a49922f7e1f90a6a9de5c5e5f
6 files changed +45 -1
ref-filter.c
+25
@@ -157,6 +157,7 @@ enum atom_type {
157 ATOM_IF,
158 ATOM_THEN,
159 ATOM_ELSE,
160 + ATOM_REST,
161 };
162
163 /*
@@ -559,6 +560,15 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
560 return 0;
561 }
562
563 +static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
564 + const char *arg, struct strbuf *err)
565 +{
566 + if (arg)
567 + return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
568 + format->use_rest = 1;
569 + return 0;
570 +}
571 +
572 static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
573 const char *arg, struct strbuf *unused_err)
574 {
@@ -615,6 +625,7 @@ static struct {
625 [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
626 [ATOM_THEN] = { "then", SOURCE_NONE },
627 [ATOM_ELSE] = { "else", SOURCE_NONE },
628 + [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
629 /*
630 * Please update $__git_ref_fieldlist in git-completion.bash
631 * when you add new atoms
@@ -992,6 +1003,11 @@ static const char *find_next(const char *cp)
1003 return NULL;
1004 }
1005
1006 +static int reject_atom(enum atom_type atom_type)
1007 +{
1008 + return atom_type == ATOM_REST;
1009 +}
1010 +
1011 /*
1012 * Make sure the format string is well formed, and parse out
1013 * the used atoms.
@@ -1012,6 +1028,8 @@ int verify_ref_format(struct ref_format *format)
1028 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
1029 if (at < 0)
1030 die("%s", err.buf);
1031 + if (reject_atom(used_atom[at].atom_type))
1032 + die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2);
1033
1034 if ((format->quote_style == QUOTE_PYTHON ||
1035 format->quote_style == QUOTE_SHELL ||
@@ -1931,6 +1949,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
1949 v->handler = else_atom_handler;
1950 v->s = xstrdup("");
1951 continue;
1952 + } else if (atom_type == ATOM_REST) {
1953 + if (ref->rest)
1954 + v->s = xstrdup(ref->rest);
1955 + else
1956 + v->s = xstrdup("");
1957 + continue;
1958 } else
1959 continue;
1960
@@ -2148,6 +2172,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
2172
2173 FLEX_ALLOC_STR(ref, refname, refname);
2174 oidcpy(&ref->objectname, oid);
2175 + ref->rest = NULL;
2176
2177 return ref;
2178 }
ref-filter.h
+4 -1
@@ -38,6 +38,7 @@ struct ref_sorting {
38
39 struct ref_array_item {
40 struct object_id objectname;
41 + const char *rest;
42 int flag;
43 unsigned int kind;
44 const char *symref;
@@ -76,14 +77,16 @@ struct ref_format {
77 * verify_ref_format() afterwards to finalize.
78 */
79 const char *format;
80 + const char *rest;
81 int quote_style;
82 + int use_rest;
83 int use_color;
84
85 /* Internal state to ref-filter */
86 int need_color_reset_at_eol;
87 };
88
86 -#define REF_FORMAT_INIT { NULL, 0, -1 }
89 +#define REF_FORMAT_INIT { .use_color = -1 }
90
91 /* Macros for checking --merged and --no-merged options */
92 #define _OPT_MERGED_NO_MERGED(option, filter, h) \
t/t3203-branch-output.sh
+4
@@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '
340 test_cmp expect actual
341 '
342
343 +test_expect_success 'git branch with --format=%(rest) must fail' '
344 + test_must_fail git branch --format="%(rest)" >actual
345 +'
346 +
347 test_expect_success 'worktree colors correct' '
348 cat >expect <<-EOF &&
349 * <GREEN>(HEAD detached from fromtag)<RESET>
t/t6300-for-each-ref.sh
+4
@@ -1211,6 +1211,10 @@ test_expect_success 'basic atom: head contents:trailers' '
1211 test_cmp expect actual.clean
1212 '
1213
1214 +test_expect_success 'basic atom: rest must fail' '
1215 + test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1216 +'
1217 +
1218 test_expect_success 'trailer parsing not fooled by --- line' '
1219 git commit --allow-empty -F - <<-\EOF &&
1220 this is the subject
t/t7004-tag.sh
+4
@@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '
1998 test_cmp expect actual
1999 '
2000
2001 +test_expect_success 'git tag -l with --format="%(rest)" must fail' '
2002 + test_must_fail git tag -l --format="%(rest)" "v1*"
2003 +'
2004 +
2005 test_expect_success "set up color tests" '
2006 echo "<RED>v1.0<RESET>" >expect.color &&
2007 echo "v1.0" >expect.bare &&
t/t7030-verify-tag.sh
+4
@@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '
194 test_cmp expect actual
195 '
196
197 +test_expect_success GPG 'verifying tag with --format="%(rest)" must fail' '
198 + test_must_fail git verify-tag --format="%(rest)" "fourth-signed"
199 +'
200 +
201 test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
202 test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
203 test_must_be_empty actual-forged