@matheusbsbs / matheus-bagliano / commits / 47d4676ac8
Fork 0

pretty: refactor `format_sanitized_subject()`

The function 'format_sanitized_subject()' is responsible for sanitized subject line in pretty.c e.g. the subject line the-sanitized-subject-line It would be a nice enhancement to `subject` atom to have the same feature. So in the later commits, we plan to add this feature to ref-filter. Refactor `format_sanitized_subject()`, so it can be reused in ref-filter.c for adding new modifier `sanitize` to "subject" atom. Currently, the loop inside `format_sanitized_subject()` runs until `\n` is found. But now, we stored the first occurrence of `\n` in a variable `eol` and passed it in `format_sanitized_subject()`. And the loop runs upto `eol`. 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 47d4676ac8a50b7d53e12ae200ed9b5661522129
2 files changed +14 -9
pretty.c
+11 -9
@@ -839,21 +839,22 @@ static int istitlechar(char c)
839 (c >= '0' && c <= '9') || c == '.' || c == '_';
840 }
841
842 -static void format_sanitized_subject(struct strbuf *sb, const char *msg)
842 +void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
843 {
844 size_t trimlen;
845 size_t start_len = sb->len;
846 int space = 2;
847 + int i;
848
848 - for (; *msg && *msg != '\n'; msg++) {
849 - if (istitlechar(*msg)) {
849 + for (i = 0; i < len; i++) {
850 + if (istitlechar(msg[i])) {
851 if (space == 1)
852 strbuf_addch(sb, '-');
853 space = 0;
853 - strbuf_addch(sb, *msg);
854 - if (*msg == '.')
855 - while (*(msg+1) == '.')
856 - msg++;
854 + strbuf_addch(sb, msg[i]);
855 + if (msg[i] == '.')
856 + while (msg[i+1] == '.')
857 + i++;
858 } else
859 space |= 1;
860 }
@@ -1155,7 +1156,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1156 const struct commit *commit = c->commit;
1157 const char *msg = c->message;
1158 struct commit_list *p;
1158 - const char *arg;
1159 + const char *arg, *eol;
1160 size_t res;
1161 char **slot;
1162
@@ -1405,7 +1406,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1406 format_subject(sb, msg + c->subject_off, " ");
1407 return 1;
1408 case 'f': /* sanitized subject */
1408 - format_sanitized_subject(sb, msg + c->subject_off);
1409 + eol = strchrnul(msg + c->subject_off, '\n');
1410 + format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
1411 return 1;
1412 case 'b': /* body */
1413 strbuf_addstr(sb, msg + c->body_off);
pretty.h
+3
@@ -139,4 +139,7 @@ const char *format_subject(struct strbuf *sb, const char *msg,
139 /* Check if "cmit_fmt" will produce an empty output. */
140 int commit_format_is_empty(enum cmit_fmt);
141
142 +/* Make subject of commit message suitable for filename */
143 +void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
144 +
145 #endif /* PRETTY_H */