Skip to content

Commit b9dee07

Browse files
adlternativegitster
authored andcommitted
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 <[email protected]> Suggected-by: Jacob Keller <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Hariom Verma <[email protected]> Signed-off-by: ZheNing Hu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e85fcb3 commit b9dee07

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

ref-filter.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ enum atom_type {
157157
ATOM_IF,
158158
ATOM_THEN,
159159
ATOM_ELSE,
160+
ATOM_REST,
160161
};
161162

162163
/*
@@ -559,6 +560,15 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
559560
return 0;
560561
}
561562

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+
562572
static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
563573
const char *arg, struct strbuf *unused_err)
564574
{
@@ -615,6 +625,7 @@ static struct {
615625
[ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
616626
[ATOM_THEN] = { "then", SOURCE_NONE },
617627
[ATOM_ELSE] = { "else", SOURCE_NONE },
628+
[ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
618629
/*
619630
* Please update $__git_ref_fieldlist in git-completion.bash
620631
* when you add new atoms
@@ -992,6 +1003,11 @@ static const char *find_next(const char *cp)
9921003
return NULL;
9931004
}
9941005

1006+
static int reject_atom(enum atom_type atom_type)
1007+
{
1008+
return atom_type == ATOM_REST;
1009+
}
1010+
9951011
/*
9961012
* Make sure the format string is well formed, and parse out
9971013
* the used atoms.
@@ -1012,6 +1028,8 @@ int verify_ref_format(struct ref_format *format)
10121028
at = parse_ref_filter_atom(format, sp + 2, ep, &err);
10131029
if (at < 0)
10141030
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);
10151033

10161034
if ((format->quote_style == QUOTE_PYTHON ||
10171035
format->quote_style == QUOTE_SHELL ||
@@ -1931,6 +1949,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
19311949
v->handler = else_atom_handler;
19321950
v->s = xstrdup("");
19331951
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;
19341958
} else
19351959
continue;
19361960

@@ -2148,6 +2172,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
21482172

21492173
FLEX_ALLOC_STR(ref, refname, refname);
21502174
oidcpy(&ref->objectname, oid);
2175+
ref->rest = NULL;
21512176

21522177
return ref;
21532178
}

ref-filter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct ref_sorting {
3838

3939
struct ref_array_item {
4040
struct object_id objectname;
41+
const char *rest;
4142
int flag;
4243
unsigned int kind;
4344
const char *symref;
@@ -76,14 +77,16 @@ struct ref_format {
7677
* verify_ref_format() afterwards to finalize.
7778
*/
7879
const char *format;
80+
const char *rest;
7981
int quote_style;
82+
int use_rest;
8083
int use_color;
8184

8285
/* Internal state to ref-filter */
8386
int need_color_reset_at_eol;
8487
};
8588

86-
#define REF_FORMAT_INIT { NULL, 0, -1 }
89+
#define REF_FORMAT_INIT { .use_color = -1 }
8790

8891
/* Macros for checking --merged and --no-merged options */
8992
#define _OPT_MERGED_NO_MERGED(option, filter, h) \

t/t3203-branch-output.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '
340340
test_cmp expect actual
341341
'
342342

343+
test_expect_success 'git branch with --format=%(rest) must fail' '
344+
test_must_fail git branch --format="%(rest)" >actual
345+
'
346+
343347
test_expect_success 'worktree colors correct' '
344348
cat >expect <<-EOF &&
345349
* <GREEN>(HEAD detached from fromtag)<RESET>

t/t6300-for-each-ref.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,10 @@ test_expect_success 'basic atom: head contents:trailers' '
12111211
test_cmp expect actual.clean
12121212
'
12131213

1214+
test_expect_success 'basic atom: rest must fail' '
1215+
test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
1216+
'
1217+
12141218
test_expect_success 'trailer parsing not fooled by --- line' '
12151219
git commit --allow-empty -F - <<-\EOF &&
12161220
this is the subject

t/t7004-tag.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '
19981998
test_cmp expect actual
19991999
'
20002000

2001+
test_expect_success 'git tag -l with --format="%(rest)" must fail' '
2002+
test_must_fail git tag -l --format="%(rest)" "v1*"
2003+
'
2004+
20012005
test_expect_success "set up color tests" '
20022006
echo "<RED>v1.0<RESET>" >expect.color &&
20032007
echo "v1.0" >expect.bare &&

t/t7030-verify-tag.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '
194194
test_cmp expect actual
195195
'
196196

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+
197201
test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
198202
test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
199203
test_must_be_empty actual-forged

0 commit comments

Comments
 (0)