Skip to content

Commit

Permalink
comment on timeouts at end of run if encountered #867
Browse files Browse the repository at this point in the history
extend timeout duration for files with extremely long lines
  • Loading branch information
AlDanial committed Nov 18, 2024
1 parent 62b9c4b commit fd8d789
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Unix/cloc
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,25 @@ if ($opt_count_diff) {
exit if $opt_count_diff > 3;
goto Top_of_Processing_Loop;
}
suggest_remedies_for_errors(\@Errors) if @Errors;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub suggest_remedies_for_errors { # {{{1
my ($raa_errors) = @_; # [ [ numeric error code, filename], .. ]
my $hit_timeout = 0;
foreach my $pair (@{$raa_errors}) {
my ($code, $file) = @{$pair};
$hit_timeout = $code == -5 ? 1 : $hit_timeout;
}
if ($hit_timeout) {
print "\n";
print "One or more files took longer to process than expected.\n";
print "Try rerunning without timeout guards by adding --timeout 0\n";
print "to your command line arguments. See the documentation on\n";
print "the --timeout switch for more information.\n";
print "\n";
}
}
# 1}}}
sub brief_usage { # {{{1
return "
cloc -- Count Lines of Code
Expand Down Expand Up @@ -7301,6 +7320,15 @@ sub rm_comments { # {{{1
# a corresponding */ can cause huge delays so put a timer on this.
my $max_duration_sec = scalar(@lines)/1000.0; # est lines per second
$max_duration_sec = 1.0 if $max_duration_sec < 1;
# add a scale factor for super long lines
my $max_line_len = 0;
foreach my $line (@lines) {
my $len = length($line);
$max_line_len = $len if $len > $max_line_len;
}
if ($max_line_len > 1000) {
$max_duration_sec *= $max_line_len / 2000;
}
if (defined $opt_timeout) {
$max_duration_sec = $opt_timeout if $opt_timeout > 0;
}
Expand All @@ -7312,6 +7340,7 @@ sub rm_comments { # {{{1
@lines = &{$subroutine}(\@lines, @args); # apply filter...
alarm 0;
};
#$@ = "alarm\n"; # to trigger the timeout case in tests
if ($@) {
# timed out
die unless $@ eq "alarm\n";
Expand Down Expand Up @@ -13536,6 +13565,69 @@ sub align_by_pairs { # {{{1
#print Dumper("align_by_pairs", @files_L_minus_dir, @files_R_minus_dir);
#die;
} # 1}}}
sub align_from_git { # {{{1
# have git identify the files that changed, as well as how
my ($L_tag , # in
$R_tag , # in
$ra_added , # out
$ra_removed , # out
$ra_compare_list , # out
$rh_renamed , # out
) = @_;
print "-> align_from_git()\n" if $opt_v > 2;

# On the command line, L_tag and R_tag are commit hashes or tags. Here they are
# replaced with temp directories like /tmp/vGxIL7AWRw and /tmp/BS400yIQEl
my $cmd = "git -c \"safe.directory=*\" --no-pager diff --name-status --diff-filter=ADRM $L_tag $R_tag";

# A = added, D = deleted, M = modified, R = renamed; entries tab separated
# Example:
# M README.md
# R089 package.json dist/pack age.json-AND
# M package-lock.json
# A src/apps/compare-tags-branches-commits-llm-explanation/README.md
# A src/internals/cloc-git/cloc-diff-rel.ts
# D src/internals/cloc-git/cloc-git-diff-rel-between-commits.ts

print $cmd, "\n" if $opt_v > 1;
open(GSIM, "$cmd |") or die "Unable to run $cmd $!";
while (<GSIM>) {
chomp;
my @words = split(/\t/);
#print "align_from_git: words=[@words]\n";
if (scalar(@words) == 2) {
if ($words[0] =~ /^M/) {
( my $right = $words[1] ) =~ s[^${L_tag}/][${R_tag}/];
push @{$ra_compare_list}, [ $words[1], $right ];
} elsif ($words[0] =~ /^A/) {
push @{$ra_added} , $words[1];
} elsif ($words[0] =~ /^D/) {
push @{$ra_removed}, $words[1];
} else {
die "cloc.align_from_git() parse failure with [$_]\n";
}
} elsif (scalar(@words) == 3) {
if ($words[0] =~ /^R/) { # rename
( my $clean_L = $words[1] ) =~ s[^${L_tag}/][];
( my $clean_R = $words[2] ) =~ s[^${R_tag}/][];
$rh_renamed->{ $clean_L } = $clean_R;
push @{$ra_compare_list}, [ $words[1], $words[2] ];
} elsif ($words[0] =~ /^A/) {
push @{$ra_added} , $words[1];
} elsif ($words[0] =~ /^D/) {
push @{$ra_removed}, $words[1];
} else {
die "cloc.align_from_git() parse failure with [$_]\n";
}
} else {
die "Unexpected output from git diff --name-status\n";
}
}
close(GSIM);

print "<- align_from_git()\n" if $opt_v > 2;
return;
} # 1}}}
sub html_header { # {{{1
my ($title , ) = @_;

Expand Down
29 changes: 29 additions & 0 deletions cloc
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,25 @@ if ($opt_count_diff) {
exit if $opt_count_diff > 3;
goto Top_of_Processing_Loop;
}
suggest_remedies_for_errors(\@Errors) if @Errors;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub suggest_remedies_for_errors { # {{{1
my ($raa_errors) = @_; # [ [ numeric error code, filename], .. ]
my $hit_timeout = 0;
foreach my $pair (@{$raa_errors}) {
my ($code, $file) = @{$pair};
$hit_timeout = $code == -5 ? 1 : $hit_timeout;
}
if ($hit_timeout) {
print "\n";
print "One or more files took longer to process than expected.\n";
print "Try rerunning without timeout guards by adding --timeout 0\n";
print "to your command line arguments. See the documentation on\n";
print "the --timeout switch for more information.\n";
print "\n";
}
}
# 1}}}
sub brief_usage { # {{{1
return "
cloc -- Count Lines of Code
Expand Down Expand Up @@ -7291,6 +7310,15 @@ sub rm_comments { # {{{1
# a corresponding */ can cause huge delays so put a timer on this.
my $max_duration_sec = scalar(@lines)/1000.0; # est lines per second
$max_duration_sec = 1.0 if $max_duration_sec < 1;
# add a scale factor for super long lines
my $max_line_len = 0;
foreach my $line (@lines) {
my $len = length($line);
$max_line_len = $len if $len > $max_line_len;
}
if ($max_line_len > 1000) {
$max_duration_sec *= $max_line_len / 2000;
}
if (defined $opt_timeout) {
$max_duration_sec = $opt_timeout if $opt_timeout > 0;
}
Expand All @@ -7302,6 +7330,7 @@ sub rm_comments { # {{{1
@lines = &{$subroutine}(\@lines, @args); # apply filter...
alarm 0;
};
#$@ = "alarm\n"; # to trigger the timeout case in tests
if ($@) {
# timed out
die unless $@ eq "alarm\n";
Expand Down

0 comments on commit fd8d789

Please sign in to comment.