2006-04-05 11:03:58 +02:00
|
|
|
#ifndef XDIFF_INTERFACE_H
|
|
|
|
#define XDIFF_INTERFACE_H
|
|
|
|
|
2016-09-05 22:08:02 +02:00
|
|
|
#include "cache.h"
|
2006-04-05 11:03:58 +02:00
|
|
|
#include "xdiff/xdiff.h"
|
|
|
|
|
xdiff: reject files larger than ~1GB
The xdiff code is not prepared to handle extremely large
files. It uses "int" in many places, which can overflow if
we have a very large number of lines or even bytes in our
input files. This can cause us to produce incorrect diffs,
with no indication that the output is wrong. Or worse, we
may even underallocate a buffer whose size is the result of
an overflowing addition.
We're much better off to tell the user that we cannot diff
or merge such a large file. This patch covers both cases,
but in slightly different ways:
1. For merging, we notice the large file and cleanly fall
back to a binary merge (which is effectively "we cannot
merge this").
2. For diffing, we make the binary/text distinction much
earlier, and in many different places. For this case,
we'll use the xdi_diff as our choke point, and reject
any diff there before it hits the xdiff code.
This means in most cases we'll die() immediately after.
That's not ideal, but in practice we shouldn't
generally hit this code path unless the user is trying
to do something tricky. We already consider files
larger than core.bigfilethreshold to be binary, so this
code would only kick in when that is circumvented
(either by bumping that value, or by using a
.gitattribute to mark a file as diffable).
In other words, we can avoid being "nice" here, because
there is already nice code that tries to do the right
thing. We are adding the suspenders to the nice code's
belt, so notice when it has been worked around (both to
protect the user from malicious inputs, and because it
is better to die() than generate bogus output).
The maximum size was chosen after experimenting with feeding
large files to the xdiff code. It's just under a gigabyte,
which leaves room for two obvious cases:
- a diff3 merge conflict result on files of maximum size X
could be 3*X plus the size of the markers, which would
still be only about 3G, which fits in a 32-bit int.
- some of the diff code allocates arrays of one int per
record. Even if each file consists only of blank lines,
then a file smaller than 1G will have fewer than 1G
records, and therefore the int array will fit in 4G.
Since the limit is arbitrary anyway, I chose to go under a
gigabyte, to leave a safety margin (e.g., we would not want
to overflow by allocating "(records + 1) * sizeof(int)" or
similar.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-09-25 01:12:45 +02:00
|
|
|
/*
|
|
|
|
* xdiff isn't equipped to handle content over a gigabyte;
|
|
|
|
* we make the cutoff 1GB - 1MB to give some breathing
|
|
|
|
* room for constant-sized additions (e.g., merge markers)
|
|
|
|
*/
|
|
|
|
#define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
|
|
|
|
|
2018-11-02 07:35:45 +01:00
|
|
|
typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
|
|
|
|
typedef void (*xdiff_emit_hunk_fn)(void *data,
|
|
|
|
long old_begin, long old_nr,
|
|
|
|
long new_begin, long new_nr,
|
|
|
|
const char *func, long funclen);
|
2006-04-05 11:03:58 +02:00
|
|
|
|
2007-12-13 22:25:07 +01:00
|
|
|
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
|
Make xdi_diff_outf interface for running xdiff_outf diffs
To prepare for the need to initialize and release resources for an
xdi_diff with the xdiff_outf output function, make a new function to
wrap this usage.
Old:
ecb.outf = xdiff_outf;
ecb.priv = &state;
...
xdi_diff(file_p, file_o, &xpp, &xecfg, &ecb);
New:
xdi_diff_outf(file_p, file_o, &state.xm, &xpp, &xecfg, &ecb);
Signed-off-by: Brian Downing <bdowning@lavos.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-08-14 07:36:50 +02:00
|
|
|
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
|
2018-11-02 07:35:45 +01:00
|
|
|
xdiff_emit_hunk_fn hunk_fn,
|
|
|
|
xdiff_emit_line_fn line_fn,
|
|
|
|
void *consume_callback_data,
|
2010-05-04 22:41:34 +02:00
|
|
|
xpparam_t const *xpp, xdemitconf_t const *xecfg);
|
2006-12-20 17:37:07 +01:00
|
|
|
int read_mmfile(mmfile_t *ptr, const char *filename);
|
2016-09-05 22:08:02 +02:00
|
|
|
void read_mmblob(mmfile_t *ptr, const struct object_id *oid);
|
2007-06-05 04:36:11 +02:00
|
|
|
int buffer_is_binary(const char *ptr, unsigned long size);
|
2006-04-05 11:03:58 +02:00
|
|
|
|
2008-09-19 00:42:48 +02:00
|
|
|
extern void xdiff_set_find_func(xdemitconf_t *xecfg, const char *line, int cflags);
|
2009-07-02 00:01:43 +02:00
|
|
|
extern void xdiff_clear_find_func(xdemitconf_t *xecfg);
|
2008-08-29 19:49:56 +02:00
|
|
|
extern int git_xmerge_config(const char *var, const char *value, void *cb);
|
|
|
|
extern int git_xmerge_style;
|
2007-07-06 09:45:10 +02:00
|
|
|
|
2018-11-02 07:36:06 +01:00
|
|
|
/*
|
|
|
|
* Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
|
|
|
|
* one just sends the hunk line to the line_fn callback).
|
|
|
|
*/
|
|
|
|
void discard_hunk_line(void *priv,
|
|
|
|
long ob, long on, long nb, long nn,
|
|
|
|
const char *func, long funclen);
|
|
|
|
|
2017-10-25 20:49:11 +02:00
|
|
|
/*
|
|
|
|
* Compare the strings l1 with l2 which are of size s1 and s2 respectively.
|
|
|
|
* Returns 1 if the strings are deemed equal, 0 otherwise.
|
|
|
|
* The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
|
|
|
|
* are treated for the comparision.
|
|
|
|
*/
|
|
|
|
extern int xdiff_compare_lines(const char *l1, long s1,
|
|
|
|
const char *l2, long s2, long flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a hash of the string s of length len.
|
|
|
|
* The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
|
|
|
|
* are treated for the hash.
|
|
|
|
*/
|
|
|
|
extern unsigned long xdiff_hash_string(const char *s, size_t len, long flags);
|
|
|
|
|
2006-04-05 11:03:58 +02:00
|
|
|
#endif
|