]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pretty: refactor `format_sanitized_subject()`
authorHariom Verma <hariom18599@gmail.com>
Fri, 21 Aug 2020 21:41:49 +0000 (21:41 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Aug 2020 20:52:51 +0000 (13:52 -0700)
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>
pretty.c
pretty.h

index 2a3d46bf42fea1d375eb1b29e52ee1caaa0372a0..7a7708a0ea707ac58f1961a65b40fa89ea8631d3 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -839,21 +839,22 @@ static int istitlechar(char c)
                (c >= '0' && c <= '9') || c == '.' || c == '_';
 }
 
-static void format_sanitized_subject(struct strbuf *sb, const char *msg)
+void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
 {
        size_t trimlen;
        size_t start_len = sb->len;
        int space = 2;
+       int i;
 
-       for (; *msg && *msg != '\n'; msg++) {
-               if (istitlechar(*msg)) {
+       for (i = 0; i < len; i++) {
+               if (istitlechar(msg[i])) {
                        if (space == 1)
                                strbuf_addch(sb, '-');
                        space = 0;
-                       strbuf_addch(sb, *msg);
-                       if (*msg == '.')
-                               while (*(msg+1) == '.')
-                                       msg++;
+                       strbuf_addch(sb, msg[i]);
+                       if (msg[i] == '.')
+                               while (msg[i+1] == '.')
+                                       i++;
                } else
                        space |= 1;
        }
@@ -1155,7 +1156,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        const struct commit *commit = c->commit;
        const char *msg = c->message;
        struct commit_list *p;
-       const char *arg;
+       const char *arg, *eol;
        size_t res;
        char **slot;
 
@@ -1405,7 +1406,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                format_subject(sb, msg + c->subject_off, " ");
                return 1;
        case 'f':       /* sanitized subject */
-               format_sanitized_subject(sb, msg + c->subject_off);
+               eol = strchrnul(msg + c->subject_off, '\n');
+               format_sanitized_subject(sb, msg + c->subject_off, eol - (msg + c->subject_off));
                return 1;
        case 'b':       /* body */
                strbuf_addstr(sb, msg + c->body_off);
index 071f2fb8e449cee51c6556998b416c4fa7c8005a..7ce6c0b437b448b7f384ac3680de5c3e8983d8fb 100644 (file)
--- a/pretty.h
+++ b/pretty.h
@@ -139,4 +139,7 @@ const char *format_subject(struct strbuf *sb, const char *msg,
 /* Check if "cmit_fmt" will produce an empty output. */
 int commit_format_is_empty(enum cmit_fmt);
 
+/* Make subject of commit message suitable for filename */
+void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
+
 #endif /* PRETTY_H */