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>
Karthik Nayak committed
Sep 11, 2015 at 20:34 UTC
1bb38e5a6a8fb6cf9b882a1a7038d649ceba0085
5 files changed
+103
-6
Documentation/git-for-each-ref.txt
+2
-1
@@ -150,7 +150,8 @@ The complete message in a commit and tag object is `contents`.
150
Its first line is `contents:subject`, where subject is the concatenation
151
of all lines of the commit message up to the first blank line. The next
152
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`.
155
156
For sorting purposes, fields with numeric values sort in numeric
157
order (`objectsize`, `authordate`, `committerdate`, `taggerdate`).
builtin/tag.c
+4
@@ -185,6 +185,10 @@ static enum contains_result contains(struct commit *candidate,
185
return contains_test(candidate, want);
186
}
187
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
+ */
192
static void show_tag_lines(const struct object_id *oid, int lines)
193
{
194
int i;
ref-filter.c
+43
-4
@@ -45,9 +45,6 @@ static struct {
45
{ "subject" },
46
{ "body" },
47
{ "contents" },
48
- { "contents:subject" },
49
- { "contents:body" },
50
- { "contents:signature" },
48
{ "upstream" },
49
{ "push" },
50
{ "symref" },
@@ -65,6 +62,11 @@ struct align {
62
unsigned int width;
63
};
64
65
+struct contents {
66
+ unsigned int lines;
67
+ struct object_id oid;
68
+};
69
+
70
struct ref_formatting_stack {
71
struct ref_formatting_stack *prev;
72
struct strbuf output;
@@ -81,6 +83,7 @@ struct atom_value {
83
const char *s;
84
union {
85
struct align align;
86
+ struct contents contents;
87
} u;
88
void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
89
unsigned long ul; /* used for sorting when not FIELD_STR */
@@ -643,6 +646,30 @@ static void find_subpos(const char *buf, unsigned long sz,
646
*nonsiglen = *sig - buf;
647
}
648
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
+
673
/* See grab_values */
674
static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
675
{
@@ -653,6 +680,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
680
for (i = 0; i < used_atom_cnt; i++) {
681
const char *name = used_atom[i];
682
struct atom_value *v = &val[i];
683
+ const char *valp = NULL;
684
if (!!deref != (*name == '*'))
685
continue;
686
if (deref)
@@ -662,7 +690,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
690
strcmp(name, "contents") &&
691
strcmp(name, "contents:subject") &&
692
strcmp(name, "contents:body") &&
665
- strcmp(name, "contents:signature"))
693
+ strcmp(name, "contents:signature") &&
694
+ !starts_with(name, "contents:lines="))
695
continue;
696
if (!subpos)
697
find_subpos(buf, sz,
@@ -682,6 +711,16 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
711
v->s = xmemdupz(sigpos, siglen);
712
else if (!strcmp(name, "contents"))
713
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
+ }
724
}
725
}
726
ref-filter.h
+2
-1
@@ -59,7 +59,8 @@ struct ref_filter {
59
struct commit *merge_commit;
60
61
unsigned int with_commit_tag_algo : 1;
62
- unsigned int kind;
62
+ unsigned int kind,
63
+ lines;
64
};
65
66
struct ref_filter_cbdata {
t/t6302-for-each-ref-filter.sh
+52
@@ -167,4 +167,56 @@ test_expect_success 'nested alignment with quote formatting' "
167
test_cmp expect actual
168
"
169
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
+
222
test_done