Skip to content

Commit

Permalink
log -L: check range set invariants when we look it up
Browse files Browse the repository at this point in the history
lookup_line_range() is a good place to check that the range sets
satisfy the invariants: they have been computed and set in earlier
iterations, and we now start working with them.

Signed-off-by: Thomas Rast <trast@inf.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Thomas Rast authored and Junio C Hamano committed Apr 5, 2013
1 parent 39410bf commit 4596f19
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions line-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ static int range_cmp(const void *_r, const void *_s)
return 1;
}

/*
* Check that the ranges are non-empty, sorted and non-overlapping
*/
static void range_set_check_invariants(struct range_set *rs)
{
int i;

if (!rs)
return;

if (rs->nr)
assert(rs->ranges[0].start < rs->ranges[0].end);

for (i = 1; i < rs->nr; i++) {
assert(rs->ranges[i-1].end < rs->ranges[i].start);
assert(rs->ranges[i].start < rs->ranges[i].end);
}
}

/*
* Helper: In-place pass of sorting and merging the ranges in the
* range set, to re-establish the invariants after another operation
Expand All @@ -103,6 +122,8 @@ static void sort_and_merge_range_set(struct range_set *rs)
}
assert(o <= rs->nr);
rs->nr = o;

range_set_check_invariants(rs);
}

/*
Expand Down Expand Up @@ -687,8 +708,13 @@ static struct line_log_data *lookup_line_range(struct rev_info *revs,
struct commit *commit)
{
struct line_log_data *ret = NULL;
struct line_log_data *d;

ret = lookup_decoration(&revs->line_log_data, &commit->object);

for (d = ret; d; d = d->next)
range_set_check_invariants(&d->ranges);

return ret;
}

Expand Down

0 comments on commit 4596f19

Please sign in to comment.