Skip to content

Commit

Permalink
support directory args to --vcs-git, #772
Browse files Browse the repository at this point in the history
  • Loading branch information
AlDanial committed Oct 13, 2023
1 parent b385408 commit 46655b4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 41 deletions.
68 changes: 47 additions & 21 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ if (defined $opt_vcs) {
}
if ($opt_vcs eq "git") {
$opt_vcs = "git ls-files";
my @submodules = invoke_generator('git submodule status');
my @submodules = invoke_generator('git submodule status', \@ARGV);
foreach my $SM (@submodules) {
$SM =~ s/^\s+//; # may have leading space
$SM =~ s/\(\S+\)\s*$//; # may end with something like (heads/master)
Expand Down Expand Up @@ -3506,7 +3506,7 @@ sub xml_yaml_or_json_header { # {{{1
${Q}cloc_url${Q} : ${Q}$URL${Q}${C}
${Q}cloc_version${Q} : ${Q}$version${Q}${C}
${Q}n_files${Q} : $sum_files${C}
${Q}n_lines${Q} : $sum_lines${C}";
${Q}n_lines${Q} : $sum_lines";
} else {
$header = "${start}${Q}header${Q} : $open_B
${Q}cloc_url${Q} : ${Q}$URL${Q}${C}
Expand Down Expand Up @@ -5696,30 +5696,56 @@ sub invoke_generator { # {{{1
# If user provided file/directory inputs, only return
# generated files that are in user's request.
# Populates global variable %Ignored.
print "-> invoke_generator($generator)\n" if $opt_v > 2;
open(FH, "$generator |") or
die "Failed to pipe $generator: $!";
print "-> invoke_generator($generator, @{$ra_user_inputs})\n" if $opt_v > 2;
my $start_dir = cwd();
my @dir_list = ();
my @file_list = ();

if (!@{$ra_user_inputs}) {
# input must be a generator command, ie "find -type f -name '*.c'"
# issued from the cwd
push @dir_list, ".";
}
foreach my $file_dir (@{$ra_user_inputs}) {
if (is_dir($file_dir)) {
push @dir_list, $file_dir;
} elsif (is_file($file_dir)) {
push @file_list, $file_dir;
}
}

my @files = ();
while(<FH>) {
chomp;
my $F = $_;
print "VCS input: $F\n" if $opt_v >= 2;
if (!defined $ra_user_inputs or !@{$ra_user_inputs}) {
push @files, $F;
} else {
# is this file desired?
my $want_this_one = 0;
foreach my $file_dir (@{$ra_user_inputs}) {
$file_dir =~ s{\\}{/}g if $ON_WINDOWS;
if (/^$file_dir/) {
$want_this_one = 1;
last;
foreach my $work_dir (@dir_list) {
if ($work_dir ne $start_dir) {
chdir $work_dir or die "Failed to chdir to $work_dir: $!";
}
open(FH, "$generator |") or die "Failed to pipe $generator: $!";
while(<FH>) {
chomp;
my $F = "$work_dir/$_";
print "VCS input: $F\n" if $opt_v >= 2;
if (!@file_list) {
# no files given on the command line; accept all
push @files, $F;
} else {
# is this file desired?
my $want_this_one = 0;
foreach my $file (@file_list) {
$file =~ s{\\}{/}g if $ON_WINDOWS;
if (/^$file/) {
$want_this_one = 1;
last;
}
}
push @files, $F if $want_this_one;
}
push @files, $F if $want_this_one;
}
close(FH);
if ($work_dir ne $start_dir) {
chdir $start_dir or die "Failed to chdir to $start_dir: $!";
}
}
close(FH);

# apply match/not-match file/dir filters to the list so far
my @post_filter = ();
foreach my $F (@files) {
Expand Down
7 changes: 7 additions & 0 deletions Unix/t/02_git.t
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ my @Tests = (
'cd' => 'cloc_submodule_test',
},

{
'name' => '--vcs=git from non-git directory, #772',
'args' => '--vcs=git cloc_submodule_test',
'ref' => '../tests/outputs/issues/772/results.yaml',
'cd' => '.',
},

);

my $Verbose = 0;
Expand Down
66 changes: 46 additions & 20 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ if (defined $opt_vcs) {
}
if ($opt_vcs eq "git") {
$opt_vcs = "git ls-files";
my @submodules = invoke_generator('git submodule status');
my @submodules = invoke_generator('git submodule status', \@ARGV);
foreach my $SM (@submodules) {
$SM =~ s/^\s+//; # may have leading space
$SM =~ s/\(\S+\)\s*$//; # may end with something like (heads/master)
Expand Down Expand Up @@ -5711,30 +5711,56 @@ sub invoke_generator { # {{{1
# If user provided file/directory inputs, only return
# generated files that are in user's request.
# Populates global variable %Ignored.
print "-> invoke_generator($generator)\n" if $opt_v > 2;
open(FH, "$generator |") or
die "Failed to pipe $generator: $!";
print "-> invoke_generator($generator, @{$ra_user_inputs})\n" if $opt_v > 2;
my $start_dir = cwd();
my @dir_list = ();
my @file_list = ();

if (!@{$ra_user_inputs}) {
# input must be a generator command, ie "find -type f -name '*.c'"
# issued from the cwd
push @dir_list, ".";
}
foreach my $file_dir (@{$ra_user_inputs}) {
if (is_dir($file_dir)) {
push @dir_list, $file_dir;
} elsif (is_file($file_dir)) {
push @file_list, $file_dir;
}
}

my @files = ();
while(<FH>) {
chomp;
my $F = $_;
print "VCS input: $F\n" if $opt_v >= 2;
if (!defined $ra_user_inputs or !@{$ra_user_inputs}) {
push @files, $F;
} else {
# is this file desired?
my $want_this_one = 0;
foreach my $file_dir (@{$ra_user_inputs}) {
$file_dir =~ s{\\}{/}g if $ON_WINDOWS;
if (/^$file_dir/) {
$want_this_one = 1;
last;
foreach my $work_dir (@dir_list) {
if ($work_dir ne $start_dir) {
chdir $work_dir or die "Failed to chdir to $work_dir: $!";
}
open(FH, "$generator |") or die "Failed to pipe $generator: $!";
while(<FH>) {
chomp;
my $F = "$work_dir/$_";
print "VCS input: $F\n" if $opt_v >= 2;
if (!@file_list) {
# no files given on the command line; accept all
push @files, $F;
} else {
# is this file desired?
my $want_this_one = 0;
foreach my $file (@file_list) {
$file =~ s{\\}{/}g if $ON_WINDOWS;
if (/^$file/) {
$want_this_one = 1;
last;
}
}
push @files, $F if $want_this_one;
}
push @files, $F if $want_this_one;
}
close(FH);
if ($work_dir ne $start_dir) {
chdir $start_dir or die "Failed to chdir to $start_dir: $!";
}
}
close(FH);

# apply match/not-match file/dir filters to the list so far
my @post_filter = ();
foreach my $F (@files) {
Expand Down
41 changes: 41 additions & 0 deletions tests/outputs/issues/772/results.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
# github.com/AlDanial/cloc
header :
cloc_url : github.com/AlDanial/cloc
cloc_version : 1.99
elapsed_seconds : 0.0455169677734375
n_files : 6
n_lines : 655
files_per_second : 131.81897418706
lines_per_second : 14390.2380154207
report_file : ../tests/outputs/issues/772/results.yaml
'C' :
nFiles: 2
blank: 70
comment: 46
code: 231
'C++' :
nFiles: 1
blank: 49
comment: 55
code: 182
'MATLAB' :
nFiles: 1
blank: 0
comment: 1
code: 2
'Markdown' :
nFiles: 1
blank: 0
comment: 0
code: 2
'Python' :
nFiles: 1
blank: 4
comment: 11
code: 2
SUM:
blank: 123
comment: 113
code: 419
nFiles: 6

0 comments on commit 46655b4

Please sign in to comment.