Skip to content

Commit bafd092

Browse files
committed
MERGE: merge PR #67.
Original commit message: Universal diffs as generated by git (for example) have some compatibility problems regarding what lcov --diff expects. This pull request fixes a few of those problems to make it easier to use git diffs. In particular: - Start-of-block lines are now allowed to have comments after the ending @@. Git sometimes uses this to give hints about the diff block location, like the function name. - The diff reader has been changed to track more state and to be a bit more flexible, due to git printing more info lines in between files and in the header, like when using git show. Committed by: Henry Cox ([email protected]) on behalf of Ricardo Garcia ([email protected]) Signed-off-by: Ricardo Garcia <[email protected]>
1 parent 2ffb6c4 commit bafd092

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

bin/lcov

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,9 +2326,16 @@ sub read_diff($)
23262326
my $file_old; # Name of old file in diff section
23272327
my $file_new; # Name of new file in diff section
23282328
my $filename; # Name of common filename of diff section
2329-
my $in_block = 0; # Non-zero while we are inside a diff block
23302329
local *HANDLE; # File handle for reading the diff file
23312330

2331+
use constant {
2332+
IN_META => 0, # Out of any block or relevant header.
2333+
HAVE_OLD => 1, # After reading the --- line.
2334+
HAVE_NEW => 2, # After reading the +++ line after a --- line.
2335+
IN_BLOCK => 3, # Inside a diff block.
2336+
};
2337+
my $state = IN_META; # Non-zero while we are inside a diff block
2338+
23322339
info("Reading diff $diff_file\n");
23332340

23342341
# Check if file exists and is readable
@@ -2355,74 +2362,77 @@ sub read_diff($)
23552362
# --- <filename> <date>
23562363
/^--- (\S+)/ && do {
23572364
$file_old = lcovutil::strip_directories($1, $strip);
2365+
$state = HAVE_OLD;
23582366
last;
23592367
};
23602368
# Filename of new file:
23612369
# +++ <filename> <date>
23622370
/^\+\+\+ (\S+)/ && do {
2363-
# Add last file to resulting hash
2364-
if ($filename) {
2365-
my %new_hash;
2366-
$diff{$filename} = $mapping;
2367-
$mapping = \%new_hash;
2371+
if ($state == HAVE_OLD) {
2372+
# Add last file to resulting hash
2373+
if ($filename) {
2374+
my %new_hash;
2375+
$diff{$filename} = $mapping;
2376+
$mapping = \%new_hash;
2377+
}
2378+
$file_new = lcovutil::strip_directories($1, $strip);
2379+
$filename = $file_old;
2380+
$paths{$filename} = $file_new;
2381+
$num_old = 1;
2382+
$num_new = 1;
2383+
$state = HAVE_NEW;
2384+
} else {
2385+
# +++ line out of place.
2386+
$state = IN_META;
23682387
}
2369-
$file_new = lcovutil::strip_directories($1, $strip);
2370-
$filename = $file_old;
2371-
$paths{$filename} = $file_new;
2372-
$num_old = 1;
2373-
$num_new = 1;
23742388
last;
23752389
};
23762390
# Start of diff block:
23772391
# @@ -old_start,old_num, +new_start,new_num @@
2378-
/^\@\@\s+-(\d+),(\d+)\s+\+(\d+),(\d+)\s+\@\@$/ && do {
2379-
$in_block = 1;
2380-
while ($num_old < $1) {
2381-
$mapping->{$num_new} = $num_old;
2382-
$num_old++;
2383-
$num_new++;
2392+
/^\@\@\s+-(\d+),(\d+)\s+\+(\d+),(\d+)\s+\@\@/ && do {
2393+
if ($state == HAVE_NEW || $state == IN_BLOCK) {
2394+
while ($num_old < $1) {
2395+
$mapping->{$num_new} = $num_old;
2396+
$num_old++;
2397+
$num_new++;
2398+
}
2399+
$state = IN_BLOCK;
2400+
} else {
2401+
# @@ line out of place.
2402+
$state = IN_META;
23842403
}
23852404
last;
23862405
};
23872406
# Unchanged line
2388-
# <line starts with blank>
2389-
/^ / && do {
2390-
if ($in_block == 0) {
2391-
last;
2407+
# <line starts with blank or is empty>
2408+
(/^ / || /^$/) && do {
2409+
if ($state == IN_BLOCK) {
2410+
$mapping->{$num_new} = $num_old;
2411+
$num_old++;
2412+
$num_new++;
2413+
} else {
2414+
# Back to the starting point.
2415+
$state = IN_META;
23922416
}
2393-
$mapping->{$num_new} = $num_old;
2394-
$num_old++;
2395-
$num_new++;
23962417
last;
23972418
};
2398-
# Line as seen in old file
2419+
# Line as seen in old file or new file.
23992420
# <line starts with '-'>
2400-
/^-/ && do {
2401-
if ($in_block == 0) {
2402-
last;
2403-
}
2404-
$num_old++;
2405-
last;
2406-
};
2407-
# Line as seen in new file
2408-
# <line starts with '+'>
2409-
/^\+/ && do {
2410-
if ($in_block == 0) {
2411-
last;
2412-
}
2413-
$num_new++;
2414-
last;
2415-
};
2416-
# Empty line
2417-
/^$/ && do {
2418-
if ($in_block == 0) {
2419-
last;
2421+
/^([-+])/ && do {
2422+
if ($state == IN_BLOCK) {
2423+
if ($1 eq "-") {
2424+
$num_old++;
2425+
} else {
2426+
$num_new++;
2427+
}
2428+
} else {
2429+
# Back to the starting point.
2430+
$state = IN_META;
24202431
}
2421-
$mapping->{$num_new} = $num_old;
2422-
$num_old++;
2423-
$num_new++;
24242432
last;
24252433
};
2434+
# With any other pattern, back to the starting point.
2435+
$state = IN_META;
24262436
}
24272437
}
24282438

0 commit comments

Comments
 (0)