Skip to content

Commit

Permalink
support --ignore-regex with --diff, #862, #865, #868
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Dec 14, 2024
1 parent 0774046 commit 804cd9e
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 39 deletions.
63 changes: 47 additions & 16 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,12 @@ sub count_filesets { # {{{1
foreach (@comm_L) { $_ = lc }
foreach (@comm_R) { $_ = lc }
}
my $n_code_removed_L = 0;
my $n_code_removed_R = 0;
@code_L = apply_ignores(\@code_L, $Lang_L, $file_L, $rha_ignore_regex,
\$n_code_removed_L);
@code_R = apply_ignores(\@code_R, $Lang_R, $file_R, $rha_ignore_regex,
\$n_code_removed_R);
# step 3: compute code diffs
array_diff("$file_L v. $file_R" , # in
\@code_L , # in
Expand Down Expand Up @@ -3154,8 +3160,40 @@ sub count_filesets { # {{{1
"n_filepairs_compared" => $n_file_pairs_compared
}
} # 1}}}
sub apply_ignores { # {{{1
my ($ra_lines , # in
$language , # in
$file , # in
$rha_ignore_regex , # in
$rs_lines_code_removed, # out
) = @_;
my @output = ();
print "-> apply_ignores($file, $language)\n" if $opt_v > 2;

if (%{$rha_ignore_regex} and defined($rha_ignore_regex->{$language})) {
foreach my $line (@{$ra_lines}) {
my $keep = 1;
foreach my $regex (@{$rha_ignore_regex->{$language}}) {
if ($line =~ m{$regex}) {
print "apply_ignores reject '$line' in $file because of '$regex'\n"
if $opt_v > 4;
$keep = 0;
++${$rs_lines_code_removed};
last;
}
}
push @output, $line if $keep;
}
} else {
@output = @{$ra_lines};
}

print "<- apply_ignores(${$rs_lines_code_removed})\n" if $opt_v > 2;
return @output;
} # 1}}}
sub write_alignment_data { # {{{1
my ($filename, $n_filepairs_compared, $data ) = @_;
print "-> write_alignment_data($filename)\n" if $opt_v > 2;
my @output = ();
if ( $data->{'added'} ) {
my %added_lines = %{$data->{'added'}};
Expand All @@ -3181,9 +3219,12 @@ sub write_alignment_data { # {{{1
}
}
write_file($filename, {}, @output);
print "<- write_alignment_data\n" if $opt_v > 2;
} # 1}}}
sub exclude_dir_validates { # {{{1
my ($rh_Exclude_Dir) = @_;
# $opt_v hasn't been set yet
# print "-> exclude_dir_validates\n" if $opt_v > 2;
my $is_OK = 1;
foreach my $dir (keys %{$rh_Exclude_Dir}) {
if (($ON_WINDOWS and $dir =~ m{\\}) or ($dir =~ m{/})) {
Expand All @@ -3194,6 +3235,7 @@ sub exclude_dir_validates { # {{{1
if (!$is_OK) {
warn "Use '--fullpath --not-match-d=REGEX' instead\n";
}
# print "<- exclude_dir_validates\n" if $opt_v > 2;
return $is_OK;
} # 1}}}
sub process_exclude_list_file { # {{{1
Expand Down Expand Up @@ -6966,23 +7008,12 @@ sub call_counter { # {{{1
@lines = rm_comments(\@lines, $language, $file,
\%EOL_Continuation_re, $ra_Errors);

if (%{$rha_ignore_regex} and defined($rha_ignore_regex->{$language})) {
my @keep_lines = ();
foreach my $line (@lines) {
my $keep = 1;
foreach my $regex (@{$rha_ignore_regex->{$language}}) {
if ($line =~ m{$regex}) {
print "reject '$line' in $file because of '$regex'\n" if $opt_v > 4;
$keep = 0;
last;
}
}
push @keep_lines, $line if $keep;
}
@lines = @keep_lines;
}
my $n_code_removed = 0;
@lines = apply_ignores(\@lines, $language, $file, $rha_ignore_regex,
\$n_code_removed);
$total_lines -= $n_code_removed;

my $comment_lines = $total_lines - $blank_lines - scalar @lines;
my $comment_lines = $total_lines - $blank_lines - scalar @lines;
if ($opt_strip_comments) {
my $stripped_file = "";
if ($opt_original_dir) {
Expand Down
7 changes: 7 additions & 0 deletions Unix/t/01_opts.t
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,13 @@ my @Tests = (
'ref' => '../tests/outputs/issues/862/results.yaml',
},

{
'name' => '--ignore-regex --diff (github issues #862, #865, #868)',
'cd' => '../tests/inputs/issues/862',
'args' => '--ignore-regex="*|import" --diff a.py b.py',
'ref' => '../tests/outputs/issues/862/diff_results.yaml',
},

);

# Special cases:
Expand Down
63 changes: 47 additions & 16 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -3030,6 +3030,12 @@ sub count_filesets { # {{{1
foreach (@comm_L) { $_ = lc }
foreach (@comm_R) { $_ = lc }
}
my $n_code_removed_L = 0;
my $n_code_removed_R = 0;
@code_L = apply_ignores(\@code_L, $Lang_L, $file_L, $rha_ignore_regex,
\$n_code_removed_L);
@code_R = apply_ignores(\@code_R, $Lang_R, $file_R, $rha_ignore_regex,
\$n_code_removed_R);
# step 3: compute code diffs
array_diff("$file_L v. $file_R" , # in
\@code_L , # in
Expand Down Expand Up @@ -3144,8 +3150,40 @@ sub count_filesets { # {{{1
"n_filepairs_compared" => $n_file_pairs_compared
}
} # 1}}}
sub apply_ignores { # {{{1
my ($ra_lines , # in
$language , # in
$file , # in
$rha_ignore_regex , # in
$rs_lines_code_removed, # out
) = @_;
my @output = ();
print "-> apply_ignores($file, $language)\n" if $opt_v > 2;

if (%{$rha_ignore_regex} and defined($rha_ignore_regex->{$language})) {
foreach my $line (@{$ra_lines}) {
my $keep = 1;
foreach my $regex (@{$rha_ignore_regex->{$language}}) {
if ($line =~ m{$regex}) {
print "apply_ignores reject '$line' in $file because of '$regex'\n"
if $opt_v > 4;
$keep = 0;
++${$rs_lines_code_removed};
last;
}
}
push @output, $line if $keep;
}
} else {
@output = @{$ra_lines};
}

print "<- apply_ignores(${$rs_lines_code_removed})\n" if $opt_v > 2;
return @output;
} # 1}}}
sub write_alignment_data { # {{{1
my ($filename, $n_filepairs_compared, $data ) = @_;
print "-> write_alignment_data($filename)\n" if $opt_v > 2;
my @output = ();
if ( $data->{'added'} ) {
my %added_lines = %{$data->{'added'}};
Expand All @@ -3171,9 +3209,12 @@ sub write_alignment_data { # {{{1
}
}
write_file($filename, {}, @output);
print "<- write_alignment_data\n" if $opt_v > 2;
} # 1}}}
sub exclude_dir_validates { # {{{1
my ($rh_Exclude_Dir) = @_;
# $opt_v hasn't been set yet
# print "-> exclude_dir_validates\n" if $opt_v > 2;
my $is_OK = 1;
foreach my $dir (keys %{$rh_Exclude_Dir}) {
if (($ON_WINDOWS and $dir =~ m{\\}) or ($dir =~ m{/})) {
Expand All @@ -3184,6 +3225,7 @@ sub exclude_dir_validates { # {{{1
if (!$is_OK) {
warn "Use '--fullpath --not-match-d=REGEX' instead\n";
}
# print "<- exclude_dir_validates\n" if $opt_v > 2;
return $is_OK;
} # 1}}}
sub process_exclude_list_file { # {{{1
Expand Down Expand Up @@ -6956,23 +6998,12 @@ sub call_counter { # {{{1
@lines = rm_comments(\@lines, $language, $file,
\%EOL_Continuation_re, $ra_Errors);

if (%{$rha_ignore_regex} and defined($rha_ignore_regex->{$language})) {
my @keep_lines = ();
foreach my $line (@lines) {
my $keep = 1;
foreach my $regex (@{$rha_ignore_regex->{$language}}) {
if ($line =~ m{$regex}) {
print "reject '$line' in $file because of '$regex'\n" if $opt_v > 4;
$keep = 0;
last;
}
}
push @keep_lines, $line if $keep;
}
@lines = @keep_lines;
}
my $n_code_removed = 0;
@lines = apply_ignores(\@lines, $language, $file, $rha_ignore_regex,
\$n_code_removed);
$total_lines -= $n_code_removed;

my $comment_lines = $total_lines - $blank_lines - scalar @lines;
my $comment_lines = $total_lines - $blank_lines - scalar @lines;
if ($opt_strip_comments) {
my $stripped_file = "";
if ($opt_original_dir) {
Expand Down
21 changes: 21 additions & 0 deletions tests/inputs/issues/862/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
import numpy as np

from pylab import gca

labels = ["Baseline", "System"]
data = [3.75 , 4.75]
error = [0.3497 , 0.3108]

xlocations = na.array(range(len(data)))+0.5
width = 0.6
# bar chart with hollow bars:
bar(xlocations, data, yerr=error, width=width, edgecolor='k', facecolor='none')
yticks(range(0, 8))
xticks(xlocations+ width/2, labels)
xlim(0, xlocations[-1]+width*2)
title("Average Ratings on the Training Set")
gca().get_xaxis().tick_bottom()
gca().get_yaxis().tick_left()

show()
21 changes: 21 additions & 0 deletions tests/inputs/issues/862/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
import numpy.numarray as na

from pylab import *

labels = ["Baseline", "System"]
data = [3.75 , 4.75]
error = [0.3497 , 0.3108]

xlocations = na.array(range(len(data)))+0.5
width = 0.5
# bar chart with hollow bars:
bar(xlocations, data, yerr=error, width=width, edgecolor='k', facecolor='none')
yticks(range(0, 8))
xticks(xlocations+ width/2, labels)
xlim(0, xlocations[-1]+width*2)
title("Average Ratings on the Training Set")
gca().get_xaxis().tick_bottom()
gca().get_yaxis().tick_left()

show()
56 changes: 56 additions & 0 deletions tests/outputs/issues/862/diff_results.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 2.03
elapsed_seconds : 0.00476408004760742
n_files : 1
n_lines : 15
files_per_second : 209.904113702332
lines_per_second : 3148.56170553498
report_file : ../../../outputs/issues/862/diff_results.yaml
added :
'Python' :
comment : 0
code : 0
blank : 0
nFiles : 0
same :
'Python' :
comment : 1
code : 13
blank : 0
nFiles : 0
modified :
'Python' :
comment : 0
code : 1
blank : 0
nFiles : 1
removed :
'Python' :
comment : 0
code : 0
blank : 0
nFiles : 0
SUM :
added :
nFiles : 0
comment : 0
code : 0
blank : 0
same :
nFiles : 0
comment : 1
code : 13
blank : 0
modified :
nFiles : 1
comment : 0
code : 1
blank : 0
removed :
nFiles : 0
comment : 0
code : 0
blank : 0
14 changes: 7 additions & 7 deletions tests/outputs/issues/862/results.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 2.03
elapsed_seconds : 0.0048370361328125
elapsed_seconds : 0.00425577163696289
n_files : 2
n_lines : 16
files_per_second : 413.476340694006
lines_per_second : 3307.81072555205
n_lines : 11
files_per_second : 469.950028011204
lines_per_second : 2584.72515406162
report_file : results.yaml
'C' :
nFiles: 1
blank: 1
comment: 4
comment: 0
code: 5
'Fortran 77' :
nFiles: 1
blank: 1
comment: 3
comment: 2
code: 2
SUM:
blank: 2
comment: 7
comment: 2
code: 7
nFiles: 2

0 comments on commit 804cd9e

Please sign in to comment.