Skip to content

Commit bc74985

Browse files
committed
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]>
1 parent 0588dbe commit bc74985

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "worktree.h"
2424
#include "hashmap.h"
2525
#include "argv-array.h"
26+
#include "format-support.h"
2627

2728
static struct ref_msg {
2829
const char *gone;
@@ -127,8 +128,8 @@ static struct used_atom {
127128
unsigned int nobracket : 1, push : 1, push_remote : 1;
128129
} remote_ref;
129130
struct {
130-
enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH,
131-
C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
131+
enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
132+
C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
132133
struct process_trailer_options trailer_opts;
133134
unsigned int nlines;
134135
} contents;
@@ -301,9 +302,12 @@ static int body_atom_parser(const struct ref_format *format, struct used_atom *a
301302
static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
302303
const char *arg, struct strbuf *err)
303304
{
304-
if (arg)
305-
return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments"));
306-
atom->u.contents.option = C_SUB;
305+
if (!arg)
306+
atom->u.contents.option = C_SUB;
307+
else if (!strcmp(arg, "sanitize"))
308+
atom->u.contents.option = C_SUB_SANITIZE;
309+
else
310+
return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
307311
return 0;
308312
}
309313

@@ -1282,8 +1286,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
12821286
continue;
12831287
if (deref)
12841288
name++;
1285-
if (strcmp(name, "subject") &&
1286-
strcmp(name, "body") &&
1289+
if (strcmp(name, "body") &&
1290+
!starts_with(name, "subject") &&
12871291
!starts_with(name, "trailers") &&
12881292
!starts_with(name, "contents"))
12891293
continue;
@@ -1295,7 +1299,11 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
12951299

12961300
if (atom->u.contents.option == C_SUB)
12971301
v->s = copy_subject(subpos, sublen);
1298-
else if (atom->u.contents.option == C_BODY_DEP)
1302+
else if (atom->u.contents.option == C_SUB_SANITIZE) {
1303+
struct strbuf sb = STRBUF_INIT;
1304+
format_sanitized_subject(&sb, subpos, sublen);
1305+
v->s = strbuf_detach(&sb, NULL);
1306+
} else if (atom->u.contents.option == C_BODY_DEP)
12991307
v->s = xmemdupz(bodypos, bodylen);
13001308
else if (atom->u.contents.option == C_LENGTH)
13011309
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)