Skip to content

Commit 905f0a4

Browse files
harry-hovgitster
authored andcommitted
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 <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47d4676 commit 905f0a4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ contents:subject::
247247
The first paragraph of the message, which typically is a
248248
single line, is taken as the "subject" of the commit or the
249249
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.
250253

251254
contents:body::
252255
The remainder of the commit or the tag message that follows

ref-filter.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ static struct used_atom {
127127
unsigned int nobracket : 1, push : 1, push_remote : 1;
128128
} remote_ref;
129129
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;
132132
struct process_trailer_options trailer_opts;
133133
unsigned int nlines;
134134
} contents;
@@ -301,9 +301,12 @@ static int body_atom_parser(const struct ref_format *format, struct used_atom *a
301301
static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
302302
const char *arg, struct strbuf *err)
303303
{
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);
307310
return 0;
308311
}
309312

@@ -1282,8 +1285,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
12821285
continue;
12831286
if (deref)
12841287
name++;
1285-
if (strcmp(name, "subject") &&
1286-
strcmp(name, "body") &&
1288+
if (strcmp(name, "body") &&
1289+
!starts_with(name, "subject") &&
12871290
!starts_with(name, "trailers") &&
12881291
!starts_with(name, "contents"))
12891292
continue;
@@ -1295,7 +1298,11 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
12951298

12961299
if (atom->u.contents.option == C_SUB)
12971300
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)
12991306
v->s = xmemdupz(bodypos, bodylen);
13001307
else if (atom->u.contents.option == C_LENGTH)
13011308
v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));

t/t6300-for-each-ref.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ test_atom head taggerdate ''
150150
test_atom head creator 'C O Mitter <[email protected]> 1151968723 +0200'
151151
test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
152152
test_atom head subject 'Initial'
153+
test_atom head subject:sanitize 'Initial'
153154
test_atom head contents:subject 'Initial'
154155
test_atom head body ''
155156
test_atom head contents:body ''
@@ -207,6 +208,7 @@ test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
207208
test_atom tag creator 'C O Mitter <[email protected]> 1151968725 +0200'
208209
test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
209210
test_atom tag subject 'Tagging at 1151968727'
211+
test_atom tag subject:sanitize 'Tagging-at-1151968727'
210212
test_atom tag contents:subject 'Tagging at 1151968727'
211213
test_atom tag body ''
212214
test_atom tag contents:body ''
@@ -619,6 +621,7 @@ test_expect_success 'create tag with subject and body content' '
619621
git tag -F msg subject-body
620622
'
621623
test_atom refs/tags/subject-body subject 'the subject line'
624+
test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
622625
test_atom refs/tags/subject-body body 'first body line
623626
second body line
624627
'
@@ -639,6 +642,7 @@ test_expect_success 'create tag with multiline subject' '
639642
git tag -F msg multiline
640643
'
641644
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'
642646
test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
643647
test_atom refs/tags/multiline body 'first body line
644648
second body line
@@ -671,13 +675,15 @@ sig='-----BEGIN PGP SIGNATURE-----
671675

672676
PREREQ=GPG
673677
test_atom refs/tags/signed-empty subject ''
678+
test_atom refs/tags/signed-empty subject:sanitize ''
674679
test_atom refs/tags/signed-empty contents:subject ''
675680
test_atom refs/tags/signed-empty body "$sig"
676681
test_atom refs/tags/signed-empty contents:body ''
677682
test_atom refs/tags/signed-empty contents:signature "$sig"
678683
test_atom refs/tags/signed-empty contents "$sig"
679684

680685
test_atom refs/tags/signed-short subject 'subject line'
686+
test_atom refs/tags/signed-short subject:sanitize 'subject-line'
681687
test_atom refs/tags/signed-short contents:subject 'subject line'
682688
test_atom refs/tags/signed-short body "$sig"
683689
test_atom refs/tags/signed-short contents:body ''
@@ -686,6 +692,7 @@ test_atom refs/tags/signed-short contents "subject line
686692
$sig"
687693

688694
test_atom refs/tags/signed-long subject 'subject line'
695+
test_atom refs/tags/signed-long subject:sanitize 'subject-line'
689696
test_atom refs/tags/signed-long contents:subject 'subject line'
690697
test_atom refs/tags/signed-long body "body contents
691698
$sig"

0 commit comments

Comments
 (0)