Skip to content

Commit 0e488f1

Browse files
phillipwoodgitster
authored andcommitted
diff --color-moved: shrink potential moved blocks as we go
Rather than setting `match` to NULL and then looping over the list of potential matched blocks for a second time to remove blocks with no matches just filter out the blocks with no matches as we go. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ff046a0 commit 0e488f1

File tree

1 file changed

+8
-36
lines changed

1 file changed

+8
-36
lines changed

diff.c

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -996,12 +996,12 @@ static void add_lines_to_move_detection(struct diff_options *o,
996996
static void pmb_advance_or_null(struct diff_options *o,
997997
struct emitted_diff_symbol *l,
998998
struct moved_block *pmb,
999-
int pmb_nr)
999+
int *pmb_nr)
10001000
{
1001-
int i;
1001+
int i, j;
10021002
unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
10031003

1004-
for (i = 0; i < pmb_nr; i++) {
1004+
for (i = 0, j = 0; i < *pmb_nr; i++) {
10051005
int match;
10061006
struct moved_entry *prev = pmb[i].match;
10071007
struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1015,38 +1015,12 @@ static void pmb_advance_or_null(struct diff_options *o,
10151015
match = cur &&
10161016
xdiff_compare_lines(cur->es->line, cur->es->len,
10171017
l->line, l->len, flags);
1018-
if (match)
1019-
pmb[i].match = cur;
1020-
else
1021-
moved_block_clear(&pmb[i]);
1022-
}
1023-
}
1024-
1025-
static int shrink_potential_moved_blocks(struct moved_block *pmb,
1026-
int pmb_nr)
1027-
{
1028-
int lp, rp;
1029-
1030-
/* Shrink the set of potential block to the remaining running */
1031-
for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
1032-
while (lp < pmb_nr && pmb[lp].match)
1033-
lp++;
1034-
/* lp points at the first NULL now */
1035-
1036-
while (rp > -1 && !pmb[rp].match)
1037-
rp--;
1038-
/* rp points at the last non-NULL */
1039-
1040-
if (lp < pmb_nr && rp > -1 && lp < rp) {
1041-
pmb[lp] = pmb[rp];
1042-
memset(&pmb[rp], 0, sizeof(pmb[rp]));
1043-
rp--;
1044-
lp++;
1018+
if (match) {
1019+
pmb[j] = pmb[i];
1020+
pmb[j++].match = cur;
10451021
}
10461022
}
1047-
1048-
/* Remember the number of running sets */
1049-
return rp + 1;
1023+
*pmb_nr = j;
10501024
}
10511025

10521026
static void fill_potential_moved_blocks(struct diff_options *o,
@@ -1181,9 +1155,7 @@ static void mark_color_as_moved(struct diff_options *o,
11811155
continue;
11821156
}
11831157

1184-
pmb_advance_or_null(o, l, pmb, pmb_nr);
1185-
1186-
pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1158+
pmb_advance_or_null(o, l, pmb, &pmb_nr);
11871159

11881160
if (pmb_nr == 0) {
11891161
int contiguous = adjust_last_block(o, n, block_length);

0 commit comments

Comments
 (0)