Skip to content

Commit 1bb38e5

Browse files
KarthikNayakgitster
authored andcommitted
ref-filter: add support for %(contents:lines=X)
In 'tag.c' we can print N lines from the annotation of the tag using the '-n<num>' option. Copy code from 'tag.c' to 'ref-filter' and modify it to support appending of N lines from the annotation of tags to the given strbuf. Implement %(contents:lines=X) where X lines of the given object are obtained. While we're at it, remove unused "contents:<suboption>" atoms from the `valid_atom` array. Add documentation and test for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5b4f285 commit 1bb38e5

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ The complete message in a commit and tag object is `contents`.
150150
Its first line is `contents:subject`, where subject is the concatenation
151151
of all lines of the commit message up to the first blank line. The next
152152
line is 'contents:body', where body is all of the lines after the first
153-
blank line. Finally, the optional GPG signature is `contents:signature`.
153+
blank line. The optional GPG signature is `contents:signature`. The
154+
first `N` lines of the message is obtained using `contents:lines=N`.
154155

155156
For sorting purposes, fields with numeric values sort in numeric
156157
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).

builtin/tag.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,
185185
return contains_test(candidate, want);
186186
}
187187

188+
/*
189+
* Currently modified and used in ref-filter as append_lines(), will
190+
* eventually be removed as we port tag.c to use ref-filter APIs.
191+
*/
188192
static void show_tag_lines(const struct object_id *oid, int lines)
189193
{
190194
int i;

ref-filter.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ static struct {
4545
{ "subject" },
4646
{ "body" },
4747
{ "contents" },
48-
{ "contents:subject" },
49-
{ "contents:body" },
50-
{ "contents:signature" },
5148
{ "upstream" },
5249
{ "push" },
5350
{ "symref" },
@@ -65,6 +62,11 @@ struct align {
6562
unsigned int width;
6663
};
6764

65+
struct contents {
66+
unsigned int lines;
67+
struct object_id oid;
68+
};
69+
6870
struct ref_formatting_stack {
6971
struct ref_formatting_stack *prev;
7072
struct strbuf output;
@@ -81,6 +83,7 @@ struct atom_value {
8183
const char *s;
8284
union {
8385
struct align align;
86+
struct contents contents;
8487
} u;
8588
void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
8689
unsigned long ul; /* used for sorting when not FIELD_STR */
@@ -643,6 +646,30 @@ static void find_subpos(const char *buf, unsigned long sz,
643646
*nonsiglen = *sig - buf;
644647
}
645648

649+
/*
650+
* If 'lines' is greater than 0, append that many lines from the given
651+
* 'buf' of length 'size' to the given strbuf.
652+
*/
653+
static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
654+
{
655+
int i;
656+
const char *sp, *eol;
657+
size_t len;
658+
659+
sp = buf;
660+
661+
for (i = 0; i < lines && sp < buf + size; i++) {
662+
if (i)
663+
strbuf_addstr(out, "\n ");
664+
eol = memchr(sp, '\n', size - (sp - buf));
665+
len = eol ? eol - sp : size - (sp - buf);
666+
strbuf_add(out, sp, len);
667+
if (!eol)
668+
break;
669+
sp = eol + 1;
670+
}
671+
}
672+
646673
/* See grab_values */
647674
static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
648675
{
@@ -653,6 +680,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
653680
for (i = 0; i < used_atom_cnt; i++) {
654681
const char *name = used_atom[i];
655682
struct atom_value *v = &val[i];
683+
const char *valp = NULL;
656684
if (!!deref != (*name == '*'))
657685
continue;
658686
if (deref)
@@ -662,7 +690,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
662690
strcmp(name, "contents") &&
663691
strcmp(name, "contents:subject") &&
664692
strcmp(name, "contents:body") &&
665-
strcmp(name, "contents:signature"))
693+
strcmp(name, "contents:signature") &&
694+
!starts_with(name, "contents:lines="))
666695
continue;
667696
if (!subpos)
668697
find_subpos(buf, sz,
@@ -682,6 +711,16 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
682711
v->s = xmemdupz(sigpos, siglen);
683712
else if (!strcmp(name, "contents"))
684713
v->s = xstrdup(subpos);
714+
else if (skip_prefix(name, "contents:lines=", &valp)) {
715+
struct strbuf s = STRBUF_INIT;
716+
const char *contents_end = bodylen + bodypos - siglen;
717+
718+
if (strtoul_ui(valp, 10, &v->u.contents.lines))
719+
die(_("positive value expected contents:lines=%s"), valp);
720+
/* Size is the length of the message after removing the signature */
721+
append_lines(&s, subpos, contents_end - subpos, v->u.contents.lines);
722+
v->s = strbuf_detach(&s, NULL);
723+
}
685724
}
686725
}
687726

ref-filter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ struct ref_filter {
5959
struct commit *merge_commit;
6060

6161
unsigned int with_commit_tag_algo : 1;
62-
unsigned int kind;
62+
unsigned int kind,
63+
lines;
6364
};
6465

6566
struct ref_filter_cbdata {

t/t6302-for-each-ref-filter.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,56 @@ test_expect_success 'nested alignment with quote formatting' "
167167
test_cmp expect actual
168168
"
169169

170+
test_expect_success 'check `%(contents:lines=1)`' '
171+
cat >expect <<-\EOF &&
172+
master |three
173+
side |four
174+
odd/spot |three
175+
double-tag |Annonated doubly
176+
four |four
177+
one |one
178+
signed-tag |A signed tag message
179+
three |three
180+
two |two
181+
EOF
182+
git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
183+
test_cmp expect actual
184+
'
185+
186+
test_expect_success 'check `%(contents:lines=0)`' '
187+
cat >expect <<-\EOF &&
188+
master |
189+
side |
190+
odd/spot |
191+
double-tag |
192+
four |
193+
one |
194+
signed-tag |
195+
three |
196+
two |
197+
EOF
198+
git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
199+
test_cmp expect actual
200+
'
201+
202+
test_expect_success 'check `%(contents:lines=99999)`' '
203+
cat >expect <<-\EOF &&
204+
master |three
205+
side |four
206+
odd/spot |three
207+
double-tag |Annonated doubly
208+
four |four
209+
one |one
210+
signed-tag |A signed tag message
211+
three |three
212+
two |two
213+
EOF
214+
git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
215+
test_cmp expect actual
216+
'
217+
218+
test_expect_success '`%(contents:lines=-1)` should fail' '
219+
test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
220+
'
221+
170222
test_done

0 commit comments

Comments
 (0)