Skip to content

Commit 6b03fd8

Browse files
committed
Merge branch 'jk/output-prefix-cleanup' into maint-2.47
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
2 parents 304e77d + 1164e27 commit 6b03fd8

File tree

7 files changed

+28
-43
lines changed

7 files changed

+28
-43
lines changed

diff-lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ int index_differs_from(struct repository *r,
701701
return (has_changes != 0);
702702
}
703703

704-
static struct strbuf *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
704+
static const char *idiff_prefix_cb(struct diff_options *opt UNUSED, void *data)
705705
{
706706
return data;
707707
}
@@ -716,7 +716,7 @@ void show_interdiff(const struct object_id *oid1, const struct object_id *oid2,
716716
opts.output_format = DIFF_FORMAT_PATCH;
717717
opts.output_prefix = idiff_prefix_cb;
718718
strbuf_addchars(&prefix, ' ', indent);
719-
opts.output_prefix_data = &prefix;
719+
opts.output_prefix_data = prefix.buf;
720720
diff_setup_done(&opts);
721721

722722
diff_tree_oid(oid1, oid2, "", &opts);

diff.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,12 +2317,9 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
23172317

23182318
const char *diff_line_prefix(struct diff_options *opt)
23192319
{
2320-
struct strbuf *msgbuf;
2321-
if (!opt->output_prefix)
2322-
return "";
2323-
2324-
msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2325-
return msgbuf->buf;
2320+
return opt->output_prefix ?
2321+
opt->output_prefix(opt, opt->output_prefix_data) :
2322+
"";
23262323
}
23272324

23282325
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,
54005397

54015398
BUG_ON_OPT_NEG(unset);
54025399
options->line_prefix = optarg;
5403-
options->line_prefix_length = strlen(options->line_prefix);
54045400
graph_setup_line_prefix(options);
54055401
return 0;
54065402
}

diff.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ typedef void (*add_remove_fn_t)(struct diff_options *options,
9494
typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
9595
struct diff_options *options, void *data);
9696

97-
typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
97+
typedef const char *(*diff_prefix_fn_t)(struct diff_options *opt, void *data);
9898

9999
#define DIFF_FORMAT_RAW 0x0001
100100
#define DIFF_FORMAT_DIFFSTAT 0x0002
@@ -274,7 +274,6 @@ struct diff_options {
274274
const char *single_follow;
275275
const char *a_prefix, *b_prefix;
276276
const char *line_prefix;
277-
size_t line_prefix_length;
278277

279278
/**
280279
* collection of boolean options that affects the operation, but some do

graph.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ static void graph_show_line_prefix(const struct diff_options *diffopt)
7676
if (!diffopt || !diffopt->line_prefix)
7777
return;
7878

79-
fwrite(diffopt->line_prefix,
80-
sizeof(char),
81-
diffopt->line_prefix_length,
82-
diffopt->file);
79+
fputs(diffopt->line_prefix, diffopt->file);
8380
}
8481

8582
static const char **column_colors;
@@ -312,22 +309,28 @@ struct git_graph {
312309
* stored as an index into the array column_colors.
313310
*/
314311
unsigned short default_column_color;
312+
313+
/*
314+
* Scratch buffer for generating prefixes to be used with
315+
* diff_output_prefix_callback().
316+
*/
317+
struct strbuf prefix_buf;
315318
};
316319

317-
static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
320+
static const char *diff_output_prefix_callback(struct diff_options *opt, void *data)
318321
{
319322
struct git_graph *graph = data;
320-
static struct strbuf msgbuf = STRBUF_INIT;
321323

322324
assert(opt);
323325

324-
strbuf_reset(&msgbuf);
326+
if (!graph)
327+
return opt->line_prefix;
328+
329+
strbuf_reset(&graph->prefix_buf);
325330
if (opt->line_prefix)
326-
strbuf_add(&msgbuf, opt->line_prefix,
327-
opt->line_prefix_length);
328-
if (graph)
329-
graph_padding_line(graph, &msgbuf);
330-
return &msgbuf;
331+
strbuf_addstr(&graph->prefix_buf, opt->line_prefix);
332+
graph_padding_line(graph, &graph->prefix_buf);
333+
return graph->prefix_buf.buf;
331334
}
332335

333336
static const struct diff_options *default_diffopt;
@@ -397,6 +400,7 @@ struct git_graph *graph_init(struct rev_info *opt)
397400
* The diff output prefix callback, with this we can make
398401
* all the diff output to align with the graph lines.
399402
*/
403+
strbuf_init(&graph->prefix_buf, 0);
400404
opt->diffopt.output_prefix = diff_output_prefix_callback;
401405
opt->diffopt.output_prefix_data = graph;
402406

@@ -412,6 +416,7 @@ void graph_clear(struct git_graph *graph)
412416
free(graph->new_columns);
413417
free(graph->mapping);
414418
free(graph->old_mapping);
419+
strbuf_release(&graph->prefix_buf);
415420
free(graph);
416421
}
417422

line-log.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -897,16 +897,6 @@ static void print_line(const char *prefix, char first,
897897
fputs("\\ No newline at end of file\n", file);
898898
}
899899

900-
static const char *output_prefix(struct diff_options *opt)
901-
{
902-
if (opt->output_prefix) {
903-
struct strbuf *sb = opt->output_prefix(opt, opt->output_prefix_data);
904-
return sb->buf;
905-
} else {
906-
return "";
907-
}
908-
}
909-
910900
static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *range)
911901
{
912902
unsigned int i, j = 0;
@@ -916,7 +906,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
916906
struct diff_ranges *diff = &range->diff;
917907

918908
struct diff_options *opt = &rev->diffopt;
919-
const char *prefix = output_prefix(opt);
909+
const char *prefix = diff_line_prefix(opt);
920910
const char *c_reset = diff_get_color(opt->use_color, DIFF_RESET);
921911
const char *c_frag = diff_get_color(opt->use_color, DIFF_FRAGINFO);
922912
const char *c_meta = diff_get_color(opt->use_color, DIFF_METAINFO);
@@ -1011,7 +1001,7 @@ static void dump_diff_hacky_one(struct rev_info *rev, struct line_log_data *rang
10111001
*/
10121002
static void dump_diff_hacky(struct rev_info *rev, struct line_log_data *range)
10131003
{
1014-
const char *prefix = output_prefix(&rev->diffopt);
1004+
const char *prefix = diff_line_prefix(&rev->diffopt);
10151005

10161006
fprintf(rev->diffopt.file, "%s\n", prefix);
10171007

log-tree.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,12 +922,7 @@ int log_tree_diff_flush(struct rev_info *opt)
922922
* diff/diffstat output for readability.
923923
*/
924924
int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
925-
if (opt->diffopt.output_prefix) {
926-
struct strbuf *msg = NULL;
927-
msg = opt->diffopt.output_prefix(&opt->diffopt,
928-
opt->diffopt.output_prefix_data);
929-
fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
930-
}
925+
fputs(diff_line_prefix(&opt->diffopt), opt->diffopt.file);
931926

932927
/*
933928
* We may have shown three-dashes line early

range-diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static void patch_diff(const char *a, const char *b,
480480
diff_flush(diffopt);
481481
}
482482

483-
static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
483+
static const char *output_prefix_cb(struct diff_options *opt UNUSED, void *data)
484484
{
485485
return data;
486486
}
@@ -508,7 +508,7 @@ static void output(struct string_list *a, struct string_list *b,
508508
opts.flags.suppress_hunk_header_line_count = 1;
509509
opts.output_prefix = output_prefix_cb;
510510
strbuf_addstr(&indent, " ");
511-
opts.output_prefix_data = &indent;
511+
opts.output_prefix_data = indent.buf;
512512
diff_setup_done(&opts);
513513

514514
/*

0 commit comments

Comments
 (0)