-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
cloc
executable file
·18511 lines (16978 loc) · 744 KB
/
cloc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
# cloc -- Count Lines of Code {{{1
# Copyright (C) 2006-2024 Al Danial <[email protected]>
# First release August 2006
#
# Includes code from:
# - SLOCCount v2.26
# http://www.dwheeler.com/sloccount/
# by David Wheeler.
# - Regexp::Common v2017060201
# https://metacpan.org/pod/Regexp::Common
# by Damian Conway and Abigail.
# - Win32::Autoglob 1.01
# https://metacpan.org/pod/Win32::Autoglob
# by Sean M. Burke.
# - Algorithm::Diff 1.1902
# https://metacpan.org/pod/Algorithm::Diff
# by Tye McQueen.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details:
# <http://www.gnu.org/licenses/gpl.txt>.
#
# 1}}}
my $VERSION = "2.03"; # odd number == beta; even number == stable
my $URL = "github.com/AlDanial/cloc"; # 'https://' pushes header too wide
require 5.10.0;
# use modules {{{1
use warnings;
use strict;
use Getopt::Long;
use File::Basename;
use File::Temp qw { tempfile tempdir };
use File::Find;
use File::Path;
use File::Spec;
use IO::File;
use List::Util qw( min max );
use Cwd;
use POSIX qw { strftime ceil};
# Parallel::ForkManager isn't in the standard distribution.
# Use it only if installed, and only if --processes=N is given.
# The module load happens in get_max_processes().
my $HAVE_Parallel_ForkManager = 0;
# Digest::MD5 isn't in the standard distribution. Use it only if installed.
my $HAVE_Digest_MD5 = 0;
eval "use Digest::MD5;";
if (defined $Digest::MD5::VERSION) {
$HAVE_Digest_MD5 = 1;
} else {
warn "Digest::MD5 not installed; will skip file uniqueness checks.\n";
}
# Time::HiRes became standard with Perl 5.8
my $HAVE_Time_HiRes = 0;
eval "use Time::HiRes;";
$HAVE_Time_HiRes = 1 if defined $Time::HiRes::VERSION;
my $HAVE_Rexexp_Common;
# Regexp::Common isn't in the standard distribution. It will
# be installed in a temp directory if necessary.
eval "use Regexp::Common qw ( comment ) ";
if (defined $Regexp::Common::VERSION) {
$HAVE_Rexexp_Common = 1;
} else {
$HAVE_Rexexp_Common = 0;
}
# Uncomment next two lines when building Windows executable with perl2exe
# or if running on a system that already has Regexp::Common.
#use Regexp::Common;
#$HAVE_Rexexp_Common = 1;
#perl2exe_include "Regexp/Common/whitespace.pm"
#perl2exe_include "Regexp/Common/URI.pm"
#perl2exe_include "Regexp/Common/URI/fax.pm"
#perl2exe_include "Regexp/Common/URI/file.pm"
#perl2exe_include "Regexp/Common/URI/ftp.pm"
#perl2exe_include "Regexp/Common/URI/gopher.pm"
#perl2exe_include "Regexp/Common/URI/http.pm"
#perl2exe_include "Regexp/Common/URI/pop.pm"
#perl2exe_include "Regexp/Common/URI/prospero.pm"
#perl2exe_include "Regexp/Common/URI/news.pm"
#perl2exe_include "Regexp/Common/URI/tel.pm"
#perl2exe_include "Regexp/Common/URI/telnet.pm"
#perl2exe_include "Regexp/Common/URI/tv.pm"
#perl2exe_include "Regexp/Common/URI/wais.pm"
#perl2exe_include "Regexp/Common/CC.pm"
#perl2exe_include "Regexp/Common/SEN.pm"
#perl2exe_include "Regexp/Common/number.pm"
#perl2exe_include "Regexp/Common/delimited.pm"
#perl2exe_include "Regexp/Common/profanity.pm"
#perl2exe_include "Regexp/Common/net.pm"
#perl2exe_include "Regexp/Common/zip.pm"
#perl2exe_include "Regexp/Common/comment.pm"
#perl2exe_include "Regexp/Common/balanced.pm"
#perl2exe_include "Regexp/Common/lingua.pm"
#perl2exe_include "Regexp/Common/list.pm"
#perl2exe_include "File/Glob.pm"
use Text::Tabs qw { expand };
use Cwd qw { cwd };
use File::Glob;
# 1}}}
# Usage information, options processing. {{{1
my $ON_WINDOWS = 0;
$ON_WINDOWS = 1 if ($^O =~ /^MSWin/) or ($^O eq "Windows_NT");
if ($ON_WINDOWS and $ENV{'SHELL'}) {
if ($ENV{'SHELL'} =~ m{^/}) {
$ON_WINDOWS = 0; # make Cygwin look like Unix
} else {
$ON_WINDOWS = 1; # MKS defines $SHELL but still acts like Windows
}
}
use Cwd "abs_path";
use File::Spec;
# Fix for issues when runing cloc through a symlink on Windows
# e.g. : it have been installed with Winget
# See https://github.com/AlDanial/cloc/issues/849
if ($ON_WINDOWS) {
my $exec_path = abs_path($0);
if (-l $0) {
$exec_path = abs_path(readlink($0));
}
$0 = $exec_path;
}
my $HAVE_Win32_Long_Path = 0;
# Win32::LongPath is an optional dependency that when available on
# Windows will be used to support reading files past the 255 char
# path length limit.
if ($ON_WINDOWS) {
eval "use Win32::LongPath;";
if (defined $Win32::LongPath::VERSION) {
$HAVE_Win32_Long_Path = 1;
}
}
my $config_file = '';
if ( $ENV{'HOME'} ) {
$config_file = File::Spec->catfile( $ENV{'HOME'}, '.config', 'cloc', 'options.txt');
} elsif ( $ENV{'APPDATA'} and $ON_WINDOWS ) {
$config_file = File::Spec->catfile( $ENV{'APPDATA'}, 'cloc');
}
# $config_file may be updated by check_alternate_config_files()
my $NN = chr(27) . "[0m"; # normal
$NN = "" if $ON_WINDOWS or !(-t STDOUT); # -t STDOUT: is it a terminal?
my $BB = chr(27) . "[1m"; # bold
$BB = "" if $ON_WINDOWS or !(-t STDOUT);
my $script = basename $0;
# Intended for v1.88:
# --git-diff-simindex Git diff strategy #3: use git's similarity index
# (git diff -M --name-status) to identify file pairs
# to compare. This is especially useful to compare
# files that were renamed between the commits.
my $brief_usage = brief_usage();
my $usage = long_usage();
$| = 1; # flush STDOUT
my $start_time = get_time();
my (
$opt_categorized ,
$opt_found ,
@opt_force_lang ,
$opt_lang_no_ext ,
@opt_script_lang ,
$opt_count_diff ,
$opt_diff ,
$opt_diff_alignment ,
$opt_diff_list_file ,
$opt_diff_list_files ,
$opt_diff_timeout ,
$opt_timeout ,
$opt_html ,
$opt_ignored ,
$opt_counted ,
$opt_show_ext ,
$opt_show_lang ,
$opt_progress_rate ,
$opt_print_filter_stages ,
$opt_v ,
$opt_vcs ,
$opt_version ,
$opt_include_content ,
$opt_exclude_content ,
$opt_exclude_lang ,
$opt_exclude_list_file ,
$opt_exclude_dir ,
$opt_explain ,
$opt_include_ext ,
$opt_include_lang ,
$opt_force_lang_def ,
$opt_read_lang_def ,
$opt_write_lang_def ,
$opt_write_lang_def_incl_dup,
$opt_strip_code ,
$opt_strip_comments ,
$opt_original_dir ,
$opt_quiet ,
$opt_report_file ,
$opt_sdir ,
$opt_sum_reports ,
$opt_hide_rate ,
$opt_processes ,
$opt_unicode ,
$opt_no3 , # accept it but don't use it
$opt_3 ,
$opt_extract_with ,
$opt_by_file ,
$opt_by_file_by_lang ,
$opt_by_percent ,
$opt_xml ,
$opt_xsl ,
$opt_yaml ,
$opt_csv ,
$opt_csv_delimiter ,
$opt_fullpath ,
$opt_json ,
$opt_md ,
$opt_match_f ,
@opt_not_match_f ,
$opt_match_d ,
@opt_not_match_d ,
$opt_skip_uniqueness ,
$opt_list_file ,
$opt_help ,
$opt_skip_win_hidden ,
$opt_read_binary_files ,
$opt_sql ,
$opt_sql_append ,
$opt_sql_project ,
$opt_sql_style ,
$opt_inline ,
$opt_exclude_ext ,
$opt_ignore_whitespace ,
$opt_ignore_case ,
$opt_ignore_case_ext ,
$opt_follow_links ,
$opt_autoconf ,
$opt_sum_one ,
$opt_stdin_name ,
$opt_force_on_windows ,
$opt_force_on_unix , # actually forces !$ON_WINDOWS
$opt_show_os ,
$opt_skip_archive ,
$opt_max_file_size , # in MB
$opt_use_sloccount ,
$opt_no_autogen ,
$opt_force_git ,
$opt_git_diff_rel ,
$opt_git_diff_all ,
$opt_git_diff_simindex ,
$opt_config_file ,
$opt_strip_str_comments ,
$opt_file_encoding ,
$opt_docstring_as_code ,
$opt_stat ,
$opt_summary_cutoff ,
$opt_skip_leading ,
$opt_no_recurse ,
$opt_only_count_files ,
$opt_fmt ,
);
my $getopt_success = GetOptions( # {{{1
"by_file|by-file" => \$opt_by_file ,
"by_file_by_lang|by-file-by-lang" => \$opt_by_file_by_lang ,
"categorized=s" => \$opt_categorized ,
"counted=s" => \$opt_counted ,
"include_ext|include-ext=s" => \$opt_include_ext ,
"include_lang|include-lang=s" => \$opt_include_lang ,
"include_content|include-content=s" => \$opt_include_content ,
"exclude_content|exclude-content=s" => \$opt_exclude_content ,
"exclude_lang|exclude-lang=s" => \$opt_exclude_lang ,
"exclude_dir|exclude-dir=s" => \$opt_exclude_dir ,
"exclude_list_file|exclude-list-file=s" => \$opt_exclude_list_file ,
"explain=s" => \$opt_explain ,
"extract_with|extract-with=s" => \$opt_extract_with ,
"found=s" => \$opt_found ,
"count_and_diff|count-and-diff" => \$opt_count_diff ,
"diff" => \$opt_diff ,
"diff-alignment|diff_alignment=s" => \$opt_diff_alignment ,
"diff-timeout|diff_timeout=i" => \$opt_diff_timeout ,
"diff-list-file|diff_list_file=s" => \$opt_diff_list_file ,
"diff-list-files|diff_list_files" => \$opt_diff_list_files ,
"timeout=i" => \$opt_timeout ,
"html" => \$opt_html ,
"ignored=s" => \$opt_ignored ,
"quiet" => \$opt_quiet ,
"force_lang_def|force-lang-def=s" => \$opt_force_lang_def ,
"read_lang_def|read-lang-def=s" => \$opt_read_lang_def ,
"show_ext|show-ext:s" => \$opt_show_ext ,
"show_lang|show-lang:s" => \$opt_show_lang ,
"progress_rate|progress-rate=i" => \$opt_progress_rate ,
"print_filter_stages|print-filter-stages" => \$opt_print_filter_stages ,
"report_file|report-file=s" => \$opt_report_file ,
"out=s" => \$opt_report_file ,
"script_lang|script-lang=s" => \@opt_script_lang ,
"sdir=s" => \$opt_sdir ,
"skip_uniqueness|skip-uniqueness" => \$opt_skip_uniqueness ,
"strip_code|strip-code=s" => \$opt_strip_code ,
"strip_comments|strip-comments=s" => \$opt_strip_comments ,
"original_dir|original-dir" => \$opt_original_dir ,
"sum_reports|sum-reports" => \$opt_sum_reports ,
"hide_rate|hide-rate" => \$opt_hide_rate ,
"processes=n" => \$opt_processes ,
"unicode" => \$opt_unicode ,
"no3" => \$opt_no3 , # ignored
"3" => \$opt_3 ,
"v|verbose:i" => \$opt_v ,
"vcs=s" => \$opt_vcs ,
"version" => \$opt_version ,
"write_lang_def|write-lang-def=s" => \$opt_write_lang_def ,
"write_lang_def_incl_dup|write-lang-def-incl-dup=s" => \$opt_write_lang_def_incl_dup,
"xml" => \$opt_xml ,
"xsl=s" => \$opt_xsl ,
"force_lang|force-lang=s" => \@opt_force_lang ,
"lang_no_ext|lang-no-ext=s" => \$opt_lang_no_ext ,
"yaml" => \$opt_yaml ,
"csv" => \$opt_csv ,
"csv_delimiter|csv-delimiter=s" => \$opt_csv_delimiter ,
"json" => \$opt_json ,
"md" => \$opt_md ,
"fullpath" => \$opt_fullpath ,
"match_f|match-f=s" => \$opt_match_f ,
"not_match_f|not-match-f=s" => \@opt_not_match_f ,
"match_d|match-d=s" => \$opt_match_d ,
"not_match_d|not-match-d=s" => \@opt_not_match_d ,
"list_file|list-file=s" => \$opt_list_file ,
"help" => \$opt_help ,
"skip_win_hidden|skip-win-hidden" => \$opt_skip_win_hidden ,
"read_binary_files|read-binary-files" => \$opt_read_binary_files ,
"sql=s" => \$opt_sql ,
"sql_project|sql-project=s" => \$opt_sql_project ,
"sql_append|sql-append" => \$opt_sql_append ,
"sql_style|sql-style=s" => \$opt_sql_style ,
"inline" => \$opt_inline ,
"exclude_ext|exclude-ext=s" => \$opt_exclude_ext ,
"ignore_whitespace|ignore-whitespace" => \$opt_ignore_whitespace ,
"ignore_case|ignore-case" => \$opt_ignore_case ,
"ignore_case_ext|ignore-case-ext" => \$opt_ignore_case_ext ,
"follow_links|follow-links" => \$opt_follow_links ,
"autoconf" => \$opt_autoconf ,
"sum_one|sum-one" => \$opt_sum_one ,
"by_percent|by-percent=s" => \$opt_by_percent ,
"stdin_name|stdin-name=s" => \$opt_stdin_name ,
"windows" => \$opt_force_on_windows ,
"unix" => \$opt_force_on_unix ,
"show_os|show-os" => \$opt_show_os ,
"skip_archive|skip-archive=s" => \$opt_skip_archive ,
"max_file_size|max-file-size=f" => \$opt_max_file_size ,
"use_sloccount|use-sloccount" => \$opt_use_sloccount ,
"no_autogen|no-autogen" => \$opt_no_autogen ,
"git" => \$opt_force_git ,
"git_diff_rel|git-diff-rel" => \$opt_git_diff_rel ,
"git_diff_all|git-diff-all" => \$opt_git_diff_all ,
# "git_diff_simindex|git-diff-simindex" => \$opt_git_diff_simindex ,
"config=s" => \$opt_config_file ,
"strip_str_comments|strip-str-comments" => \$opt_strip_str_comments ,
"file_encoding|file-encoding=s" => \$opt_file_encoding ,
"docstring_as_code|docstring-as-code" => \$opt_docstring_as_code ,
"stat" => \$opt_stat ,
"summary_cutoff|summary-cutoff=s" => \$opt_summary_cutoff ,
"skip_leading|skip-leading:s" => \$opt_skip_leading ,
"no_recurse|no-recurse" => \$opt_no_recurse ,
"only_count_files|only-count-files" => \$opt_only_count_files ,
"fmt=i" => \$opt_fmt ,
);
# 1}}}
$config_file = $opt_config_file if defined $opt_config_file;
load_from_config_file($config_file, # {{{2
\$opt_by_file ,
\$opt_by_file_by_lang ,
\$opt_categorized ,
\$opt_counted ,
\$opt_include_ext ,
\$opt_include_lang ,
\$opt_include_content ,
\$opt_exclude_content ,
\$opt_exclude_lang ,
\$opt_exclude_dir ,
\$opt_exclude_list_file ,
\$opt_explain ,
\$opt_extract_with ,
\$opt_found ,
\$opt_count_diff ,
\$opt_diff_list_files ,
\$opt_diff ,
\$opt_diff_alignment ,
\$opt_diff_timeout ,
\$opt_timeout ,
\$opt_html ,
\$opt_ignored ,
\$opt_quiet ,
\$opt_force_lang_def ,
\$opt_read_lang_def ,
\$opt_show_ext ,
\$opt_show_lang ,
\$opt_progress_rate ,
\$opt_print_filter_stages ,
\$opt_report_file ,
\@opt_script_lang ,
\$opt_sdir ,
\$opt_skip_uniqueness ,
\$opt_strip_code ,
\$opt_strip_comments ,
\$opt_original_dir ,
\$opt_sum_reports ,
\$opt_hide_rate ,
\$opt_processes ,
\$opt_unicode ,
\$opt_3 ,
\$opt_v ,
\$opt_vcs ,
\$opt_version ,
\$opt_write_lang_def ,
\$opt_write_lang_def_incl_dup,
\$opt_xml ,
\$opt_xsl ,
\@opt_force_lang ,
\$opt_lang_no_ext ,
\$opt_yaml ,
\$opt_csv ,
\$opt_csv_delimiter ,
\$opt_json ,
\$opt_md ,
\$opt_fullpath ,
\$opt_match_f ,
\@opt_not_match_f ,
\$opt_match_d ,
\@opt_not_match_d ,
\$opt_list_file ,
\$opt_help ,
\$opt_skip_win_hidden ,
\$opt_read_binary_files ,
\$opt_sql ,
\$opt_sql_project ,
\$opt_sql_append ,
\$opt_sql_style ,
\$opt_inline ,
\$opt_exclude_ext ,
\$opt_ignore_whitespace ,
\$opt_ignore_case ,
\$opt_ignore_case_ext ,
\$opt_follow_links ,
\$opt_autoconf ,
\$opt_sum_one ,
\$opt_by_percent ,
\$opt_stdin_name ,
\$opt_force_on_windows ,
\$opt_force_on_unix ,
\$opt_show_os ,
\$opt_skip_archive ,
\$opt_max_file_size ,
\$opt_use_sloccount ,
\$opt_no_autogen ,
\$opt_force_git ,
\$opt_strip_str_comments ,
\$opt_file_encoding ,
\$opt_docstring_as_code ,
\$opt_stat ,
); # 2}}} Not pretty. Not at all.
if ($opt_version) {
printf "$VERSION\n";
exit;
}
my $opt_git = 0;
$opt_git = 1 if defined($opt_git_diff_all) or
defined($opt_git_diff_rel) or
(defined($opt_vcs) and ($opt_vcs eq "git"));
$opt_by_file = 1 if defined $opt_by_file_by_lang;
$opt_fmt = 0 unless defined $opt_fmt;
if ($opt_fmt) {
$opt_by_file = 1;
$opt_json = 1;
}
my $CLOC_XSL = "cloc.xsl"; # created with --xsl
$CLOC_XSL = "cloc-diff.xsl" if $opt_diff;
die "\n" unless $getopt_success;
print $usage and exit if $opt_help;
my %Exclude_Language = ();
%Exclude_Language = map { $_ => 1 } split(/,/, $opt_exclude_lang)
if $opt_exclude_lang;
my %Exclude_Dir = ();
%Exclude_Dir = map { $_ => 1 } split(/,/, $opt_exclude_dir )
if $opt_exclude_dir ;
die unless exclude_dir_validates(\%Exclude_Dir);
my %Include_Ext = ();
%Include_Ext = map { $_ => 1 } split(/,/, $opt_include_ext)
if $opt_include_ext;
my %Include_Language = (); # keys are lower case language names
%Include_Language = map { lc($_) => 1 } split(/,/, $opt_include_lang)
if $opt_include_lang;
# Forcibly exclude .svn, .cvs, .hg, .git, .bzr directories. The contents of these
# directories often conflict with files of interest.
$opt_exclude_dir = 1;
$Exclude_Dir{".svn"} = 1;
$Exclude_Dir{".cvs"} = 1;
$Exclude_Dir{".hg"} = 1;
$Exclude_Dir{".git"} = 1;
$Exclude_Dir{".bzr"} = 1;
$Exclude_Dir{".snapshot"} = 1; # NetApp backups
$Exclude_Dir{".config"} = 1;
$opt_count_diff = defined $opt_count_diff ? 1 : 0;
$opt_diff = 1 if $opt_diff_alignment or
$opt_diff_list_file or
$opt_diff_list_files or
$opt_git_diff_rel or
$opt_git_diff_all or
$opt_git_diff_simindex;
$opt_force_git = 1 if $opt_git_diff_rel or
$opt_git_diff_all or
$opt_git_diff_simindex;
$opt_diff_alignment = 0 if $opt_diff_list_file;
$opt_exclude_ext = "" unless $opt_exclude_ext;
$opt_ignore_whitespace = 0 unless $opt_ignore_whitespace;
$opt_ignore_case = 0 unless $opt_ignore_case;
$opt_ignore_case_ext = 0 unless $opt_ignore_case_ext;
$opt_lang_no_ext = 0 unless $opt_lang_no_ext;
$opt_follow_links = 0 unless $opt_follow_links;
if (defined $opt_diff_timeout) {
# if defined but with a value of <= 0, set to 2^31-1 seconds = 68 years
$opt_diff_timeout = 2**31-1 unless $opt_diff_timeout > 0;
} else {
$opt_diff_timeout =10; # seconds
}
if (defined $opt_timeout) {
# if defined but with a value of <= 0, set to 2^31-1 seconds = 68 years
$opt_timeout = 2**31-1 unless $opt_timeout > 0;
# else is computed dynamically, ref $max_duration_sec
}
$opt_csv = 0 unless defined $opt_csv;
$opt_csv = 1 if $opt_csv_delimiter;
$ON_WINDOWS = 1 if $opt_force_on_windows;
$ON_WINDOWS = 0 if $opt_force_on_unix;
$opt_max_file_size = 100 unless $opt_max_file_size;
my $HAVE_SLOCCOUNT_c_count = 0;
if (!$ON_WINDOWS and $opt_use_sloccount) {
# Only bother doing this kludgey test is user explicitly wants
# to use SLOCCount. Debian based systems will hang if just doing
# external_utility_exists("c_count")
# if c_count is in $PATH; c_count expects to have input.
$HAVE_SLOCCOUNT_c_count = external_utility_exists("c_count /bin/sh");
}
if ($opt_use_sloccount) {
if (!$HAVE_SLOCCOUNT_c_count) {
warn "c_count could not be found; ignoring --use-sloccount\n";
$opt_use_sloccount = 0;
} else {
warn "Using c_count, php_count, xml_count, pascal_count from SLOCCount\n";
warn "--diff is disabled with --use-sloccount\n" if $opt_diff;
warn "--count-and-diff is disabled with --use-sloccount\n" if $opt_count_diff;
warn "--unicode is disabled with --use-sloccount\n" if $opt_unicode;
warn "--strip-comments is disabled with --use-sloccount\n" if $opt_strip_comments;
warn "--strip-code is disabled with --use-sloccount\n" if $opt_strip_code;
$opt_diff = 0;
$opt_count_diff = undef;
$opt_unicode = 0;
$opt_strip_comments = 0;
$opt_strip_code = 0;
}
}
die "--strip-comments and --strip-code are mutually exclusive\n" if
$opt_strip_comments and $opt_strip_code;
$opt_vcs = 0 if $opt_force_git;
# replace Windows path separators with /
if ($ON_WINDOWS) {
map { s{\\}{/}g } @ARGV;
if ($opt_git) {
# PowerShell tab expansion automatically prefixes local directories
# with ".\" (now mapped to "./"). git ls-files output does not
# include this. Strip this prefix to permit clean matches.
map { s{^\./}{} } @ARGV;
}
}
my @COUNT_DIFF_ARGV = undef;
my $COUNT_DIFF_report_file = undef;
if ($opt_count_diff and !$opt_diff_list_file) {
die "--count-and-diff requires two arguments; got ", scalar @ARGV, "\n"
if scalar @ARGV != 2;
# prefix with a dummy term so that $opt_count_diff is the
# index into @COUNT_DIFF_ARGV to work on at each pass
@COUNT_DIFF_ARGV = (undef, $ARGV[0],
$ARGV[1],
[$ARGV[0], $ARGV[1]]); # 3rd pass: diff them
$COUNT_DIFF_report_file = $opt_report_file if $opt_report_file;
}
# Options defaults:
$opt_quiet = 1 if ($opt_md or $opt_json or !(-t STDOUT))
and !defined $opt_report_file;
$opt_progress_rate = 100 unless defined $opt_progress_rate;
$opt_progress_rate = 0 if defined $opt_quiet;
if (!defined $opt_v) {
$opt_v = 0;
} elsif (!$opt_v) {
$opt_v = 1;
}
if (defined $opt_xsl) {
$opt_xsl = $CLOC_XSL if $opt_xsl eq "1";
$opt_xml = 1;
}
my $skip_generate_report = 0;
$opt_sql_style = 0 unless defined $opt_sql_style;
$opt_sql = 0 unless $opt_sql_style or defined $opt_sql;
if ($opt_sql eq "-" || $opt_sql eq "1") { # stream SQL output to STDOUT
$opt_quiet = 1;
$skip_generate_report = 1;
$opt_by_file = 1;
$opt_sum_reports = 0;
$opt_progress_rate = 0;
} elsif ($opt_sql) { # write SQL output to a file
$opt_by_file = 1;
$skip_generate_report = 1;
$opt_sum_reports = 0;
}
if ($opt_sql_style) {
$opt_sql_style = lc $opt_sql_style;
if (!grep { lc $_ eq $opt_sql_style } qw ( Oracle Named_Columns )) {
die "'$opt_sql_style' is not a recognized SQL style.\n";
}
}
$opt_by_percent = '' unless defined $opt_by_percent;
if ($opt_by_percent and $opt_by_percent !~ m/^(c|cm|cb|cmb)$/i) {
die "--by-percent must be either 'c', 'cm', 'cb', or 'cmb'\n";
}
$opt_by_percent = lc $opt_by_percent;
if (defined $opt_vcs) {
if ($opt_vcs eq "auto") {
if (is_dir(".git")) {
$opt_vcs = "git";
} elsif (is_dir(".svn")) {
$opt_vcs = "svn";
} else {
warn "--vcs auto: unable to determine versioning system\n";
}
}
if ($opt_vcs eq "git") {
$opt_vcs = "git -c \"safe.directory=*\" ls-files";
my @submodules = invoke_generator("git -c \"safe.directory=*\" 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)
my ($checksum, $dir) = split(' ', $SM, 2);
$dir =~ s/\s $//;
$Exclude_Dir{$dir} = 1;
}
} elsif ($opt_vcs eq "svn") {
$opt_vcs = "svn list -R";
}
}
my $list_no_autogen = 0;
if (defined $opt_no_autogen and scalar @ARGV == 1 and $ARGV[0] eq "list") {
$list_no_autogen = 1;
}
if ($opt_summary_cutoff) {
my $error = summary_cutoff_error($opt_summary_cutoff);
die "$error\n" if $error;
}
if (!$opt_config_file) {
# if not explicitly given, look for a config file in other
# possible locations
my $other_loc = check_alternate_config_files($opt_list_file,
$opt_exclude_list_file, $opt_read_lang_def, $opt_force_lang_def,
$opt_diff_list_file);
$opt_config_file = $other_loc if $other_loc;
}
# --match-d and --not-match-d: if end with a trailing slash, update the
# regex to be either slash or end of line since File::Find::find() will
# not see the trailing slash in leaf directories (#732, #833).
if ($opt_match_d and $opt_match_d =~ m{/$}) {
$opt_match_d =~ s{/$}{(/|\$)};
}
foreach my $nmd (@opt_not_match_d) {
if ($nmd =~ m{/$}) {
$nmd =~ s{/$}{(/|\$)};
}
}
die $brief_usage unless defined $opt_version or
defined $opt_show_lang or
defined $opt_show_ext or
defined $opt_show_os or
defined $opt_write_lang_def or
defined $opt_write_lang_def_incl_dup or
defined $opt_list_file or
defined $opt_diff_list_file or
defined $opt_vcs or
defined $opt_xsl or
defined $opt_explain or
$list_no_autogen or
scalar @ARGV >= 1;
if (!$opt_diff_list_file) {
die "--diff requires two arguments; got ", scalar @ARGV, "\n"
if $opt_diff and !$opt_sum_reports and scalar @ARGV != 2;
die "--diff arguments are identical; nothing done", "\n"
if $opt_diff and !$opt_sum_reports and scalar @ARGV == 2
and $ARGV[0] eq $ARGV[1];
}
my $HAVE_Algorithm_Diff = 0;
# Algorithm::Diff isn't in the standard distribution. It will
# be installed in a temp directory if necessary.
eval "use Algorithm::Diff qw ( sdiff ) ";
if (defined $Algorithm::Diff::VERSION) {
$HAVE_Algorithm_Diff = 1;
} else {
Install_Algorithm_Diff();
}
trick_pp_packer_encode() if $ON_WINDOWS and $opt_file_encoding;
$File::Find::dont_use_nlink = 1 if $opt_stat or top_level_SMB_dir(\@ARGV);
my @git_similarity = (); # only populated with --git-diff-simindex
my %git_metadata = ();
get_git_metadata(\@ARGV, \%git_metadata) if $opt_force_git;
#use Data::Dumper;
#print Dumper(\%git_metadata);
replace_git_hash_with_tarfile(\@ARGV, \@git_similarity);
# 1}}}
# Step 1: Initialize global constants. {{{1
#
my $nFiles_Found = 0; # updated in make_file_list
my (%Language_by_Extension, %Language_by_Script,
%Filters_by_Language, %Not_Code_Extension, %Not_Code_Filename,
%Language_by_File_Type, %Language_by_Filename,
%Scale_Factor, %Known_Binary_Archives,
%Language_by_Prefix, %EOL_Continuation_re,
);
my $ALREADY_SHOWED_HEADER = 0;
my $ALREADY_SHOWED_XML_SECTION = 0;
my %Error_Codes = ( 'Unable to read' => -1,
'Neither file nor directory' => -2,
'Diff error (quoted comments?)' => -3,
'Diff error, exceeded timeout' => -4,
'Line count, exceeded timeout' => -5,
);
my %Extension_Collision = (
'ADSO/IDSM' => [ 'adso' ] ,
'C#/Smalltalk' => [ 'cs' ] ,
'D/dtrace' => [ 'd' ] ,
'F#/Forth' => [ 'fs' ] ,
'Fortran 77/Forth' => [ 'f', 'for' ] ,
'IDL/Qt Project/Prolog/ProGuard' => [ 'pro' ] ,
'Lisp/Julia' => [ 'jl' ] ,
'Lisp/OpenCL' => [ 'cl' ] ,
'MATLAB/Mathematica/Objective-C/MUMPS/Mercury' => [ 'm' ] ,
'Pascal/Pawn' => [ 'p' ] ,
'Pascal/Puppet' => [ 'pp' ] ,
'Perl/Prolog' => [ 'pl', 'PL' ] ,
'PHP/Pascal/Fortran/Pawn' => [ 'inc' ] ,
'Raku/Prolog' => [ 'p6', 'P6' ] ,
'XML-Qt-GTK/Glade' => [ 'ui' ] ,
'TypeScript/Qt Linguist' => [ 'ts' ] ,
'Verilog-SystemVerilog/Coq' => [ 'v' ] ,
'Visual Basic/TeX/Apex Class' => [ 'cls' ] ,
'Scheme/SaltStack' => [ 'sls' ] ,
);
my @Autogen_to_ignore = no_autogen_files($list_no_autogen);
if ($opt_force_lang_def) {
# replace cloc's definitions
read_lang_def(
$opt_force_lang_def , # Sample values:
\%Language_by_Extension, # Language_by_Extension{f} = 'Fortran 77'
\%Language_by_Script , # Language_by_Script{sh} = 'Bourne Shell'
\%Language_by_File_Type , # Language_by_File_Type{makefile} = 'make'
\%Filters_by_Language , # Filters_by_Language{Bourne Shell}[0] =
# [ 'remove_matches' , '^\s*#' ]
\%Not_Code_Extension , # Not_Code_Extension{jpg} = 1
\%Not_Code_Filename , # Not_Code_Filename{README} = 1
\%Scale_Factor , # Scale_Factor{Perl} = 4.0
\%EOL_Continuation_re , # EOL_Continuation_re{C } = '\\$'
);
} else {
set_constants( #
\%Language_by_Extension, # Language_by_Extension{f} = 'Fortran 77'
\%Language_by_Script , # Language_by_Script{sh} = 'Bourne Shell'
\%Language_by_File_Type , # Language_by_File_Type{makefile} = 'make'
\%Language_by_Prefix , # Language_by_Prefix{Dockerfile} = 'Dockerfile'
\%Filters_by_Language , # Filters_by_Language{Bourne Shell}[0] =
# [ 'remove_matches' , '^\s*#' ]
\%Not_Code_Extension , # Not_Code_Extension{jpg} = 1
\%Not_Code_Filename , # Not_Code_Filename{README} = 1
\%Scale_Factor , # Scale_Factor{Perl} = 4.0
\%Known_Binary_Archives, # Known_Binary_Archives{.tar} = 1
\%EOL_Continuation_re , # EOL_Continuation_re{C } = '\\$'
);
if ($opt_no_autogen) {
foreach my $F (@Autogen_to_ignore) { $Not_Code_Filename{ $F } = 1; }
}
}
if ($opt_read_lang_def) {
# augment cloc's definitions (keep cloc's where there are overlaps)
merge_lang_def(
$opt_read_lang_def , # Sample values:
\%Language_by_Extension, # Language_by_Extension{f} = 'Fortran 77'
\%Language_by_Script , # Language_by_Script{sh} = 'Bourne Shell'
\%Language_by_File_Type , # Language_by_File_Type{makefile} = 'make'
\%Filters_by_Language , # Filters_by_Language{Bourne Shell}[0] =
# [ 'remove_matches' , '^\s*#' ]
\%Not_Code_Extension , # Not_Code_Extension{jpg} = 1
\%Not_Code_Filename , # Not_Code_Filename{README} = 1
\%Scale_Factor , # Scale_Factor{Perl} = 4.0
\%EOL_Continuation_re , # EOL_Continuation_re{C } = '\\$'
);
}
if ($opt_lang_no_ext and !defined $Filters_by_Language{$opt_lang_no_ext}) {
die_unknown_lang($opt_lang_no_ext, "--lang-no-ext")
}
check_scale_existence(\%Filters_by_Language, \%Language_by_Extension,
\%Scale_Factor);
my $nCounted = 0;
# Process command line provided extension-to-language mapping overrides.
# Make a hash of known languages in lower case for easier matching.
my %Recognized_Language_lc = (); # key = language name in lc, value = true name
foreach my $language (keys %Filters_by_Language) {
my $lang_lc = lc $language;
$Recognized_Language_lc{$lang_lc} = $language;
}
my %Forced_Extension = (); # file name extensions which user wants to count
my $All_One_Language = 0; # set to !0 if --force-lang's <ext> is missing
foreach my $pair (@opt_force_lang) {
my ($lang, $extension) = split(',', $pair);
my $lang_lc = lc $lang;
if (defined $extension) {
$Forced_Extension{$extension} = $lang;
die_unknown_lang($lang, "--force-lang")
unless $Recognized_Language_lc{$lang_lc};
$Language_by_Extension{$extension} = $Recognized_Language_lc{$lang_lc};
} else {
# the scary case--count everything as this language
$All_One_Language = $Recognized_Language_lc{$lang_lc};
}
}
foreach my $pair (@opt_script_lang) {
my ($lang, $script_name) = split(',', $pair);
my $lang_lc = lc $lang;
if (!defined $script_name) {
die "The --script-lang option requires a comma separated pair of ".
"strings.\n";
}
die_unknown_lang($lang, "--script-lang")
unless $Recognized_Language_lc{$lang_lc};
$Language_by_Script{$script_name} = $Recognized_Language_lc{$lang_lc};
}
# If user provided a language definition file, make sure those
# extensions aren't rejected.
foreach my $ext (%Language_by_Extension) {
next unless defined $Not_Code_Extension{$ext};
delete $Not_Code_Extension{$ext};
}
# If user provided file extensions to ignore, add these to
# the exclusion list.
foreach my $ext (map { $_ => 1 } split(/,/, $opt_exclude_ext ) ) {
$ext = lc $ext if $ON_WINDOWS or $opt_ignore_case_ext;
$Not_Code_Extension{$ext} = 1;
}
# If SQL or --by-file output is requested, keep track of directory names
# generated by File::Temp::tempdir and used to temporarily hold the results
# of compressed archives. Contents of the SQL table 't' will be much
# cleaner if these meaningless directory names are stripped from the front
# of files pulled from the archives.
my %TEMP_DIR = ();
my $TEMP_OFF = 0; # Needed for --sdir; keep track of the number of
# scratch directories made in this run to avoid
# file overwrites by multiple extractions to same
# sdir.
# Also track locations where temporary installations, if necessary, of
# Algorithm::Diff and/or Regexp::Common are done. Make sure these
# directories are not counted as inputs (ref bug #80 2012-11-23).
my %TEMP_INST = ();
# invert %Language_by_Script hash to get an easy-to-look-up list of known
# scripting languages
my %Script_Language = map { $_ => 1 } values %Language_by_Script ;
# 1}}}
# Step 2: Early exits for display, summation. {{{1
#
print_extension_info( $opt_show_ext ) if defined $opt_show_ext ;
print_language_info( $opt_show_lang, '') if defined $opt_show_lang;
print_language_filters( $opt_explain ) if defined $opt_explain ;
exit if (defined $opt_show_ext) or
(defined $opt_show_lang) or
(defined $opt_explain) or
$list_no_autogen;
Top_of_Processing_Loop:
# Sorry, coding purists. Using a goto to implement --count-and-diff
# which has to do three passes over the main code, starting with
# a clean slate each time.
if ($opt_count_diff) {
@ARGV = ( $COUNT_DIFF_ARGV[ $opt_count_diff ] );
if ($opt_count_diff == 3) {
$opt_diff = 1;
@ARGV = @{$COUNT_DIFF_ARGV[ $opt_count_diff ]}; # last arg is list of list
} elsif ($opt_diff_list_files) {
$opt_diff = 0;
}
if ($opt_report_file) {
# Instead of just one output file, will have three.
# Keep their names unique otherwise results are clobbered.
# Replace file path separators with underscores otherwise
# may end up with illegal file names.
my ($fn_0, $fn_1) = (undef, undef);
if ($ON_WINDOWS) {
($fn_0 = $ARGV[0]) =~ s{\\}{_}g;
$fn_0 =~ s{:}{_}g;
$fn_0 =~ s{/}{_}g;
($fn_1 = $ARGV[1]) =~ s{\\}{_}g if defined $ARGV[1];
$fn_1 =~ s{:}{_}g if defined $ARGV[1];
$fn_1 =~ s{/}{_}g if defined $ARGV[1];
} else {
($fn_0 = $ARGV[0]) =~ s{/}{_}g;
($fn_1 = $ARGV[1]) =~ s{/}{_}g if defined $ARGV[1];
}
if ($opt_count_diff == 3) {
$opt_report_file = $COUNT_DIFF_report_file . ".diff.$fn_0.$fn_1";
} else {
$opt_report_file = $COUNT_DIFF_report_file . ".$fn_0";
}
} else {
# STDOUT; print a header showing what it's working on
if ($opt_count_diff == 3) {
print "\ndiff $ARGV[0] $ARGV[1]::\n";
} else {
print "\n" if $opt_count_diff > 1;
print "$ARGV[0]::\n";
}
}
$ALREADY_SHOWED_HEADER = 0;
$ALREADY_SHOWED_XML_SECTION = 0;
}
#print "Before glob have [", join(",", @ARGV), "]\n";
@ARGV = windows_glob(@ARGV) if $ON_WINDOWS;
#print "after glob have [", join(",", @ARGV), "]\n";
# filter out archive files if requested to do so
if (defined $opt_skip_archive) {
my @non_archive = ();
foreach my $candidate (@ARGV) {
if ($candidate !~ m/${opt_skip_archive}$/) {
push @non_archive, $candidate;
}
}
@ARGV = @non_archive;
}
if ($opt_sum_reports and $opt_diff) {
my @results = ();
if ($opt_csv and !defined($opt_csv_delimiter)) {
$opt_csv_delimiter = ",";
}
if ($opt_list_file) { # read inputs from the list file
my @list = read_list_file($opt_list_file);
if ($opt_csv) {
@results = combine_csv_diffs($opt_csv_delimiter, \@list);
} else {
@results = combine_diffs(\@list);
}
} elsif ($opt_vcs) { # read inputs from the VCS generator
my @list = invoke_generator($opt_vcs, \@ARGV);
if ($opt_csv) {
@results = combine_csv_diffs($opt_csv_delimiter, \@list);
} else {
@results = combine_diffs(\@list);
}
} else { # get inputs from the command line
if ($opt_csv) {
@results = combine_csv_diffs($opt_csv_delimiter, \@ARGV);
} else {