Skip to content

Commit

Permalink
apply file filter rules with --no-recurse (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Sep 2, 2024
1 parent 90f5746 commit 1ef8059
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
15 changes: 13 additions & 2 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -5833,19 +5833,30 @@ sub make_file_list { # {{{1
next;
}
if ($opt_no_recurse) {
my @candidate_file_list = ();
if ($ON_WINDOWS and $HAVE_Win32_Long_Path) {
my $d = Win32::LongPath->new();
$d->opendirL($dir);
foreach my $entry ($d->readdirL()) {
my $F = "$dir/$entry";
push @file_list, $F if is_file($F);
push @candidate_file_list, $F if is_file($F);
}
$d->closedirL();
} else {
opendir(DIR, $dir);
push @file_list, grep(is_file($_), readdir(DIR));
push @candidate_file_list, grep(is_file("$dir/$_"), readdir(DIR));
closedir(DIR);
}
foreach my $F (@candidate_file_list) {
# pretend to be File::Find's wanted() routine, namely subroutine files(),
# to apply all file filter rules
$File::Find::dir = $dir;
$File::Find::name = $F;
$_ = "$dir/$F";
files(); # side effect: populates @file_list
}
# ... then prepend the directory name to each file
@file_list = map { "$dir/$_" } @file_list;
} else {
find({wanted => \&files ,
preprocess => \&find_preprocessor,
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 @@ -858,6 +858,13 @@ my @Tests = (
'ref' => '../tests/outputs/issues/833/results.yaml',
},

{
'name' => '--match-f with --no-recurse (github issue #851)',
'cd' => '../tests/inputs/issues/851',
'args' => "--match-f '.lua\$' --no-recurse level_1",
'ref' => '../tests/outputs/issues/851/results.yaml',
},

);

# Special cases:
Expand Down
15 changes: 13 additions & 2 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -5848,19 +5848,30 @@ sub make_file_list { # {{{1
next;
}
if ($opt_no_recurse) {
my @candidate_file_list = ();
if ($ON_WINDOWS and $HAVE_Win32_Long_Path) {
my $d = Win32::LongPath->new();
$d->opendirL($dir);
foreach my $entry ($d->readdirL()) {
my $F = "$dir/$entry";
push @file_list, $F if is_file($F);
push @candidate_file_list, $F if is_file($F);
}
$d->closedirL();
} else {
opendir(DIR, $dir);
push @file_list, grep(is_file($_), readdir(DIR));
push @candidate_file_list, grep(is_file("$dir/$_"), readdir(DIR));
closedir(DIR);
}
foreach my $F (@candidate_file_list) {
# pretend to be File::Find's wanted() routine, namely subroutine files(),
# to apply all file filter rules
$File::Find::dir = $dir;
$File::Find::name = $F;
$_ = "$dir/$F";
files(); # side effect: populates @file_list
}
# ... then prepend the directory name to each file
@file_list = map { "$dir/$_" } @file_list;
} else {
find({wanted => \&files ,
preprocess => \&find_preprocessor,
Expand Down
6 changes: 6 additions & 0 deletions tests/inputs/issues/851/level_1/flatbuffers.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

/* Comment 1 */
table TestTable {
// Comment 2
Item: int32;
}
14 changes: 14 additions & 0 deletions tests/inputs/issues/851/level_1/hello.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- single line comment

--[[
multi
line
comment
]]

--[[
also a comment
--]]

print("hello, world")
print([[not a comment]])
15 changes: 15 additions & 0 deletions tests/inputs/issues/851/level_1/level_2/hello_2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- another instance
-- single line comment

--[[
multi
line
comment
]]

--[[
also a comment
--]]

print("hello 2, world")
print([[not a comment]])
21 changes: 21 additions & 0 deletions tests/outputs/issues/851/results.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 2.02
elapsed_seconds : 0.00290799140930176
n_files : 1
n_lines : 14
files_per_second : 343.879970484545
lines_per_second : 4814.31958678364
report_file : /home/al/git-cloc/tests/outputs/issues/851/results.yaml
'Lua' :
nFiles: 1
blank: 3
comment: 9
code: 2
SUM:
blank: 3
comment: 9
code: 2
nFiles: 1

0 comments on commit 1ef8059

Please sign in to comment.