From fd8d789466d969b3925847b33683d54e9623189a Mon Sep 17 00:00:00 2001 From: AlDanial Date: Sun, 17 Nov 2024 21:08:13 -0800 Subject: [PATCH] comment on timeouts at end of run if encountered #867 extend timeout duration for files with extremely long lines --- Unix/cloc | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ cloc | 29 ++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/Unix/cloc b/Unix/cloc index 6025dcb9..53bad9b5 100755 --- a/Unix/cloc +++ b/Unix/cloc @@ -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 @@ -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; } @@ -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"; @@ -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 () { + 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 , ) = @_; diff --git a/cloc b/cloc index 29d8ae62..77cb2e07 100755 --- a/cloc +++ b/cloc @@ -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 @@ -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; } @@ -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";