git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 8/8] ref-filter: add `sanitize` option for 'subject' atom

From
HGHariom Verma via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 21, 2020, 21:41 UTC
Message-ID
<7cba8d7a2881055e89976ca420392da2eaa596b8.1598046110.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.684.v4.git.1598046110.gitgitgadget@gmail.com>
From: Hariom Verma <hariom18599@gmail.com>

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>
---
 Documentation/git-for-each-ref.txt |  3 +++
 ref-filter.c                       | 23 +++++++++++++++--------
 t/t6300-for-each-ref.sh            |  7 +++++++
 3 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index dd09763e7d..616ce46087 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -247,6 +247,9 @@ contents:subject::
 	The first paragraph of the message, which typically is a
 	single line, is taken as the "subject" of the commit or the
 	tag message.
+	Instead of `contents:subject`, field `subject` can also be used to
+	obtain same results. `:sanitize` can be appended to `subject` for
+	subject line suitable for filename.
 
 contents:body::
 	The remainder of the commit or the tag message that follows
diff --git a/ref-filter.c b/ref-filter.c
index c7d81088e4..12bb78ce06 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -127,8 +127,8 @@ static struct used_atom {
 			unsigned int nobracket : 1, push : 1, push_remote : 1;
 		} remote_ref;
 		struct {
-			enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH,
-			       C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
+			enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
+			       C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
 			struct process_trailer_options trailer_opts;
 			unsigned int nlines;
 		} contents;
@@ -301,9 +301,12 @@ static int body_atom_parser(const struct ref_format *format, struct used_atom *a
 static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
 			       const char *arg, struct strbuf *err)
 {
-	if (arg)
-		return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments"));
-	atom->u.contents.option = C_SUB;
+	if (!arg)
+		atom->u.contents.option = C_SUB;
+	else if (!strcmp(arg, "sanitize"))
+		atom->u.contents.option = C_SUB_SANITIZE;
+	else
+		return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
 	return 0;
 }
 
@@ -1282,8 +1285,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
 			continue;
 		if (deref)
 			name++;
-		if (strcmp(name, "subject") &&
-		    strcmp(name, "body") &&
+		if (strcmp(name, "body") &&
+		    !starts_with(name, "subject") &&
 		    !starts_with(name, "trailers") &&
 		    !starts_with(name, "contents"))
 			continue;
@@ -1295,7 +1298,11 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
 
 		if (atom->u.contents.option == C_SUB)
 			v->s = copy_subject(subpos, sublen);
-		else if (atom->u.contents.option == C_BODY_DEP)
+		else if (atom->u.contents.option == C_SUB_SANITIZE) {
+			struct strbuf sb = STRBUF_INIT;
+			format_sanitized_subject(&sb, subpos, sublen);
+			v->s = strbuf_detach(&sb, NULL);
+		} else if (atom->u.contents.option == C_BODY_DEP)
 			v->s = xmemdupz(bodypos, bodylen);
 		else if (atom->u.contents.option == C_LENGTH)
 			v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 79d5b29387..220ff5c3c2 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -150,6 +150,7 @@ test_atom head taggerdate ''
 test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
 test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
 test_atom head subject 'Initial'
+test_atom head subject:sanitize 'Initial'
 test_atom head contents:subject 'Initial'
 test_atom head body ''
 test_atom head contents:body ''
@@ -207,6 +208,7 @@ test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
 test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
 test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
 test_atom tag subject 'Tagging at 1151968727'
+test_atom tag subject:sanitize 'Tagging-at-1151968727'
 test_atom tag contents:subject 'Tagging at 1151968727'
 test_atom tag body ''
 test_atom tag contents:body ''
@@ -619,6 +621,7 @@ test_expect_success 'create tag with subject and body content' '
 	git tag -F msg subject-body
 '
 test_atom refs/tags/subject-body subject 'the subject line'
+test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
 test_atom refs/tags/subject-body body 'first body line
 second body line
 '
@@ -639,6 +642,7 @@ test_expect_success 'create tag with multiline subject' '
 	git tag -F msg multiline
 '
 test_atom refs/tags/multiline subject 'first subject line second subject line'
+test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
 test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
 test_atom refs/tags/multiline body 'first body line
 second body line
@@ -671,6 +675,7 @@ sig='-----BEGIN PGP SIGNATURE-----
 
 PREREQ=GPG
 test_atom refs/tags/signed-empty subject ''
+test_atom refs/tags/signed-empty subject:sanitize ''
 test_atom refs/tags/signed-empty contents:subject ''
 test_atom refs/tags/signed-empty body "$sig"
 test_atom refs/tags/signed-empty contents:body ''
@@ -678,6 +683,7 @@ test_atom refs/tags/signed-empty contents:signature "$sig"
 test_atom refs/tags/signed-empty contents "$sig"
 
 test_atom refs/tags/signed-short subject 'subject line'
+test_atom refs/tags/signed-short subject:sanitize 'subject-line'
 test_atom refs/tags/signed-short contents:subject 'subject line'
 test_atom refs/tags/signed-short body "$sig"
 test_atom refs/tags/signed-short contents:body ''
@@ -686,6 +692,7 @@ test_atom refs/tags/signed-short contents "subject line
 $sig"
 
 test_atom refs/tags/signed-long subject 'subject line'
+test_atom refs/tags/signed-long subject:sanitize 'subject-line'
 test_atom refs/tags/signed-long contents:subject 'subject line'
 test_atom refs/tags/signed-long body "body contents
 $sig"
-- 
gitgitgadget
Previous: Hariom Verma via GitGitGadgetNext: Hariom Verma via GitGitGadget
Message 49 of 51 in “[GSoC] Improvements to ref-filter”
  1. 0/5 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Jul 27, 2020
  2. 1/5 ref-filter: support different email formatsHariom Verma via GitGitGadget, Jul 27, 2020
  3. Junio C HamanoJul 27, 2020
  4. Hariom vermaJul 28, 2020
  5. Junio C HamanoJul 28, 2020
  6. Đoàn Trần Công DanhJul 28, 2020
  7. Junio C HamanoJul 28, 2020
  8. 2/5 ref-filter: add `short` option for 'tree' and 'parent'Hariom Verma via GitGitGadget, Jul 27, 2020
  9. Junio C HamanoJul 27, 2020
  10. 3/5 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Jul 27, 2020
  11. 4/5 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Jul 27, 2020
  12. 5/5 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Jul 27, 2020
  13. 0/9 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 5, 2020
  14. 1/9 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 5, 2020
  15. 2/9 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 5, 2020
  16. 3/9 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 5, 2020
  17. 4/9 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 5, 2020
  18. 5/9 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 5, 2020
  19. 6/9 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 5, 2020
  20. 8/9 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Aug 5, 2020
  21. 9/9 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 5, 2020
  22. 7/9 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 5, 2020
  23. Junio C HamanoAug 5, 2020
  24. Hariom vermaAug 6, 2020
  25. 0/9 [Resend][GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 17, 2020
  26. 2/9 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 17, 2020
  27. 3/9 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 17, 2020
  28. 1/9 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 17, 2020
  29. 4/9 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 17, 2020
  30. 7/9 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 17, 2020
  31. Junio C HamanoAug 17, 2020
  32. Hariom vermaAug 19, 2020
  33. Junio C HamanoAug 19, 2020
  34. Junio C HamanoAug 19, 2020
  35. Hariom vermaAug 20, 2020
  36. Hariom vermaAug 20, 2020
  37. 6/9 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 17, 2020
  38. 5/9 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 17, 2020
  39. 8/9 format-support: move `format_sanitized_subject()` from prettyHariom Verma via GitGitGadget, Aug 17, 2020
  40. Junio C HamanoAug 17, 2020
  41. 9/9 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 17, 2020
  42. Junio C HamanoAug 17, 2020
  43. 0/8 [GSoC] Improvements to ref-filterHariom Verma via GitGitGadget, Aug 21, 2020
  44. 1/8 ref-filter: support different email formatsHariom Verma via GitGitGadget, Aug 21, 2020
  45. 2/8 ref-filter: refactor `grab_objectname()`Hariom Verma via GitGitGadget, Aug 21, 2020
  46. 3/8 ref-filter: modify error messages in `grab_objectname()`Hariom Verma via GitGitGadget, Aug 21, 2020
  47. 5/8 ref-filter: add `short` modifier to 'tree' atomHariom Verma via GitGitGadget, Aug 21, 2020
  48. 6/8 ref-filter: add `short` modifier to 'parent' atomHariom Verma via GitGitGadget, Aug 21, 2020
  49. 8/8 ref-filter: add `sanitize` option for 'subject' atomHariom Verma via GitGitGadget, Aug 21, 2020
  50. 4/8 ref-filter: rename `objectname` related functions and fieldsHariom Verma via GitGitGadget, Aug 21, 2020
  51. 7/8 pretty: refactor `format_sanitized_subject()`Hariom Verma via GitGitGadget, Aug 21, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.