Merge branch 'jk/output-prefix-cleanup'

Code clean-up.

* jk/output-prefix-cleanup:
  diff: store graph prefix buf in git_graph struct
  diff: return line_prefix directly when possible
  diff: return const char from output_prefix callback
  diff: drop line_prefix_length field
  line-log: use diff_line_prefix() instead of custom helper
This commit is contained in:
Junio C Hamano 2024-10-10 14:22:29 -07:00
commit 3eb4cc451e
7 changed files with 28 additions and 43 deletions

View File

@ -701,7 +701,7 @@ int index_differs_from(struct repository *r,
return (has_changes != 0);
}
static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
static const char *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
{
return data;
}
@ -716,7 +716,7 @@ void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
opts.output_format = DIFF_FORMAT_PATCH;
opts.output_prefix = idiff_prefix_cb;
strbuf_addchars(&prefix, ' ', indent);
opts.output_prefix_data = &prefix;
opts.output_prefix_data = prefix.buf;
diff_setup_done(&opts);
diff_tree_oid(oid1, oid2, "", &opts);

10
diff.c
View File

@ -2317,12 +2317,9 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
const char *diff_line_prefix(struct diff_options *opt)
{
struct strbuf *msgbuf;
if (!opt->output_prefix)
return "";
msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
return msgbuf->buf;
return opt->output_prefix ?
opt->output_prefix(opt, opt->output_prefix_data) :
"";
}
static unsigned long sane_truncate_line(char *line, unsigned long len)
@ -5400,7 +5397,6 @@ static int diff_opt_line_prefix(const struct option *opt,
BUG_ON_OPT_NEG(unset);
options->line_prefix = optarg;
options->line_prefix_length = strlen(options->line_prefix);
graph_setup_line_prefix(options);
return 0;
}

3
diff.h
View File

@ -94,7 +94,7 @@ typedef void (*add_remove_fn_t)(struct diff_options *options,
typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
struct diff_options *options, void *data);
typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
typedef const char *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
#define DIFF_FORMAT_RAW 0x0001
#define DIFF_FORMAT_DIFFSTAT 0x0002
@ -274,7 +274,6 @@ struct diff_options {
const char *single_follow;
const char *a_prefix, *b_prefix;
const char *line_prefix;
size_t line_prefix_length;
/**
* collection of boolean options that affects the operation, but some do

29
graph.c
View File

@ -76,10 +76,7 @@ static void graph_show_line_prefix(const struct diff_options *diffopt)
if (!diffopt || !diffopt->line_prefix)
return;
fwrite(diffopt->line_prefix,
sizeof(char),
diffopt->line_prefix_length,
diffopt->file);
fputs(diffopt->line_prefix, diffopt->file);
}
static const char **column_colors;
@ -312,22 +309,28 @@ struct git_graph {
* stored as an index into the array column_colors.
*/
unsigned short default_column_color;
/*
* Scratch buffer for generating prefixes to be used with
* diff_output_prefix_callback().
*/
struct strbuf prefix_buf;
};
static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
static const char *diff_output_prefix_callback(struct diff_options *opt, void *data)
{
struct git_graph *graph = data;
static struct strbuf msgbuf = STRBUF_INIT;
assert(opt);
strbuf_reset(&msgbuf);
if (!graph)
return opt->line_prefix;
strbuf_reset(&graph->prefix_buf);
if (opt->line_prefix)
strbuf_add(&msgbuf, opt->line_prefix,
opt->line_prefix_length);
if (graph)
graph_padding_line(graph, &msgbuf);
return &msgbuf;
strbuf_addstr(&graph->prefix_buf, opt->line_prefix);
graph_padding_line(graph, &graph->prefix_buf);
return graph->prefix_buf.buf;
}
static const struct diff_options *default_diffopt;
@ -397,6 +400,7 @@ struct git_graph *graph_init(struct rev_info *opt)
* The diff output prefix callback, with this we can make
* all the diff output to align with the graph lines.
*/
strbuf_init(&graph->prefix_buf, 0);
opt->diffopt.output_prefix = diff_output_prefix_callback;
opt->diffopt.output_prefix_data = graph;
@ -412,6 +416,7 @@ void graph_clear(struct git_graph *graph)
free(graph->new_columns);
free(graph->mapping);
free(graph->old_mapping);
strbuf_release(&graph->prefix_buf);
free(graph);
}

View File

@ -900,16 +900,6 @@ static void print_line(const char *prefix, char first,
fputs("\\ No newline at end of file\n", file);
}
static const char *output_prefix(struct diff_options *opt)
{
if (opt->output_prefix) {
struct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);
return sb->buf;
} else {
return "";
}
}
static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)
{
unsigned int i, j = 0;
@ -919,7 +909,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
struct diff_ranges *diff = &range->diff;
struct diff_options *opt = &rev->diffopt;
const char *prefix = output_prefix(opt);
const char *prefix = diff_line_prefix(opt);
const char *c_reset = diff_get_color(opt->use_color, DIFF_RESET);
const char *c_frag = diff_get_color(opt->use_color, DIFF_FRAGINFO);
const char *c_meta = diff_get_color(opt->use_color, DIFF_METAINFO);
@ -1014,7 +1004,7 @@ out:
*/
static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)
{
const char *prefix = output_prefix(&rev->diffopt);
const char *prefix = diff_line_prefix(&rev->diffopt);
fprintf(rev->diffopt.file, "%s\n", prefix);

View File

@ -922,12 +922,7 @@ int log_tree_diff_flush(struct rev_info *opt)
* diff/diffstat output for readability.
*/
int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
if (opt->diffopt.output_prefix) {
struct strbuf *msg = NULL;
msg = opt->diffopt.output_prefix(&opt->diffopt,
opt->diffopt.output_prefix_data);
fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
}
fputs(diff_line_prefix(&opt->diffopt), opt->diffopt.file);
/*
* We may have shown three-dashes line early

View File

@ -480,7 +480,7 @@ static void patch_diff(const char *a, const char *b,
diff_flush(diffopt);
}
static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
static const char *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
{
return data;
}
@ -508,7 +508,7 @@ static void output(struct string_list *a, struct string_list *b,
opts.flags.suppress_hunk_header_line_count = 1;
opts.output_prefix = output_prefix_cb;
strbuf_addstr(&indent, " ");
opts.output_prefix_data = &indent;
opts.output_prefix_data = indent.buf;
diff_setup_done(&opts);
/*