Skip to content

Commit a64e6a4

Browse files
peffgitster
authored andcommitted
diff: clarify textconv interface
The memory allocation scheme for the textconv interface is a bit tricky, and not well documented. It was originally designed as an internal part of diff.c (matching fill_mmfile), but gradually was made public. Refactoring it is difficult, but we can at least improve the situation by documenting the intended flow and enforcing it with an in-code assertion. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a2558fb commit a64e6a4

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

diff.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4996,7 +4996,7 @@ size_t fill_textconv(struct userdiff_driver *driver,
49964996
{
49974997
size_t size;
49984998

4999-
if (!driver || !driver->textconv) {
4999+
if (!driver) {
50005000
if (!DIFF_FILE_VALID(df)) {
50015001
*outbuf = "";
50025002
return 0;
@@ -5007,6 +5007,9 @@ size_t fill_textconv(struct userdiff_driver *driver,
50075007
return df->size;
50085008
}
50095009

5010+
if (!driver->textconv)
5011+
die("BUG: fill_textconv called with non-textconv driver");
5012+
50105013
if (driver->textconv_cache && df->sha1_valid) {
50115014
*outbuf = notes_cache_get(driver->textconv_cache, df->sha1,
50125015
&size);

diff.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,26 @@ extern void diff_no_index(struct rev_info *, int, const char **, const char *);
342342

343343
extern int index_differs_from(const char *def, int diff_flags);
344344

345+
/*
346+
* Fill the contents of the filespec "df", respecting any textconv defined by
347+
* its userdiff driver. The "driver" parameter must come from a
348+
* previous call to get_textconv(), and therefore should either be NULL or have
349+
* textconv enabled.
350+
*
351+
* Note that the memory ownership of the resulting buffer depends on whether
352+
* the driver field is NULL. If it is, then the memory belongs to the filespec
353+
* struct. If it is non-NULL, then "outbuf" points to a newly allocated buffer
354+
* that should be freed by the caller.
355+
*/
345356
extern size_t fill_textconv(struct userdiff_driver *driver,
346357
struct diff_filespec *df,
347358
char **outbuf);
348359

360+
/*
361+
* Look up the userdiff driver for the given filespec, and return it if
362+
* and only if it has textconv enabled (otherwise return NULL). The result
363+
* can be passed to fill_textconv().
364+
*/
349365
extern struct userdiff_driver *get_textconv(struct diff_filespec *one);
350366

351367
extern int parse_rename_score(const char **cp_p);

userdiff.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ int userdiff_config(const char *k, const char *v);
2323
struct userdiff_driver *userdiff_find_by_name(const char *name);
2424
struct userdiff_driver *userdiff_find_by_path(const char *path);
2525

26+
/*
27+
* Initialize any textconv-related fields in the driver and return it, or NULL
28+
* if it does not have textconv enabled at all.
29+
*/
2630
struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver);
2731

2832
#endif /* USERDIFF */

0 commit comments

Comments
 (0)