ref-filter: add `sanitize` option for 'subject' atom
Currently, subject does not take any arguments. This commit introduce `sanitize` formatting option to 'subject' atom. `subject:sanitize` - print sanitized subject line, suitable for a filename. e.g. %(subject): "the subject line" %(subject:sanitize): "the-subject-line" Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Hariom Verma committed
Aug 21, 2020 at 21:41 UTC
905f0a4e64bef35482a999dd01c64945b260c010
3 files changed
+25
-8
Documentation/git-for-each-ref.txt
+3
@@ -247,6 +247,9 @@ contents:subject::
247
The first paragraph of the message, which typically is a
248
single line, is taken as the "subject" of the commit or the
249
tag message.
250
+ Instead of `contents:subject`, field `subject` can also be used to
251
+ obtain same results. `:sanitize` can be appended to `subject` for
252
+ subject line suitable for filename.
253
254
contents:body::
255
The remainder of the commit or the tag message that follows
ref-filter.c
+15
-8
@@ -127,8 +127,8 @@ static struct used_atom {
127
unsigned int nobracket : 1, push : 1, push_remote : 1;
128
} remote_ref;
129
struct {
130
- enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH,
131
- C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
130
+ enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
131
+ C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
132
struct process_trailer_options trailer_opts;
133
unsigned int nlines;
134
} contents;
@@ -301,9 +301,12 @@ static int body_atom_parser(const struct ref_format *format, struct used_atom *a
301
static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
302
const char *arg, struct strbuf *err)
303
{
304
- if (arg)
305
- return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments"));
306
- atom->u.contents.option = C_SUB;
304
+ if (!arg)
305
+ atom->u.contents.option = C_SUB;
306
+ else if (!strcmp(arg, "sanitize"))
307
+ atom->u.contents.option = C_SUB_SANITIZE;
308
+ else
309
+ return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
310
return 0;
311
}
312
@@ -1282,8 +1285,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1285
continue;
1286
if (deref)
1287
name++;
1285
- if (strcmp(name, "subject") &&
1286
- strcmp(name, "body") &&
1288
+ if (strcmp(name, "body") &&
1289
+ !starts_with(name, "subject") &&
1290
!starts_with(name, "trailers") &&
1291
!starts_with(name, "contents"))
1292
continue;
@@ -1295,7 +1298,11 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
1298
1299
if (atom->u.contents.option == C_SUB)
1300
v->s = copy_subject(subpos, sublen);
1298
- else if (atom->u.contents.option == C_BODY_DEP)
1301
+ else if (atom->u.contents.option == C_SUB_SANITIZE) {
1302
+ struct strbuf sb = STRBUF_INIT;
1303
+ format_sanitized_subject(&sb, subpos, sublen);
1304
+ v->s = strbuf_detach(&sb, NULL);
1305
+ } else if (atom->u.contents.option == C_BODY_DEP)
1306
v->s = xmemdupz(bodypos, bodylen);
1307
else if (atom->u.contents.option == C_LENGTH)
1308
v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
t/t6300-for-each-ref.sh
+7
@@ -150,6 +150,7 @@ test_atom head taggerdate ''
150
test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
151
test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
152
test_atom head subject 'Initial'
153
+test_atom head subject:sanitize 'Initial'
154
test_atom head contents:subject 'Initial'
155
test_atom head body ''
156
test_atom head contents:body ''
@@ -207,6 +208,7 @@ test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
208
test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
209
test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
210
test_atom tag subject 'Tagging at 1151968727'
211
+test_atom tag subject:sanitize 'Tagging-at-1151968727'
212
test_atom tag contents:subject 'Tagging at 1151968727'
213
test_atom tag body ''
214
test_atom tag contents:body ''
@@ -619,6 +621,7 @@ test_expect_success 'create tag with subject and body content' '
621
git tag -F msg subject-body
622
'
623
test_atom refs/tags/subject-body subject 'the subject line'
624
+test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
625
test_atom refs/tags/subject-body body 'first body line
626
second body line
627
'
@@ -639,6 +642,7 @@ test_expect_success 'create tag with multiline subject' '
642
git tag -F msg multiline
643
'
644
test_atom refs/tags/multiline subject 'first subject line second subject line'
645
+test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
646
test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
647
test_atom refs/tags/multiline body 'first body line
648
second body line
@@ -671,6 +675,7 @@ sig='-----BEGIN PGP SIGNATURE-----
675
676
PREREQ=GPG
677
test_atom refs/tags/signed-empty subject ''
678
+test_atom refs/tags/signed-empty subject:sanitize ''
679
test_atom refs/tags/signed-empty contents:subject ''
680
test_atom refs/tags/signed-empty body "$sig"
681
test_atom refs/tags/signed-empty contents:body ''
@@ -678,6 +683,7 @@ test_atom refs/tags/signed-empty contents:signature "$sig"
683
test_atom refs/tags/signed-empty contents "$sig"
684
685
test_atom refs/tags/signed-short subject 'subject line'
686
+test_atom refs/tags/signed-short subject:sanitize 'subject-line'
687
test_atom refs/tags/signed-short contents:subject 'subject line'
688
test_atom refs/tags/signed-short body "$sig"
689
test_atom refs/tags/signed-short contents:body ''
@@ -686,6 +692,7 @@ test_atom refs/tags/signed-short contents "subject line
692
$sig"
693
694
test_atom refs/tags/signed-long subject 'subject line'
695
+test_atom refs/tags/signed-long subject:sanitize 'subject-line'
696
test_atom refs/tags/signed-long contents:subject 'subject line'
697
test_atom refs/tags/signed-long body "body contents
698
$sig"