pretty.c: add %f format specifier to format_commit_message()
This specifier represents the sanitized and filename friendly subject line of a commit. No checks are made against the length of the string, so users may need to trim the result to the desired length if using as a filename. This is commonly used by format-patch to massage commit subjects into filenames and output patches to files. Signed-off-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephen Boyd committed
Mar 22, 2009 at 19:14 UTC
46d164b0cd1d5d254047d7573c53e368e42bf5e5
2 files changed
+36
Documentation/pretty-formats.txt
+1
@@ -121,6 +121,7 @@ The placeholders are:
121
- '%d': ref names, like the --decorate option of linkgit:git-log[1]
122
- '%e': encoding
123
- '%s': subject
124
+- '%f': sanitized subject line, suitable for a filename
125
- '%b': body
126
- '%Cred': switch color to red
127
- '%Cgreen': switch color to green
pretty.c
+35
@@ -493,6 +493,38 @@ static void parse_commit_header(struct format_commit_context *context)
493
context->commit_header_parsed = 1;
494
}
495
496
+static int istitlechar(char c)
497
+{
498
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
499
+ (c >= '0' && c <= '9') || c == '.' || c == '_';
500
+}
501
+
502
+static void format_sanitized_subject(struct strbuf *sb, const char *msg)
503
+{
504
+ size_t trimlen;
505
+ int space = 2;
506
+
507
+ for (; *msg && *msg != '\n'; msg++) {
508
+ if (istitlechar(*msg)) {
509
+ if (space == 1)
510
+ strbuf_addch(sb, '-');
511
+ space = 0;
512
+ strbuf_addch(sb, *msg);
513
+ if (*msg == '.')
514
+ while (*(msg+1) == '.')
515
+ msg++;
516
+ } else
517
+ space |= 1;
518
+ }
519
+
520
+ /* trim any trailing '.' or '-' characters */
521
+ trimlen = 0;
522
+ while (sb->buf[sb->len - 1 - trimlen] == '.'
523
+ || sb->buf[sb->len - 1 - trimlen] == '-')
524
+ trimlen++;
525
+ strbuf_remove(sb, sb->len - trimlen, trimlen);
526
+}
527
+
528
const char *format_subject(struct strbuf *sb, const char *msg,
529
const char *line_separator)
530
{
@@ -683,6 +715,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
715
case 's': /* subject */
716
format_subject(sb, msg + c->subject_off, " ");
717
return 1;
718
+ case 'f': /* sanitized subject */
719
+ format_sanitized_subject(sb, msg + c->subject_off);
720
+ return 1;
721
case 'b': /* body */
722
strbuf_addstr(sb, msg + c->body_off);
723
return 1;