Skip to content

Commit 47d4676

Browse files
harry-hovgitster
authored andcommitted
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 <[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 26bc0aa commit 47d4676

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

pretty.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -839,21 +839,22 @@ 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+
void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
843843
{
844844
size_t trimlen;
845845
size_t start_len = sb->len;
846846
int space = 2;
847+
int i;
847848

848-
for (; *msg && *msg != '\n'; msg++) {
849-
if (istitlechar(*msg)) {
849+
for (i = 0; i < len; i++) {
850+
if (istitlechar(msg[i])) {
850851
if (space == 1)
851852
strbuf_addch(sb, '-');
852853
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++;
857858
} else
858859
space |= 1;
859860
}
@@ -1155,7 +1156,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11551156
const struct commit *commit = c->commit;
11561157
const char *msg = c->message;
11571158
struct commit_list *p;
1158-
const char *arg;
1159+
const char *arg, *eol;
11591160
size_t res;
11601161
char **slot;
11611162

@@ -1405,7 +1406,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
14051406
format_subject(sb, msg + c->subject_off, " ");
14061407
return 1;
14071408
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));
14091411
return 1;
14101412
case 'b': /* body */
14111413
strbuf_addstr(sb, msg + c->body_off);

pretty.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,7 @@ const char *format_subject(struct strbuf *sb, const char *msg,
139139
/* Check if "cmit_fmt" will produce an empty output. */
140140
int commit_format_is_empty(enum cmit_fmt);
141141

142+
/* Make subject of commit message suitable for filename */
143+
void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
144+
142145
#endif /* PRETTY_H */

0 commit comments

Comments
 (0)