Skip to content

Commit bb2c926

Browse files
committed
Merge branch 'sb/diff-color-moved-use-xdl-recmatch' into maint
Instead of using custom line comparison and hashing functions to implement "moved lines" coloring in the diff output, use the pair of these functions from lower-layer xdiff/ code. * sb/diff-color-moved-use-xdl-recmatch: diff.c: get rid of duplicate implementation xdiff-interface: export comparing and hashing strings
2 parents fd50623 + 01be97c commit bb2c926

File tree

3 files changed

+32
-78
lines changed

3 files changed

+32
-78
lines changed

diff.c

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -707,88 +707,14 @@ struct moved_entry {
707707
struct moved_entry *next_line;
708708
};
709709

710-
static int next_byte(const char **cp, const char **endp,
711-
const struct diff_options *diffopt)
712-
{
713-
int retval;
714-
715-
if (*cp >= *endp)
716-
return -1;
717-
718-
if (isspace(**cp)) {
719-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
720-
while (*cp < *endp && isspace(**cp))
721-
(*cp)++;
722-
/*
723-
* After skipping a couple of whitespaces,
724-
* we still have to account for one space.
725-
*/
726-
return (int)' ';
727-
}
728-
729-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
730-
while (*cp < *endp && isspace(**cp))
731-
(*cp)++;
732-
/*
733-
* return the first non-ws character via the usual
734-
* below, unless we ate all of the bytes
735-
*/
736-
if (*cp >= *endp)
737-
return -1;
738-
}
739-
}
740-
741-
retval = (unsigned char)(**cp);
742-
(*cp)++;
743-
return retval;
744-
}
745-
746710
static int moved_entry_cmp(const struct diff_options *diffopt,
747711
const struct moved_entry *a,
748712
const struct moved_entry *b,
749713
const void *keydata)
750714
{
751-
const char *ap = a->es->line, *ae = a->es->line + a->es->len;
752-
const char *bp = b->es->line, *be = b->es->line + b->es->len;
753-
754-
if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
755-
return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
756-
757-
if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
758-
while (ae > ap && isspace(ae[-1]))
759-
ae--;
760-
while (be > bp && isspace(be[-1]))
761-
be--;
762-
}
763-
764-
while (1) {
765-
int ca, cb;
766-
ca = next_byte(&ap, &ae, diffopt);
767-
cb = next_byte(&bp, &be, diffopt);
768-
if (ca != cb)
769-
return 1;
770-
if (ca < 0)
771-
return 0;
772-
}
773-
}
774-
775-
static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
776-
{
777-
if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
778-
static struct strbuf sb = STRBUF_INIT;
779-
const char *ap = es->line, *ae = es->line + es->len;
780-
int c;
781-
782-
strbuf_reset(&sb);
783-
while (ae > ap && isspace(ae[-1]))
784-
ae--;
785-
while ((c = next_byte(&ap, &ae, o)) >= 0)
786-
strbuf_addch(&sb, c);
787-
788-
return memhash(sb.buf, sb.len);
789-
} else {
790-
return memhash(es->line, es->len);
791-
}
715+
return !xdiff_compare_lines(a->es->line, a->es->len,
716+
b->es->line, b->es->len,
717+
diffopt->xdl_opts);
792718
}
793719

794720
static struct moved_entry *prepare_entry(struct diff_options *o,
@@ -797,7 +723,7 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
797723
struct moved_entry *ret = xmalloc(sizeof(*ret));
798724
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
799725

800-
ret->ent.hash = get_string_hash(l, o);
726+
ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
801727
ret->es = l;
802728
ret->next_line = NULL;
803729

xdiff-interface.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "xdiff/xdiffi.h"
66
#include "xdiff/xemit.h"
77
#include "xdiff/xmacros.h"
8+
#include "xdiff/xutils.h"
89

910
struct xdiff_emit_state {
1011
xdiff_emit_consume_fn consume;
@@ -296,6 +297,17 @@ void xdiff_clear_find_func(xdemitconf_t *xecfg)
296297
}
297298
}
298299

300+
unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
301+
{
302+
return xdl_hash_record(&s, s + len, flags);
303+
}
304+
305+
int xdiff_compare_lines(const char *l1, long s1,
306+
const char *l2, long s2, long flags)
307+
{
308+
return xdl_recmatch(l1, s1, l2, s2, flags);
309+
}
310+
299311
int git_xmerge_style = -1;
300312

301313
int git_xmerge_config(const char *var, const char *value, void *cb)

xdiff-interface.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ extern void xdiff_clear_find_func(xdemitconf_t *xecfg);
2929
extern int git_xmerge_config(const char *var, const char *value, void *cb);
3030
extern int git_xmerge_style;
3131

32+
/*
33+
* Compare the strings l1 with l2 which are of size s1 and s2 respectively.
34+
* Returns 1 if the strings are deemed equal, 0 otherwise.
35+
* The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
36+
* are treated for the comparision.
37+
*/
38+
extern int xdiff_compare_lines(const char *l1, long s1,
39+
const char *l2, long s2, long flags);
40+
41+
/*
42+
* Returns a hash of the string s of length len.
43+
* The `flags` given as XDF_WHITESPACE_FLAGS determine how white spaces
44+
* are treated for the hash.
45+
*/
46+
extern unsigned long xdiff_hash_string(const char *s, size_t len, long flags);
47+
3248
#endif

0 commit comments

Comments
 (0)