diff: return const char from output_prefix callback
The diff_options structure has an output_prefix callback for returning a prefix string, but it does so by returning a pointer to a strbuf. This makes the interface awkward. There's no reason the callback should need to use a strbuf, and it creates questions about whether the ownership of the resulting buffer should be transferred to the caller (it should not be, but a recent attempt to clean up this code led to a double-free in some cases). The one advantage we get is that the strbuf contains a ptr/len pair, so we could in theory have a prefix with embedded NULs. But we can observe that none of the existing callbacks would ever produce such a NUL (they are usually just indentation or graph symbols, and even the "--line-prefix" option takes a NUL-terminated string). And anyway, only one caller (the one in log_tree_diff_flush) actually looks at the strbuf length. In every other case we use a helper function which discards the length and just returns the NUL-terminated string. So let's just have the callback return a "const char *" pointer. It's up to the callbacks themselves if they want to use a strbuf under the hood. And now the caller in log_tree_diff_flush() can just use the helper function along with everybody else. That lets us even simplify out the function pointer check, since the helper returns an empty string (technically this does mean we'll sometimes issue an empty fputs() call, but I don't think this code path is hot enough to care about that). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
2011bb4f34
commit
436728fe9d
@ -478,7 +478,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;
|
||||
}
|
||||
@ -506,7 +506,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);
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user