Skip to content

Commit 9503576

Browse files
committed
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`. But this change isn't sufficient to reuse this function in ref-filter.c because there exist tags with multiline subject. It's wise to replace `\n` with ' ', if `format_sanitized_subject()` encounters `\n` before end of subject line, just like `copy_subject()`. Because we'll be only using `format_sanitized_subject()` for "%(subject:sanitize)", instead of `copy_subject()` and `format_sanitized_subject()` both. So, added the code: ``` if (char == '\n') /* never true if called inside pretty.c */ char = ' '; ``` Now, it's ready to be reused in ref-filter.c Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com>
1 parent 764bb23 commit 9503576

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

pretty.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -839,24 +839,29 @@ static int istitlechar(char c)
839839
(c >= '0' && c <= '9') || c == '.' || c == '_';
840840
}
841841

842-
static void format_sanitized_subject(struct strbuf *sb, const char *msg)
842+
static void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
843843
{
844+
char *r = xmemdupz(msg, len);
844845
size_t trimlen;
845846
size_t start_len = sb->len;
846847
int space = 2;
848+
int i;
847849

848-
for (; *msg && *msg != '\n'; msg++) {
849-
if (istitlechar(*msg)) {
850+
for (i = 0; i < len; i++) {
851+
if (r[i] == '\n')
852+
r[i] = ' ';
853+
if (istitlechar(r[i])) {
850854
if (space == 1)
851855
strbuf_addch(sb, '-');
852856
space = 0;
853-
strbuf_addch(sb, *msg);
854-
if (*msg == '.')
855-
while (*(msg+1) == '.')
856-
msg++;
857+
strbuf_addch(sb, r[i]);
858+
if (r[i] == '.')
859+
while (r[i+1] == '.')
860+
i++;
857861
} else
858862
space |= 1;
859863
}
864+
free(r);
860865

861866
/* trim any trailing '.' or '-' characters */
862867
trimlen = 0;
@@ -1155,7 +1160,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11551160
const struct commit *commit = c->commit;
11561161
const char *msg = c->message;
11571162
struct commit_list *p;
1158-
const char *arg;
1163+
const char *arg, *eol;
11591164
size_t res;
11601165
char **slot;
11611166

@@ -1405,7 +1410,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
14051410
format_subject(sb, msg + c->subject_off, " ");
14061411
return 1;
14071412
case 'f': /* sanitized subject */
1408-
format_sanitized_subject(sb, msg + c->subject_off);
1413+
eol = strchrnul(msg + c->subject_off, '\n');
1414+
format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
14091415
return 1;
14101416
case 'b': /* body */
14111417
strbuf_addstr(sb, msg + c->body_off);

0 commit comments

Comments
 (0)