Skip to content

Commit 1762224

Browse files
committed
Merge branch 'tr/line-log'
Fix "log -L" command line parsing bugs. * tr/line-log: t4211: fix incorrect rebase at f8395ed (range-set: satisfy non-empty ranges invariant) line-log: fix "log -LN" crash when N is last line of file range-set: satisfy non-empty ranges invariant t4211: demonstrate crash when first -L encountered is empty range t4211: demonstrate empty -L range crash range-set: fix sort_and_merge_range_set() corner case bug
2 parents 6a90778 + d3a486c commit 1762224

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

line-log.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ static void range_set_check_invariants(struct range_set *rs)
110110
static void sort_and_merge_range_set(struct range_set *rs)
111111
{
112112
int i;
113-
int o = 1; /* output cursor */
113+
int o = 0; /* output cursor */
114114

115115
qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
116116

117-
for (i = 1; i < rs->nr; i++) {
118-
if (rs->ranges[i].start <= rs->ranges[o-1].end) {
117+
for (i = 0; i < rs->nr; i++) {
118+
if (rs->ranges[i].start == rs->ranges[i].end)
119+
continue;
120+
if (o > 0 && rs->ranges[i].start <= rs->ranges[o-1].end) {
119121
if (rs->ranges[o-1].end < rs->ranges[i].end)
120122
rs->ranges[o-1].end = rs->ranges[i].end;
121123
} else {
@@ -297,6 +299,7 @@ static void line_log_data_insert(struct line_log_data **list,
297299
p = xcalloc(1, sizeof(struct line_log_data));
298300
p->path = path;
299301
range_set_append(&p->ranges, begin, end);
302+
sort_and_merge_range_set(&p->ranges);
300303
if (ip) {
301304
p->next = ip->next;
302305
ip->next = p;

t/t4211-line-log.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,17 @@ test_bad_opts "-L 1,1000:b.c" "has only.*lines"
6464
test_bad_opts "-L :b.c" "argument.*not of the form"
6565
test_bad_opts "-L :foo:b.c" "no match"
6666

67+
# There is a separate bug when an empty -L range is the first -L encountered,
68+
# thus to demonstrate this particular bug, the empty -L range must follow a
69+
# non-empty -L range.
70+
test_expect_success '-L {empty-range} (any -L)' '
71+
n=$(expr $(wc -l <b.c) + 1) &&
72+
git log -L1,1:b.c -L$n:b.c
73+
'
74+
75+
test_expect_success '-L {empty-range} (first -L)' '
76+
n=$(expr $(wc -l <b.c) + 1) &&
77+
git log -L$n:b.c
78+
'
79+
6780
test_done

0 commit comments

Comments
 (0)