summaryrefslogtreecommitdiff
path: root/pretty.c
diff options
context:
space:
mode:
Diffstat (limited to 'pretty.c')
-rw-r--r--pretty.c527
1 files changed, 379 insertions, 148 deletions
diff --git a/pretty.c b/pretty.c
index 73b5ead..7ead078 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1,8 +1,13 @@
-#include "cache.h"
+#include "git-compat-util.h"
#include "config.h"
#include "commit.h"
+#include "environment.h"
+#include "gettext.h"
+#include "hash.h"
+#include "hex.h"
#include "utf8.h"
#include "diff.h"
+#include "pager.h"
#include "revision.h"
#include "string-list.h"
#include "mailmap.h"
@@ -13,6 +18,14 @@
#include "gpg-interface.h"
#include "trailer.h"
#include "run-command.h"
+#include "object-name.h"
+
+/*
+ * The limit for formatting directives, which enable the caller to append
+ * arbitrarily many bytes to the formatted buffer. This includes padding
+ * and wrapping formatters.
+ */
+#define FORMATTING_LIMIT (16 * 1024)
static char *user_format;
static struct cmt_fmt_map {
@@ -43,7 +56,9 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
rev->commit_format = CMIT_FMT_USERFORMAT;
}
-static int git_pretty_formats_config(const char *var, const char *value, void *cb)
+static int git_pretty_formats_config(const char *var, const char *value,
+ const struct config_context *ctx UNUSED,
+ void *cb UNUSED)
{
struct cmt_fmt_map *commit_format = NULL;
const char *name;
@@ -132,7 +147,7 @@ static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
for (i = 0; i < commit_formats_len; i++) {
size_t match_len;
- if (!starts_with(commit_formats[i].name, sought))
+ if (!istarts_with(commit_formats[i].name, sought))
continue;
match_len = strlen(commit_formats[i].name);
@@ -413,7 +428,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
}
const char *show_ident_date(const struct ident_split *ident,
- const struct date_mode *mode)
+ struct date_mode mode)
{
timestamp_t date = 0;
long tz = 0;
@@ -431,6 +446,62 @@ const char *show_ident_date(const struct ident_split *ident,
return show_date(date, tz, mode);
}
+static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
+ const char *buf, size_t buflen)
+{
+ strbuf_addstr(sb, color);
+ strbuf_add(sb, buf, buflen);
+ if (*color)
+ strbuf_addstr(sb, GIT_COLOR_RESET);
+}
+
+static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
+ const char *line, size_t linelen,
+ int color, enum grep_context ctx,
+ enum grep_header_field field)
+{
+ const char *buf, *eol, *line_color, *match_color;
+ regmatch_t match;
+ int eflags = 0;
+
+ buf = line;
+ eol = buf + linelen;
+
+ if (!opt || !want_color(color) || opt->invert)
+ goto end;
+
+ line_color = opt->colors[GREP_COLOR_SELECTED];
+ match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
+
+ while (grep_next_match(opt, buf, eol, ctx, &match, field, eflags)) {
+ if (match.rm_so == match.rm_eo)
+ break;
+
+ strbuf_add_with_color(sb, line_color, buf, match.rm_so);
+ strbuf_add_with_color(sb, match_color, buf + match.rm_so,
+ match.rm_eo - match.rm_so);
+ buf += match.rm_eo;
+ eflags = REG_NOTBOL;
+ }
+
+ if (eflags)
+ strbuf_add_with_color(sb, line_color, buf, eol - buf);
+ else {
+end:
+ strbuf_add(sb, buf, eol - buf);
+ }
+}
+
+static int use_in_body_from(const struct pretty_print_context *pp,
+ const struct ident_split *ident)
+{
+ if (pp->rev && pp->rev->force_in_body_from)
+ return 1;
+ if (ident_cmp(pp->from_ident, ident))
+ return 1;
+ return 0;
+}
+
void pp_user_info(struct pretty_print_context *pp,
const char *what, struct strbuf *sb,
const char *line, const char *encoding)
@@ -457,7 +528,7 @@ void pp_user_info(struct pretty_print_context *pp,
map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
if (cmit_fmt_is_mail(pp->fmt)) {
- if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
+ if (pp->from_ident && use_in_body_from(pp, &ident)) {
struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, "From: ");
@@ -496,15 +567,32 @@ void pp_user_info(struct pretty_print_context *pp,
strbuf_addch(sb, '\n');
strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
} else {
- strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
- (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
- (int)namelen, namebuf, (int)maillen, mailbuf);
+ struct strbuf id = STRBUF_INIT;
+ enum grep_header_field field = GREP_HEADER_FIELD_MAX;
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
+
+ if (!strcmp(what, "Author"))
+ field = GREP_HEADER_AUTHOR;
+ else if (!strcmp(what, "Commit"))
+ field = GREP_HEADER_COMMITTER;
+
+ strbuf_addf(sb, "%s: ", what);
+ if (pp->fmt == CMIT_FMT_FULLER)
+ strbuf_addchars(sb, ' ', 4);
+
+ strbuf_addf(&id, "%.*s <%.*s>", (int)namelen, namebuf,
+ (int)maillen, mailbuf);
+
+ append_line_with_color(sb, opt, id.buf, id.len, pp->color,
+ GREP_CONTEXT_HEAD, field);
+ strbuf_addch(sb, '\n');
+ strbuf_release(&id);
}
switch (pp->fmt) {
case CMIT_FMT_MEDIUM:
strbuf_addf(sb, "Date: %s\n",
- show_ident_date(&ident, &pp->date_mode));
+ show_ident_date(&ident, pp->date_mode));
break;
case CMIT_FMT_EMAIL:
case CMIT_FMT_MBOXRD:
@@ -513,7 +601,7 @@ void pp_user_info(struct pretty_print_context *pp,
break;
case CMIT_FMT_FULLER:
strbuf_addf(sb, "%sDate: %s\n", what,
- show_ident_date(&ident, &pp->date_mode));
+ show_ident_date(&ident, pp->date_mode));
break;
default:
/* notin' */
@@ -638,7 +726,7 @@ const char *repo_logmsg_reencode(struct repository *r,
* Otherwise, we still want to munge the encoding header in the
* result, which will be done by modifying the buffer. If we
* are using a fresh copy, we can reuse it. But if we are using
- * the cached copy from get_commit_buffer, we need to duplicate it
+ * the cached copy from repo_get_commit_buffer, we need to duplicate it
* to avoid munging the cached copy.
*/
if (msg == get_cached_commit_buffer(r, commit, NULL))
@@ -671,11 +759,7 @@ const char *repo_logmsg_reencode(struct repository *r,
* If the re-encoding failed, out might be NULL here; in that
* case we just return the commit message verbatim.
*/
- if (!out) {
- warning("unable to reencode commit to '%s'", output_encoding);
- return msg;
- }
- return out;
+ return out ? out : msg;
}
static int mailmap_name(const char **email, size_t *email_len,
@@ -691,7 +775,7 @@ static int mailmap_name(const char **email, size_t *email_len,
static size_t format_person_part(struct strbuf *sb, char part,
const char *msg, int len,
- const struct date_mode *dmode)
+ struct date_mode dmode)
{
/* currently all placeholders have same length */
const int placeholder_len = 2;
@@ -924,7 +1008,9 @@ static void strbuf_wrap(struct strbuf *sb, size_t pos,
if (pos)
strbuf_add(&tmp, sb->buf, pos);
strbuf_add_wrapped_text(&tmp, sb->buf + pos,
- (int) indent1, (int) indent2, (int) width);
+ cast_size_t_to_int(indent1),
+ cast_size_t_to_int(indent2),
+ cast_size_t_to_int(width));
strbuf_swap(&tmp, sb);
strbuf_release(&tmp);
}
@@ -948,7 +1034,7 @@ static void rewrap_message_tail(struct strbuf *sb,
static int format_reflog_person(struct strbuf *sb,
char part,
struct reflog_walk_info *log,
- const struct date_mode *dmode)
+ struct date_mode dmode)
{
const char *ident;
@@ -1050,9 +1136,18 @@ static size_t parse_padding_placeholder(const char *placeholder,
const char *end = start + strcspn(start, ",)");
char *next;
int width;
- if (!end || end == start)
+ if (!*end || end == start)
return 0;
width = strtol(start, &next, 10);
+
+ /*
+ * We need to limit the amount of padding, or otherwise this
+ * would allow the user to pad the buffer by arbitrarily many
+ * bytes and thus cause resource exhaustion.
+ */
+ if (width < -FORMATTING_LIMIT || width > FORMATTING_LIMIT)
+ return 0;
+
if (next == start || width == 0)
return 0;
if (width < 0) {
@@ -1157,6 +1252,27 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud)
return 0;
}
+static struct strbuf *expand_string_arg(struct strbuf *sb,
+ const char *argval, size_t arglen)
+{
+ char *fmt = xstrndup(argval, arglen);
+ const char *format = fmt;
+
+ strbuf_reset(sb);
+ while (strbuf_expand_step(sb, &format)) {
+ size_t len;
+
+ if (skip_prefix(format, "%", &format))
+ strbuf_addch(sb, '%');
+ else if ((len = strbuf_expand_literal(sb, format)))
+ format += len;
+ else
+ strbuf_addch(sb, '%');
+ }
+ free(fmt);
+ return sb;
+}
+
int format_set_trailers_options(struct process_trailer_options *opts,
struct string_list *filter_list,
struct strbuf *sepbuf,
@@ -1185,21 +1301,9 @@ int format_set_trailers_options(struct process_trailer_options *opts,
opts->filter_data = filter_list;
opts->only_trailers = 1;
} else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
- char *fmt;
-
- strbuf_reset(sepbuf);
- fmt = xstrndup(argval, arglen);
- strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL);
- free(fmt);
- opts->separator = sepbuf;
+ opts->separator = expand_string_arg(sepbuf, argval, arglen);
} else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
- char *fmt;
-
- strbuf_reset(kvsepbuf);
- fmt = xstrndup(argval, arglen);
- strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
- free(fmt);
- opts->key_value_separator = kvsepbuf;
+ opts->key_value_separator = expand_string_arg(kvsepbuf, argval, arglen);
} else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
!match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
!match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
@@ -1216,32 +1320,108 @@ int format_set_trailers_options(struct process_trailer_options *opts,
static size_t parse_describe_args(const char *start, struct strvec *args)
{
- const char *options[] = { "match", "exclude" };
+ struct {
+ char *name;
+ enum {
+ DESCRIBE_ARG_BOOL,
+ DESCRIBE_ARG_INTEGER,
+ DESCRIBE_ARG_STRING,
+ } type;
+ } option[] = {
+ { "tags", DESCRIBE_ARG_BOOL},
+ { "abbrev", DESCRIBE_ARG_INTEGER },
+ { "exclude", DESCRIBE_ARG_STRING },
+ { "match", DESCRIBE_ARG_STRING },
+ };
const char *arg = start;
for (;;) {
- const char *matched = NULL;
+ int found = 0;
const char *argval;
size_t arglen = 0;
+ int optval = 0;
int i;
- for (i = 0; i < ARRAY_SIZE(options); i++) {
- if (match_placeholder_arg_value(arg, options[i], &arg,
- &argval, &arglen)) {
- matched = options[i];
+ for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
+ switch (option[i].type) {
+ case DESCRIBE_ARG_BOOL:
+ if (match_placeholder_bool_arg(arg, option[i].name, &arg, &optval)) {
+ if (optval)
+ strvec_pushf(args, "--%s", option[i].name);
+ else
+ strvec_pushf(args, "--no-%s", option[i].name);
+ found = 1;
+ }
+ break;
+ case DESCRIBE_ARG_INTEGER:
+ if (match_placeholder_arg_value(arg, option[i].name, &arg,
+ &argval, &arglen)) {
+ char *endptr;
+ if (!arglen)
+ return 0;
+ strtol(argval, &endptr, 10);
+ if (endptr - argval != arglen)
+ return 0;
+ strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
+ found = 1;
+ }
+ break;
+ case DESCRIBE_ARG_STRING:
+ if (match_placeholder_arg_value(arg, option[i].name, &arg,
+ &argval, &arglen)) {
+ if (!arglen)
+ return 0;
+ strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
+ found = 1;
+ }
break;
}
}
- if (!matched)
+ if (!found)
break;
- if (!arglen)
- return 0;
- strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
}
return arg - start;
}
+
+static int parse_decoration_option(const char **arg,
+ const char *name,
+ char **opt)
+{
+ const char *argval;
+ size_t arglen;
+
+ if (match_placeholder_arg_value(*arg, name, arg, &argval, &arglen)) {
+ struct strbuf sb = STRBUF_INIT;
+
+ expand_string_arg(&sb, argval, arglen);
+ *opt = strbuf_detach(&sb, NULL);
+ return 1;
+ }
+ return 0;
+}
+
+static void parse_decoration_options(const char **arg,
+ struct decoration_options *opts)
+{
+ while (parse_decoration_option(arg, "prefix", &opts->prefix) ||
+ parse_decoration_option(arg, "suffix", &opts->suffix) ||
+ parse_decoration_option(arg, "separator", &opts->separator) ||
+ parse_decoration_option(arg, "pointer", &opts->pointer) ||
+ parse_decoration_option(arg, "tag", &opts->tag))
+ ;
+}
+
+static void free_decoration_options(const struct decoration_options *opts)
+{
+ free(opts->prefix);
+ free(opts->suffix);
+ free(opts->separator);
+ free(opts->pointer);
+ free(opts->tag);
+}
+
static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
const char *placeholder,
void *context)
@@ -1255,7 +1435,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
char **slot;
/* these are independent of the commit */
- res = strbuf_expand_literal_cb(sb, placeholder, NULL);
+ res = strbuf_expand_literal(sb, placeholder);
if (res)
return res;
@@ -1297,6 +1477,16 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
if (*next != ')')
return 0;
}
+
+ /*
+ * We need to limit the format here as it allows the
+ * user to prepend arbitrarily many bytes to the buffer
+ * when rewrapping.
+ */
+ if (width > FORMATTING_LIMIT ||
+ indent1 > FORMATTING_LIMIT ||
+ indent2 > FORMATTING_LIMIT)
+ return 0;
rewrap_message_tail(sb, c, width, indent1, indent2);
return end - placeholder + 1;
} else
@@ -1385,11 +1575,18 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
strbuf_addstr(sb, get_revision_mark(NULL, commit));
return 1;
case 'd':
- format_decorations(sb, commit, c->auto_color);
+ format_decorations(sb, commit, c->auto_color, NULL);
return 1;
case 'D':
- format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
- return 1;
+ {
+ const struct decoration_options opts = {
+ .prefix = "",
+ .suffix = ""
+ };
+
+ format_decorations(sb, commit, c->auto_color, &opts);
+ return 1;
+ }
case 'S': /* tag/branch like --source */
if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
return 0;
@@ -1405,7 +1602,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
if (c->pretty_ctx->reflog_info)
get_reflog_selector(sb,
c->pretty_ctx->reflog_info,
- &c->pretty_ctx->date_mode,
+ c->pretty_ctx->date_mode,
c->pretty_ctx->date_mode_explicit,
(placeholder[1] == 'd'));
return 2;
@@ -1420,7 +1617,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
return format_reflog_person(sb,
placeholder[1],
c->pretty_ctx->reflog_info,
- &c->pretty_ctx->date_mode);
+ c->pretty_ctx->date_mode);
}
return 0; /* unknown %g placeholder */
case 'N':
@@ -1436,8 +1633,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
check_commit_signature(c->commit, &(c->signature_check));
switch (placeholder[1]) {
case 'G':
- if (c->signature_check.gpg_output)
- strbuf_addstr(sb, c->signature_check.gpg_output);
+ if (c->signature_check.output)
+ strbuf_addstr(sb, c->signature_check.output);
break;
case '?':
switch (c->signature_check.result) {
@@ -1478,23 +1675,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
break;
case 'T':
- switch (c->signature_check.trust_level) {
- case TRUST_UNDEFINED:
- strbuf_addstr(sb, "undefined");
- break;
- case TRUST_NEVER:
- strbuf_addstr(sb, "never");
- break;
- case TRUST_MARGINAL:
- strbuf_addstr(sb, "marginal");
- break;
- case TRUST_FULLY:
- strbuf_addstr(sb, "fully");
- break;
- case TRUST_ULTIMATE:
- strbuf_addstr(sb, "ultimate");
- break;
- }
+ strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level));
break;
default:
return 0;
@@ -1502,6 +1683,23 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
return 2;
}
+ if (skip_prefix(placeholder, "(decorate", &arg)) {
+ struct decoration_options opts = { NULL };
+ size_t ret = 0;
+
+ if (*arg == ':') {
+ arg++;
+ parse_decoration_options(&arg, &opts);
+ }
+ if (*arg == ')') {
+ format_decorations(sb, commit, c->auto_color, &opts);
+ ret = arg - placeholder + 1;
+ }
+
+ free_decoration_options(&opts);
+ return ret;
+ }
+
/* For the rest we have to parse the commit header. */
if (!c->commit_header_parsed) {
msg = c->message =
@@ -1514,11 +1712,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
case 'a': /* author ... */
return format_person_part(sb, placeholder[1],
msg + c->author.off, c->author.len,
- &c->pretty_ctx->date_mode);
+ c->pretty_ctx->date_mode);
case 'c': /* committer ... */
return format_person_part(sb, placeholder[1],
msg + c->committer.off, c->committer.len,
- &c->pretty_ctx->date_mode);
+ c->pretty_ctx->date_mode);
case 'e': /* encoding */
if (c->commit_encoding)
strbuf_addstr(sb, c->commit_encoding);
@@ -1561,7 +1759,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
goto trailer_out;
}
if (*arg == ')') {
- format_trailers_from_commit(sb, msg + c->subject_off, &opts);
+ format_trailers_from_commit(&opts, msg + c->subject_off, sb);
ret = arg - placeholder + 1;
}
trailer_out:
@@ -1578,19 +1776,21 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
struct format_commit_context *c)
{
struct strbuf local_sb = STRBUF_INIT;
- int total_consumed = 0, len, padding = c->padding;
+ size_t total_consumed = 0;
+ int len, padding = c->padding;
+
if (padding < 0) {
const char *start = strrchr(sb->buf, '\n');
int occupied;
if (!start)
start = sb->buf;
- occupied = utf8_strnwidth(start, -1, 1);
+ occupied = utf8_strnwidth(start, strlen(start), 1);
occupied += c->pretty_ctx->graph_width;
padding = (-padding) - occupied;
}
while (1) {
int modifier = *placeholder == 'C';
- int consumed = format_commit_one(&local_sb, placeholder, c);
+ size_t consumed = format_commit_one(&local_sb, placeholder, c);
total_consumed += consumed;
if (!modifier)
@@ -1602,7 +1802,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
placeholder++;
total_consumed++;
}
- len = utf8_strnwidth(local_sb.buf, -1, 1);
+ len = utf8_strnwidth(local_sb.buf, local_sb.len, 1);
if (c->flush_type == flush_left_and_steal) {
const char *ch = sb->buf + sb->len - 1;
@@ -1617,7 +1817,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
if (*ch != 'm')
break;
p = ch - 1;
- while (ch - p < 10 && *p != '\033')
+ while (p > sb->buf && ch - p < 10 && *p != '\033')
p--;
if (*p != '\033' ||
ch + 1 - p != display_mode_esc_sequence_len(p))
@@ -1656,7 +1856,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
}
strbuf_addbuf(sb, &local_sb);
} else {
- int sb_len = sb->len, offset = 0;
+ size_t sb_len = sb->len, offset = 0;
if (c->flush_type == flush_left)
offset = padding - len;
else if (c->flush_type == flush_both)
@@ -1677,10 +1877,9 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
const char *placeholder,
- void *context)
+ struct format_commit_context *context)
{
- int consumed;
- size_t orig_len;
+ size_t consumed, orig_len;
enum {
NO_MAGIC,
ADD_LF_BEFORE_NON_EMPTY,
@@ -1701,14 +1900,26 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
default:
break;
}
- if (magic != NO_MAGIC)
+ if (magic != NO_MAGIC) {
placeholder++;
+ switch (placeholder[0]) {
+ case 'w':
+ /*
+ * `%+w()` cannot ever expand to a non-empty string,
+ * and it potentially changes the layout of preceding
+ * contents. We're thus not able to handle the magic in
+ * this combination and refuse the pattern.
+ */
+ return 0;
+ };
+ }
+
orig_len = sb->len;
- if (((struct format_commit_context *)context)->flush_type != no_flush)
- consumed = format_and_pad_commit(sb, placeholder, context);
- else
+ if (context->flush_type == no_flush)
consumed = format_commit_one(sb, placeholder, context);
+ else
+ consumed = format_and_pad_commit(sb, placeholder, context);
if (magic == NO_MAGIC)
return consumed;
@@ -1724,40 +1935,38 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
return consumed + 1;
}
-static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
- void *context)
-{
- struct userformat_want *w = context;
-
- if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
- placeholder++;
-
- switch (*placeholder) {
- case 'N':
- w->notes = 1;
- break;
- case 'S':
- w->source = 1;
- break;
- case 'd':
- case 'D':
- w->decorate = 1;
- break;
- }
- return 0;
-}
-
void userformat_find_requirements(const char *fmt, struct userformat_want *w)
{
- struct strbuf dummy = STRBUF_INIT;
-
if (!fmt) {
if (!user_format)
return;
fmt = user_format;
}
- strbuf_expand(&dummy, fmt, userformat_want_item, w);
- strbuf_release(&dummy);
+ while ((fmt = strchr(fmt, '%'))) {
+ fmt++;
+ if (skip_prefix(fmt, "%", &fmt))
+ continue;
+
+ if (*fmt == '+' || *fmt == '-' || *fmt == ' ')
+ fmt++;
+
+ switch (*fmt) {
+ case 'N':
+ w->notes = 1;
+ break;
+ case 'S':
+ w->source = 1;
+ break;
+ case 'd':
+ case 'D':
+ w->decorate = 1;
+ break;
+ case '(':
+ if (starts_with(fmt + 1, "decorate"))
+ w->decorate = 1;
+ break;
+ }
+ }
}
void repo_format_commit_message(struct repository *r,
@@ -1774,7 +1983,16 @@ void repo_format_commit_message(struct repository *r,
const char *output_enc = pretty_ctx->output_encoding;
const char *utf8 = "UTF-8";
- strbuf_expand(sb, format, format_commit_item, &context);
+ while (strbuf_expand_step(sb, &format)) {
+ size_t len;
+
+ if (skip_prefix(format, "%", &format))
+ strbuf_addch(sb, '%');
+ else if ((len = format_commit_item(sb, format, &context)))
+ format += len;
+ else
+ strbuf_addch(sb, '%');
+ }
rewrap_message_tail(sb, &context, 0, 0, 0);
/*
@@ -1859,11 +2077,11 @@ static void pp_header(struct pretty_print_context *pp,
}
}
-void pp_title_line(struct pretty_print_context *pp,
- const char **msg_p,
- struct strbuf *sb,
- const char *encoding,
- int need_8bit_cte)
+void pp_email_subject(struct pretty_print_context *pp,
+ const char **msg_p,
+ struct strbuf *sb,
+ const char *encoding,
+ int need_8bit_cte)
{
static const int max_length = 78; /* per rfc2047 */
struct strbuf title;
@@ -1873,19 +2091,14 @@ void pp_title_line(struct pretty_print_context *pp,
pp->preserve_subject ? "\n" : " ");
strbuf_grow(sb, title.len + 1024);
- if (pp->print_email_subject) {
- if (pp->rev)
- fmt_output_email_subject(sb, pp->rev);
- if (pp->encode_email_headers &&
- needs_rfc2047_encoding(title.buf, title.len))
- add_rfc2047(sb, title.buf, title.len,
- encoding, RFC2047_SUBJECT);
- else
- strbuf_add_wrapped_bytes(sb, title.buf, title.len,
+ fmt_output_email_subject(sb, pp->rev);
+ if (pp->encode_email_headers &&
+ needs_rfc2047_encoding(title.buf, title.len))
+ add_rfc2047(sb, title.buf, title.len,
+ encoding, RFC2047_SUBJECT);
+ else
+ strbuf_add_wrapped_bytes(sb, title.buf, title.len,
-last_line_length(sb), 1, max_length);
- } else {
- strbuf_addbuf(sb, &title);
- }
strbuf_addch(sb, '\n');
if (need_8bit_cte == 0) {
@@ -1908,9 +2121,8 @@ void pp_title_line(struct pretty_print_context *pp,
if (pp->after_subject) {
strbuf_addstr(sb, pp->after_subject);
}
- if (cmit_fmt_is_mail(pp->fmt)) {
- strbuf_addch(sb, '\n');
- }
+
+ strbuf_addch(sb, '\n');
if (pp->in_body_headers.nr) {
int i;
@@ -1939,8 +2151,9 @@ static int pp_utf8_width(const char *start, const char *end)
return width;
}
-static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
- const char *line, int linelen)
+static void strbuf_add_tabexpand(struct strbuf *sb, struct grep_opt *opt,
+ int color, int tabwidth, const char *line,
+ int linelen)
{
const char *tab;
@@ -1957,7 +2170,9 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
break;
/* Output the data .. */
- strbuf_add(sb, line, tab - line);
+ append_line_with_color(sb, opt, line, tab - line, color,
+ GREP_CONTEXT_BODY,
+ GREP_HEADER_FIELD_MAX);
/* .. and the de-tabified tab */
strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
@@ -1972,7 +2187,8 @@ static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
* worrying about width - there's nothing more to
* align.
*/
- strbuf_add(sb, line, linelen);
+ append_line_with_color(sb, opt, line, linelen, color, GREP_CONTEXT_BODY,
+ GREP_HEADER_FIELD_MAX);
}
/*
@@ -1984,11 +2200,16 @@ static void pp_handle_indent(struct pretty_print_context *pp,
struct strbuf *sb, int indent,
const char *line, int linelen)
{
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
+
strbuf_addchars(sb, ' ', indent);
if (pp->expand_tabs_in_log)
- strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen);
+ strbuf_add_tabexpand(sb, opt, pp->color, pp->expand_tabs_in_log,
+ line, linelen);
else
- strbuf_add(sb, line, linelen);
+ append_line_with_color(sb, opt, line, linelen, pp->color,
+ GREP_CONTEXT_BODY,
+ GREP_HEADER_FIELD_MAX);
}
static int is_mboxrd_from(const char *line, int len)
@@ -2006,7 +2227,9 @@ void pp_remainder(struct pretty_print_context *pp,
struct strbuf *sb,
int indent)
{
+ struct grep_opt *opt = pp->rev ? &pp->rev->grep_filter : NULL;
int first = 1;
+
for (;;) {
const char *line = *msg_p;
int linelen = get_one_line(line);
@@ -2027,14 +2250,17 @@ void pp_remainder(struct pretty_print_context *pp,
if (indent)
pp_handle_indent(pp, sb, indent, line, linelen);
else if (pp->expand_tabs_in_log)
- strbuf_add_tabexpand(sb, pp->expand_tabs_in_log,
- line, linelen);
+ strbuf_add_tabexpand(sb, opt, pp->color,
+ pp->expand_tabs_in_log, line,
+ linelen);
else {
if (pp->fmt == CMIT_FMT_MBOXRD &&
is_mboxrd_from(line, linelen))
strbuf_addch(sb, '>');
- strbuf_add(sb, line, linelen);
+ append_line_with_color(sb, opt, line, linelen,
+ pp->color, GREP_CONTEXT_BODY,
+ GREP_HEADER_FIELD_MAX);
}
strbuf_addch(sb, '\n');
}
@@ -2052,12 +2278,14 @@ void pretty_print_commit(struct pretty_print_context *pp,
int need_8bit_cte = pp->need_8bit_cte;
if (pp->fmt == CMIT_FMT_USERFORMAT) {
- format_commit_message(commit, user_format, sb, pp);
+ repo_format_commit_message(the_repository, commit,
+ user_format, sb, pp);
return;
}
encoding = get_log_output_encoding();
- msg = reencoded = logmsg_reencode(commit, NULL, encoding);
+ msg = reencoded = repo_logmsg_reencode(the_repository, commit, NULL,
+ encoding);
if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
indent = 0;
@@ -2086,7 +2314,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
}
pp_header(pp, encoding, commit, &msg, sb);
- if (pp->fmt != CMIT_FMT_ONELINE && !pp->print_email_subject) {
+ if (pp->fmt != CMIT_FMT_ONELINE && !cmit_fmt_is_mail(pp->fmt)) {
strbuf_addch(sb, '\n');
}
@@ -2094,8 +2322,11 @@ void pretty_print_commit(struct pretty_print_context *pp,
msg = skip_blank_lines(msg);
/* These formats treat the title line specially. */
- if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
- pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
+ if (pp->fmt == CMIT_FMT_ONELINE) {
+ msg = format_subject(sb, msg, " ");
+ strbuf_addch(sb, '\n');
+ } else if (cmit_fmt_is_mail(pp->fmt))
+ pp_email_subject(pp, &msg, sb, encoding, need_8bit_cte);
beginning_of_body = sb->len;
if (pp->fmt != CMIT_FMT_ONELINE)
@@ -2114,7 +2345,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
if (cmit_fmt_is_mail(pp->fmt) && sb->len <= beginning_of_body)
strbuf_addch(sb, '\n');
- unuse_commit_buffer(commit, reencoded);
+ repo_unuse_commit_buffer(the_repository, commit, reencoded);
}
void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,