Skip to content

Commit 0588dbe

Browse files
committed
format-support: move format_sanitized_subject() from pretty
In hope of some new features in `subject` atom, move funtion `format_sanitized_subject()` and all the function it uses to new file format-support.{c,h}. Consider this new file as a common interface between functions that pretty.c and ref-filter.c shares. Mentored-by: Christian Couder <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]>
1 parent f56ba4d commit 0588dbe

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ LIB_OBJS += exec-cmd.o
882882
LIB_OBJS += fetch-negotiator.o
883883
LIB_OBJS += fetch-pack.o
884884
LIB_OBJS += fmt-merge-msg.o
885+
LIB_OBJS += format-support.o
885886
LIB_OBJS += fsck.o
886887
LIB_OBJS += fsmonitor.o
887888
LIB_OBJS += gettext.o

format-support.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "diff.h"
2+
#include "log-tree.h"
3+
#include "color.h"
4+
#include "format-support.h"
5+
6+
static int istitlechar(char c)
7+
{
8+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
9+
(c >= '0' && c <= '9') || c == '.' || c == '_';
10+
}
11+
12+
void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
13+
{
14+
char *r = xmemdupz(msg, len);
15+
size_t trimlen;
16+
size_t start_len = sb->len;
17+
int space = 2;
18+
int i;
19+
20+
for (i = 0; i < len; i++) {
21+
if (r[i] == '\n')
22+
r[i] = ' ';
23+
if (istitlechar(r[i])) {
24+
if (space == 1)
25+
strbuf_addch(sb, '-');
26+
space = 0;
27+
strbuf_addch(sb, r[i]);
28+
if (r[i] == '.')
29+
while (r[i+1] == '.')
30+
i++;
31+
} else
32+
space |= 1;
33+
}
34+
free(r);
35+
36+
/* trim any trailing '.' or '-' characters */
37+
trimlen = 0;
38+
while (sb->len - trimlen > start_len &&
39+
(sb->buf[sb->len - 1 - trimlen] == '.'
40+
|| sb->buf[sb->len - 1 - trimlen] == '-'))
41+
trimlen++;
42+
strbuf_remove(sb, sb->len - trimlen, trimlen);
43+
}

format-support.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef FORMAT_SUPPORT_H
2+
#define FORMAT_SUPPORT_H
3+
4+
void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
5+
6+
#endif /* FORMAT_SUPPORT_H */

pretty.c

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "reflog-walk.h"
1313
#include "gpg-interface.h"
1414
#include "trailer.h"
15+
#include "format-support.h"
1516

1617
static char *user_format;
1718
static struct cmt_fmt_map {
@@ -833,45 +834,6 @@ static void parse_commit_header(struct format_commit_context *context)
833834
context->commit_header_parsed = 1;
834835
}
835836

836-
static int istitlechar(char c)
837-
{
838-
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
839-
(c >= '0' && c <= '9') || c == '.' || c == '_';
840-
}
841-
842-
static void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len)
843-
{
844-
char *r = xmemdupz(msg, len);
845-
size_t trimlen;
846-
size_t start_len = sb->len;
847-
int space = 2;
848-
int i;
849-
850-
for (i = 0; i < len; i++) {
851-
if (r[i] == '\n')
852-
r[i] = ' ';
853-
if (istitlechar(r[i])) {
854-
if (space == 1)
855-
strbuf_addch(sb, '-');
856-
space = 0;
857-
strbuf_addch(sb, r[i]);
858-
if (r[i] == '.')
859-
while (r[i+1] == '.')
860-
i++;
861-
} else
862-
space |= 1;
863-
}
864-
free(r);
865-
866-
/* trim any trailing '.' or '-' characters */
867-
trimlen = 0;
868-
while (sb->len - trimlen > start_len &&
869-
(sb->buf[sb->len - 1 - trimlen] == '.'
870-
|| sb->buf[sb->len - 1 - trimlen] == '-'))
871-
trimlen++;
872-
strbuf_remove(sb, sb->len - trimlen, trimlen);
873-
}
874-
875837
const char *format_subject(struct strbuf *sb, const char *msg,
876838
const char *line_separator)
877839
{

0 commit comments

Comments
 (0)