-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathccdiff
executable file
·1346 lines (1057 loc) · 38.6 KB
/
ccdiff
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
594
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
package App::ccdiff;
use 5.014000;
use warnings;
use charnames ();
use File::Find;
use Encode qw( encode );
use List::Util qw( first max );
use Term::ANSIColor qw(:constants color );
use Getopt::Long qw(:config bundling noignorecase);
our $VERSION = "0.35";
our $CMD = $0 =~ s{.*/}{}r;
sub usage {
my $err = shift and select STDERR;
say "usage: $CMD [options] file1 [file2]";
say " $CMD [options] dir1 dir2";
say " $CMD --man | --info";
say " file1 or file2 can be - (but not both)";
say " -V --version Show version and exit";
say " -v[1] --verbose[=1] Set verbosity";
say " Diff options:";
say " -U --utf-8 Input is in UTF-8";
say " -u[3] --unified=3 Show a unified diff";
say " --no-header Skip header with file names/stamps";
say " -I --index Add indices to the change chunks";
say " -I n --index=4 Only show chunk n";
say " -w --ignore-all-space Ignore all whitespace";
say " -b --ignore-space-change Ignore horizontal whitespace changes";
say " -Z --ignore-trailing-space Ignore whitespace at line ending";
say " -B --ignore-blank-lines Ignore changes where lines are all blank";
say " -i --ignore-case Ignore case changes";
say " --dc=C --diff-class=C Select Algorithm::Diff (AD or PP)";
say " or Algorithm::Diff::XS (XS = default)";
say " Other options:";
say " -t n --threshold=2 Horizontal line diff threshold";
say " -h n --heuristics=n Horizontal char diff threshold";
say " -e n --ellipsis=n Compress horizontal equal sections";
say " -m --markers Use markers to indicate change positions";
say " -a --ascii Use ASCII instead of Unicode indicators";
say " --no-color Reset all colors to none.";
say " --list-colors List available colors and exit";
say " --old=red Color to indicate removed content";
say " --new=green Color to indicate added content";
say " --bg=white Background color for colored indicators";
say " -p --pink Shortcut for --old=magenta";
say " -r --reverse Reverse/invert the colors of the indicators";
say " -s --swap Swap old/new color indicators";
say " --settings Show default settings (after reading rc)";
exit $err;
} # usage
my %rc = (
ascii => 0,
bg => "white",
chr_cml => "\x{21b1}",
chr_cmr => "\x{21b0}",
chr_eli => "\x{2508}",
chr_eli_v => "\x{21a4}\x{21a6}",
chr_eql => " ",
chr_new => "\x{25b2}",
chr_old => "\x{25bc}",
ellipsis => 0,
emacs => 0,
header => 1, # A color name is allowed
heuristics => 0,
index => 0,
iwbzusepp => 0,
markers => 0,
new => "green",
old => "red",
reverse => 0,
swap => 0,
threshold => 2,
utf8 => 0,
verbose => "cyan",
);
read_rc ();
my $opt_a = $rc{ascii};
my $opt_b;
my $opt_B;
#y $opt_c;
my $opt_E;
my $opt_h = $rc{heuristics};
my $opt_H = $rc{header};
my $opt_i;
my $opt_I = $rc{index};
my $opt_m = $rc{markers};
my $opt_r = $rc{reverse};
my $opt_s = $rc{swap};
my $opt_t = $rc{threshold};
my $opt_e = $rc{ellipsis};
my $opt_u = $rc{unified};
my $opt_U = $rc{utf8};
my $opt_v = 0;
my $opt_w;
my $opt_Z;
my $emacs = $rc{emacs};
my $old_color = $rc{old};
my $new_color = $rc{new};
my $rev_color = $rc{bg};
my $cli_color = $ENV{CLICOLOR}; # https://bixense.com/clicolors/
my $no_colors = $ENV{NO_COLOR}; # https://no-color.org
my $list_colors;
my $diff_class;
if ($no_colors) {
$ENV{CLICOLOR_FORCE} and $no_colors = 0;
}
elsif (defined $cli_color) {
# true $cli_color is the default for ccdiff
!$cli_color || !-t and $no_colors = 1;
}
unless (caller) {
$ENV{CCDIFF_OPTIONS} and unshift @ARGV, split m/\s+/ => $ENV{CCDIFF_OPTIONS};
GetOptions (
"help|?" => sub { usage (0); },
"V|version" => sub { say "$CMD [$VERSION]"; exit 0; },
"man" => sub { pod_nroff (); },
"info" => sub { pod_text (); },
"U|utf-8!" => \$opt_U,
"dc|diff-class=s" => \$diff_class,
"pp!" => sub { $diff_class = "PP" },
# "c|context:3" => \$opt_c, # implement context-diff?
"u|unified:3" => \$opt_u,
"I|idx|index:-1" => \$opt_I,
"t|threshold=i" => \$opt_t,
"H|header!" => \$opt_H,
"HC|header-color=s" => \$opt_H,
"h|heuristics=i" => \$opt_h,
"e|ellipsis=i" => \$opt_e,
"emacs!" => \$emacs,
"a|ascii" => sub { $opt_a ^= 1 },
"m|markers" => sub { $opt_m ^= 1 },
"r|reverse|invert" => sub { $opt_r ^= 1 },
"s|swap!" => sub { $opt_s ^= 1 },
"i|ignore-case!" => \$opt_i,
"w|ignore-all-space!" => \$opt_w,
"b|ignore-ws|ignore-space-change!" => \$opt_b,
"Z|ignore-trailing-space!" => \$opt_Z,
"E|ignore-tab-expansion!" => \$opt_E, # NYI
"B|ignore-blank-lines!" => \$opt_B, # Partly implemented
"p|pink!" => sub { $old_color = "magenta" },
"old=s" => \$old_color,
"new=s" => \$new_color,
"bg=s" => \$rev_color,
"no-colors" => \$no_colors,
"list-colors!" => \$list_colors,
"settings|defaults" => sub {
binmode STDOUT, ":encoding(utf-8)";
printf "%-10s : %s\n", $_, $rc{$_} // "<undef>" for sort keys %rc;
exit 0;
},
"v|verbose:1" => \$opt_v,
) or usage (1);
}
$opt_w and $opt_b = $opt_Z = $opt_E = $opt_B = 1;
$opt_h >= 1 and $opt_h /= 100;
sub pod_text {
require Pod::Text::Color;
my $m = $no_colors ? "Pod::Text" : "Pod::Text::Color";
my $p = $m->new ();
open my $fh, ">", \my $out;
$p->parse_from_file ($0, $fh);
close $fh;
print $out;
exit 0;
} # pod_text
sub pod_nroff {
first { -x "$_/nroff" } grep { -d } split m/:+/ => $ENV{PATH} or pod_text ();
require Pod::Man;
my $p = Pod::Man->new ();
open my $fh, "|-", "nroff", "-man";
$p->parse_from_file ($0, $fh);
close $fh;
exit 0;
} # pod_nroff
# Color initialization
for ($old_color, $new_color, $rev_color) {
s/^(.*)[ _]bold$/bold $1/i;
s/^bold_/bold /i;
}
my %clr = map { $_ => color (s{^(.*)[ _]bold$}{bold $1}ir =~
s{^bold[ _]}{bold }ir) }
map {( $_, "on_$_", "bold $_" )}
qw( red green blue black white cyan magenta yellow );
$clr{$_} //= color ($_) for tac_colors ();
$no_colors and $clr{$_} = "" for keys %clr;
$clr{none} = $clr{on_none} = "";
my ($reset, $bg_new, $bg_old,
$chr_cml, $chr_cmr, $chr_ctx, $chr_eli, $chr_eql, $chr_lft,
$chr_new, $chr_old, $chr_rgt,
$clr_dbg, $clr_grn, $clr_new, $clr_old, $clr_red, $clr_rev,
$cmp_sub) = (RESET);
if ($list_colors) {
my @clr = map { sprintf "%s%-18s%s", $clr{$_}, $_, $reset } sort keys %clr;
while (@clr) {
say join " " => map { $_ // "" } splice @clr, 0, 4;
}
exit;
}
sub set_options {
for ([ \$old_color, $rc{old} ], [ \$new_color, $rc{new} ], [ \$rev_color, $rc{bg} ]) {
my ($c, $def) = @$_;
$$c && exists $clr{$$c} and next;
warn "color ", $$c // "(undefined)", " is unknown, using $def instead\n";
$$c = $def;
}
$clr_red = $clr{$old_color};
$clr_grn = $clr{$new_color};
$clr_rev = $clr{$rev_color};
$clr_dbg = $opt_r && exists $clr{"on_$rc{verbose}"} ? $clr{"on_$rc{verbose}"} : $clr{$rc{verbose}};
$reset = $no_colors ? "" : RESET;
$bg_old = $clr{$rc{bg_old} || ($opt_r ? "on_$old_color" =~ s/bold //ir :
"on_$rev_color" =~ s/bold //ir)};
$bg_new = $clr{$rc{bg_new} || ($opt_r ? "on_$new_color" =~ s/bold //ir :
"on_$rev_color" =~ s/bold //ir)};
$clr_old = $opt_r ? $clr_rev . $bg_old : $clr_red . $bg_old;
$clr_new = $opt_r ? $clr_rev . $bg_new : $clr_grn . $bg_new;
$opt_s and ($clr_new, $clr_old) = ($clr_old, $clr_new);
# Indicators
if ($opt_a) {
@rc{qw( chr_old chr_new chr_cml chr_cmr chr_eli chr_eli_v )} = qw( ^ ^ > < - <> );
}
elsif (!$opt_U) {
$rc{$_} = encode ("utf-8", $rc{$_}) for qw( chr_old chr_new chr_cml chr_cmr chr_eli chr_eli_v );
}
$chr_old = $clr_old . $rc{chr_old} . $reset;
$chr_new = $clr_new . $rc{chr_new} . $reset;
$chr_cml = $clr_dbg . $rc{chr_cml} . $reset;
$chr_cmr = $clr_dbg . $rc{chr_cmr} . $reset;
$chr_eql = $rc{chr_eql};
$chr_lft = $clr_old . (defined $opt_u ? "-" : "< ") . $reset;
$chr_rgt = $clr_new . (defined $opt_u ? "+" : "> ") . $reset;
$chr_ctx = defined $opt_u ? " " : " ";
$chr_eli = $opt_v >= 2 ? $rc{chr_eli_v} : $rc{chr_eli};
$opt_m && $opt_v > 1 && length ($chr_eli) > 1 and $opt_m = 0;
if ($opt_i || $opt_b || $opt_Z) {
$rc{iwbzusepp} and $diff_class = "Algorithm::Diff";
$cmp_sub = {
keyGen => sub {
my $line = shift;
$opt_i and $line = lc $line;
$opt_Z and $line =~ s/[ \t\r]+$//g;
$opt_b and $line =~ s/[ \t]+/ /g;
return $line;
}
};
}
} # set_options
if ($diff_class) {
if ($diff_class =~ m/^(?:ad|pp|algorithm(?:-|::)diff(?:(?:-|::)pp)?)$/i) {
$diff_class =
eval { require Algorithm::Diff; "Algorithm::Diff"; }
}
elsif ($diff_class =~ m/^(?:adx|xs|algorithm(?:-|::)diff(?:-|::)xs)$/i) {
$diff_class = "Algorithm::Diff::XS";
$diff_class =
eval { require Algorithm::Diff::XS; "Algorithm::Diff::XS"; }
}
else {
die "$diff_class is an unsupported Diff class\n";
}
}
else {
$diff_class =
eval { require Algorithm::Diff::XS; "Algorithm::Diff::XS"; }
|| eval { require Algorithm::Diff; "Algorithm::Diff"; };
}
$diff_class or die "Cannot load Algorithm::Diff:\n";
unless (caller) {
if (@ARGV == 2 && -d $ARGV[0] && -d $ARGV[1]) {
my ($d_from, $d_to) = @ARGV;
my %fn;
my @f;
foreach my $idx (0, 1) {
my $dir = $ARGV[$idx];
find (sub {
-f && -s or return;
my $fn = $File::Find::name =~ s{^$dir/*}{}r;
$fn{$fn}++;
$f[$idx]{$fn}++;
}, $dir);
}
foreach my $fn (sort keys %fn) {
if ($f[0]{$fn} && $f[1]{$fn}) {
ccdiff ("$d_from/$fn", "$d_to/$fn");
}
elsif ($f[0]{$fn}) {
say "Only in $d_from: $fn";
}
else {
say "Only in $d_to: $fn";
}
delete $f[0]{$fn};
}
}
else {
ccdiff (@ARGV);
}
exit 0;
}
sub ccdiff {
my $f1 = shift or usage (1);
my $f2 = $_[0] // "-";
-b $f1 || -c $f1 || -b $f2 || -c $f2 and
die "Character and block devices are not supported\n";
-d $f1 || -d $f2 and
die "$CMD does not support directory diff\n";
my $fh;
if (@_ > 1 && ref $_[1]) { # optional hash with overruling arguments
my %opt = %{$_[1]};
foreach my $o (keys %opt) {
my $v = $opt{$o};
$o eq "ascii" and $opt_a = $v;
$o eq "bg" and $rev_color = $v;
# $o eq "context" and $opt_c = $v;
$o eq "ellipsis" and $opt_e = $v;
$o eq "emacs" and $emacs = $v;
$o eq "header" and $opt_H = $v;
$o eq "heuristics" and $opt_h = $v;
$o eq "ignore-all-space" and $opt_w = $v;
$o eq "ignore-blank-lines" and $opt_B = $v;
$o eq "ignore-case" and $opt_i = $v;
$o eq "ignore-space-change" and $opt_b = $v;
$o eq "ignore-tab-expansion" and $opt_E = $v;
$o eq "ignore-trailing-space" and $opt_Z = $v;
$o eq "index" and $opt_I = $v;
$o eq "list-colors" and $list_colors = $v;
$o eq "markers" and $opt_m = $v;
$o eq "new" and $new_color = $v;
$o eq "old" and $old_color = $v;
$o eq "reverse" and $opt_r = $v;
$o eq "swap" and $opt_s = $v;
$o eq "threshold" and $opt_t = $v;
$o eq "unified" and $opt_u = $v;
$o eq "unified" and $opt_u = $v;
$o eq "utf-8" and $opt_U = $v;
$o eq "verbose" and $opt_v = $v;
if ($o eq "out") {
open $fh, ">", $v or die "Cannot select out: $!\n";
select $fh;
}
}
}
set_options ();
$emacs and @_ == 0 && -f $f1 && -f "$f1~" and ($f1, $f2) = ("$f1~", $f1);
$f1 eq "-" && $f2 eq "-" and usage (1);
binmode STDERR, ":encoding(utf-8)";
if ($opt_U) {
binmode STDIN, ":encoding(utf-8)";
binmode STDOUT, ":encoding(utf-8)";
}
my @d1 = ref $f1 eq "ARRAY" ? @$f1 : $f1 eq "-" ? <STDIN> : do {
open my $fh, "<", $f1 or die "$f1: $!\n";
$opt_U and binmode $fh, ":encoding(utf-8)";
<$fh>;
};
my @d2 = ref $f2 eq "ARRAY" ? @$f2 : $f2 eq "-" ? <STDIN> : do {
open my $fh, "<", $f2 or die "$f2: $!\n";
$opt_U and binmode $fh, ":encoding(utf-8)";
<$fh>;
};
if ($opt_H) {
my $hc = "";
if ($opt_H =~ m/^\w\w+/) {
my ($hfg, $hbg) = split m/_?(?=on_)/ => lc $opt_H =~ s/\s+/_/gr;
$hfg && defined $clr{$hfg} and $hc .= $clr{$hfg};
$hbg && defined $clr{$hbg} and $hc .= $clr{$hbg};
}
my $nl = max length $f1, length $f2, 7;
my $sl = $hc ? ($ENV{COLUMNS} || 80) - 4 - $nl : 1;
my @h = map { -f $_
? { tag => "",
name => $_,
stamp => scalar localtime ((stat $_)[9]),
}
: { tag => "",
name => "*STDIN",
stamp => scalar localtime,
}
} $f1, $f2;
if (defined $opt_u) {
($h[0]{tag}, $h[1]{tag}) = ("---", "+++");
$sl -= 2;
printf "%s%s %-*s %-*s%s\n", $hc, $_->{tag},
$nl, $_->{name}, $sl, $_->{stamp}, $clr{reset} for @h;
}
#elsif ($opt_c) { # diff -c also provides (ugly) headers, but opt_c is NYI
# }
else {
($h[0]{tag}, $h[1]{tag}) = ("<", ">");
printf "%s%s %-*s %-*s%s\n", $hc, $_->{tag},
$nl, $_->{name}, $sl, $_->{stamp}, $clr{reset} for @h;
}
}
my $diff = $diff_class->new (\@d1, \@d2, $cmp_sub);
$diff->Base (1);
my ($N, $idx, @s) = (0, 0);
while ($diff->Next) {
$N++;
if ($diff->Same) {
if (defined $opt_u) {
@s = $diff->Items (1);
$N > 1 and print "$chr_ctx$_" for grep { defined } @s[0..($opt_u - 1)];
unshift @s, undef while @s < $opt_u;
}
next;
}
my $sep = "";
my @d = map {[ $diff->Items ($_) ]} 1, 2;
my @do = @{$d[0]};
my @dn = @{$d[1]};
if ($opt_B and "@do" !~ m/\S/ && "@dn" !~ m/\S/) {
# Modify @s for -u?
next;
}
if ($opt_I) {
$idx++;
$opt_I > 0 && $idx != $opt_I and next;
printf "%s[%03d]%s ", ${clr_dbg}, $idx, $reset;
}
if (!@dn) {
printf "%d,%dd%d\n", $diff->Get (qw( Min1 Max1 Max2 ));
$_ = $clr_old . (s/$/$reset/r) for @do;
}
elsif (!@do) {
printf "%da%d,%d\n", $diff->Get (qw( Max1 Min2 Max2 ));
$_ = $clr_new . (s/$/$reset/r) for @dn;
}
else {
$sep = "---\n" unless defined $opt_u;
printf "%d,%dc%d,%d\n", $diff->Get (qw( Min1 Max1 Min2 Max2 ));
if ($opt_t > 0 and abs (@do - @dn) > $opt_t) {
$_ = $clr_old . (s/$/$reset/r) for @do;
$_ = $clr_new . (s/$/$reset/r) for @dn;
}
else {
my @D = subdiff (@d, my $heu = {});
if ($opt_h and $heu->{pct} > $opt_h) {
$_ = $clr_old . (s/$/$reset/r) for @do;
$_ = $clr_new . (s/$/$reset/r) for @dn;
}
else {
@do = @{$D[0]};
@dn = @{$D[1]};
}
}
}
if ($opt_u and @s) {
print "$chr_ctx$_" for grep { defined } map { $s[$#s - $opt_u + $_] } 1..$opt_u;
}
print "$chr_lft$_" for @do;
print $sep;
print "$chr_rgt$_" for @dn;
}
if ($fh) {
select STDOUT;
close $fh;
}
} # ccdiff
sub subdiff {
my ($old, $new, $heu) = @_;
my $d = $diff_class->new (map { [ map { split m// } @$_ ] } $old, $new);
my ($d1, $d2, $x1, $x2, @h1, @h2) = ("", "", "", "");
my ($cml, $cmr) = $opt_v < 2 ? ("", "") : ($chr_cml, $chr_cmr);
my ($cmd, $cma) = ($chr_old, $chr_new);
@{$heu}{qw( old new same )} = (1, 1, 1); # prevent div/0
while ($d->Next) {
my @c = map {[ $d->Items ($_) ]} 1, 2;
my @co = @{$c[0]};
my @cn = @{$c[1]};
if ($d->Same) {
$heu->{same} += scalar @co;
my $e = $chr_eli;
my $c = join "" => @co;
if ($opt_e) {
my $join = "";
foreach my $sc (split m/\n/ => $c) {
$_ .= $join for $d1, $d2, $x1, $x2;
$join = "\n";
my $l = length $sc; # The length of this "same" chunck
my $le = $l - 2 * $opt_e; # The length of the text replaces with ellipsis
my $ee = $opt_v <= 1 ? $e : $e =~ s/^.\K(?=.$)/$le/r;
if ($le > length $ee) {
my $lsc = substr $sc, 0, $opt_e;
$d1 .= $lsc;
$d2 .= $lsc;
$lsc =~ s/\S/$chr_eql/g;
$x1 .= $lsc;
$x2 .= $lsc;
my $rsc = substr $sc, $l - $opt_e, $opt_e;
$d1 .= $clr_dbg . $ee . $reset . $rsc;
$d2 .= $clr_dbg . $ee . $reset . $rsc;
$rsc =~ s/\S/$chr_eql/g;
$x1 .= $chr_eql x length ($ee) . $rsc;
$x2 .= $chr_eql x length ($ee) . $rsc;
next;
}
else {
$d1 .= $sc;
$d2 .= $sc;
$sc =~ s/\S/$chr_eql/g;
$x1 .= $sc;
$x2 .= $sc;
}
}
next;
}
$d1 .= $c;
$d2 .= $c;
$c =~ s/\S/$chr_eql/g;
$x1 .= $c;
$x2 .= $c;
next;
}
if (@co) {
$heu->{old} += scalar @co;
$d1 .= $cml.$clr_old;
$d1 .= s/\n/$reset\n$clr_old/gr for @co;
$d1 .= $reset.$cmr;
$x1 .= $_ for map { s/[^\t\r\n]/$cmd/gr } @co;
$opt_v and push @h1, map { $opt_U ? charnames::viacode (ord) : unpack "H*"; } @co;
}
if (@cn) {
$heu->{new} += scalar @cn;
$d2 .= $cml.$clr_new;
$d2 .= s/\n/$reset\n$clr_new/gr for @cn;
$d2 .= $reset.$cmr;
$x2 .= $_ for map { s/[^\t\r\n]/$cma/gr } @cn;
$opt_v and push @h2, map { $opt_U ? charnames::viacode (ord) : unpack "H*"; } @cn;
}
}
$heu->{pct} = ($heu->{old} + $heu->{new}) / (2 * $heu->{same});
my @d = map { [ split m/(?<=\n)/ => s/\n*\z/\n/r ] } $d1, $d2;
if ($opt_m) {
$opt_v > 1 and s/(\S+)/ $1 /g for $x1, $x2;
s/[ \t]*\n*\z/\n/ for $x1, $x2;
my @x = map { /\S/ ? [ split m/(?<=\n)/ ] : [] } $x1, $x2;
foreach my $n (0, 1) {
@{$x[$n]} and $d[$n] = [ map {( $d[$n][$_], $x[$n][$_] // "" )} 0 .. (scalar @{$d[$n]} - 1) ];
}
}
if ($opt_v) {
$opt_U && $opt_v > 2 and $_ .= sprintf " (U+%06X)", charnames::vianame ($_) for @h1, @h2;
@h1 and push @{$d[0]}, sprintf " -- ${clr_dbg}verbose$reset : %s\n", join ", " => map { $clr_old.$_.$reset } @h1;
@h2 and push @{$d[1]}, sprintf " -- ${clr_dbg}verbose$reset : %s\n", join ", " => map { $clr_new.$_.$reset } @h2;
}
@d;
} # subdiff
sub read_rc {
my $home = $ENV{HOME} || $ENV{USERPROFILE} || $ENV{HOMEPATH};
foreach my $rcf (
"$home/ccdiff.rc",
"$home/.ccdiffrc",
"$home/.config/ccdiff",
) {
-s $rcf or next;
(stat $rcf)[2] & 022 and next;
open my $fh, "<", $rcf or next;
while (<$fh>) {
my ($k, $v) = (m/^\s*([-\w]+)\s*[:=]\s*(.*\S)/) or next;
$rc{ lc $k
=~ s{[-_]colou?r$}{}ir
=~ s{background}{bg}ir
=~ s{^(?:unicode|utf-?8?)$}{utf8}ir
} = $v
=~ s{U\+?([0-9A-Fa-f]{2,7})}{chr hex $1}ger
=~ s{^(?:no|false)$}{0}ir
=~ s{^(?:yes|true)$}{-1}ir; # -1 is still true
}
}
} # read_rc
# Return the known colors from Term::ANSIColor
# Stolen straight from the pm
sub tac_colors {
my %c256;
foreach my $r (0 .. 5) {
foreach my $g (0 .. 5) {
$c256{lc $_}++ for map {("RGB$r$g$_", "ON_RGB$r$g$_")} 0 .. 5;
}
}
$c256{lc $_}++ for
# Basic colors
qw(
CLEAR RESET BOLD DARK
FAINT ITALIC UNDERLINE UNDERSCORE
BLINK REVERSE CONCEALED
BLACK RED GREEN YELLOW
BLUE MAGENTA CYAN WHITE
ON_BLACK ON_RED ON_GREEN ON_YELLOW
ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
BRIGHT_BLACK BRIGHT_RED BRIGHT_GREEN BRIGHT_YELLOW
BRIGHT_BLUE BRIGHT_MAGENTA BRIGHT_CYAN BRIGHT_WHITE
ON_BRIGHT_BLACK ON_BRIGHT_RED ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
ON_BRIGHT_BLUE ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN ON_BRIGHT_WHITE
),
# 256 colors
(map { ("ANSI$_", "ON_ANSI$_") } 0 .. 255),
(map { ("GREY$_", "ON_GREY$_") } 0 .. 23);
my $ACV = $Term::ANSIColor::VERSION;
$ACV < 3.02 and delete @c256{grep m/italic/ => keys %c256};
$ACV < 4.00 and delete @c256{grep m/rgb|grey/ => keys %c256};
$ACV < 4.06 and delete @c256{grep m/ansi/ => keys %c256};
sort keys %c256;
} # tac_colors
1;
__END__
=encoding utf-8
=head1 NAME
ccdiff - Colored Character diff
=head1 SYNOPSIS
ccdiff [options] file1|- file2|-
ccdiff [options] dir1 dir2
ccdiff --help
ccdiff --man
ccdiff --info
=head1 DESCRIPTION
Show the diff between two files on a character by character base. In contrast to
the standard diff tools, this tool uses the diff algorithm horizontally for each
line in the vertical diff, highlighting the changes. This is very handy in hard
to spot changes like C<O> to C<0>, C<I> to C<l> or C<1> and whitespace.
If there are two argument, and both are a folder/directory, a recursive diff is
executed. This is not available whan used as a (sub)class.
=head1 OPTIONS
=head2 Command line options
=over 2
=item --help -?
Show a summary of the available command-line options and exit.
=item --version -V
Show the version and exit.
=item --man
Show this manual using pod2man and nroff.
=item --info
Show this manual using pod2text.
=item --utf-8 -U
All I/O (streams to compare and standard out) are in UTF-8.
=item --diff-class=C --dc=C --pp
Select the class used to execute the diff. By default C<ccdiff> will select
the first available out of C<Algorithm::Diff::XS> or C<Algorithm::Diff>.
Sometime the C<XS> version fails on encoding and the pure-perl version will
work just fine. You can force C<ccdiff> to use either
Select the pure-perl version with any of C<PP>, C<AD>, C<Algorthm::Diff>,
C<Algorithm-Diff>, or C<Algorithm::Diff::PP> (case insensitive).
For convenience, C<--dc=pp> can be abbreviated to C<--pp>.
--pp
--dc=pp
--dc=algorithm-diff
--diff-class=Algorithm::Diff::PP
Select the XS version with any of C<XS>, C<ADX>, C<Algorthm::Diff::XS>, or
C<Algorithm-Diff-XS> (case insensitive).
--dc=xs
--dc=algorithm-diff-xs
--diff-class=Algorithm::Diff::XS
=item --unified[=3] -u [3]
Generate a unified diff. The number of context lines is optional. When omitted
it defaults to 3. Currently there is no provision of dealing with overlapping
diff chunks. If the common part between two diff chunks is shorter than twice
the number of context lines, some lines may show twice.
The default is to use traditional diff:
5,5c5,5
< Sat Dec 18 07:00:33 1993,I.O.D.U.,,756194433,1442539
---
> Sat Dec 18 07:08:33 1998,I.O.D.U.,,756194433,1442539
a unified diff (-u1) would be
5,5c5,5
Tue Sep 6 05:43:59 2005,B.O.Q.S.,,1125978239,1943341
-Sat Dec 18 07:00:33 1993,I.O.D.U.,,756194433,1442539
+Sat Dec 18 07:08:33 1998,I.O.D.U.,,756194433,1442539
Mon Feb 23 10:37:02 2004,R.X.K.S.,van,1077529022,1654127
=item --verbose[=1] -v[1]
Show an additional line for each old or new section in a change chunk (not for
added or deleted lines) that shows the hexadecimal value of each character. If
C<--utf-8> is in effect, it will show the Unicode character name(s).
This is a debugging option, so invisible characters can still be "seen".
C<--verbose> accepts an optional verbosity-level. On level 2 and up, all
horizontal changes get left-and-right markers inserted to enable seeing the
location of the ZERO WIDTH or invisible characters. With level 3 and up and
Unicode enabled, the changed characters will also show the codepoint in hex.
An example of this:
With -Uu0v0:
1,1c1,1
- A BCDE Fg
+ A BcdEFg
With -Uu0v1:
1,1c1,1
- A BCDE Fg
- -- verbose : SPACE, LATIN CAPITAL LETTER C, LATIN CAPITAL LETTER D, SPACE
+ A BcdEFg
+ -- verbose : LATIN SMALL LETTER C, LATIN SMALL LETTER D, ZERO WIDTH SPACE
With -Uu0v2:
1,1c1,1
- A ↱ ↰B↱CD↰E↱ ↰Fg
- -- verbose : SPACE, LATIN CAPITAL LETTER C, LATIN CAPITAL LETTER D, SPACE
+ A B↱cd↰E↱↰Fg
+ -- verbose : LATIN SMALL LETTER C, LATIN SMALL LETTER D, ZERO WIDTH SPACE
With -Uu0v3:
1,1c1,1
- A ↱ ↰B↱CD↰E↱ ↰Fg
- -- verbose : SPACE (U+000020), LATIN CAPITAL LETTER C (U+000043), LATIN CAPITAL LETTER D (U+000044), SPACE (U+000020)
+ A B↱cd↰E↱↰Fg
+ -- verbose : LATIN SMALL LETTER C (U+000063), LATIN SMALL LETTER D (U+000064), ZERO WIDTH SPACE (U+00200B)
With -Uu0v2 --ascii:
1,1c1,1
- A > <B>CD<E> <Fg
- -- verbose : SPACE, LATIN CAPITAL LETTER C, LATIN CAPITAL LETTER D, SPACE
+ A B>cd<E><Fg
+ -- verbose : LATIN SMALL LETTER C, LATIN SMALL LETTER D, ZERO WIDTH SPACE
the word "verbose" and the character markers will be displayed using the
C<verbose> color. The characters used for the markers can be defined in your
configuration file as C<chr_cml> (the character used as marker on the left)
and C<chr_cmr> (the character used as marker on the right).
=item --markers -m
Use markers under each changed character in change-chunks.
C<--markers> is especially useful if the terminal does not support colors, or
if you want to copy/paste the output to (ASCII) mail. See also C<--ascii>. The
markers will have the same color as added or deleted text.
This will look like (with unified diff):
5,5c5,5
-Sat Dec 18 07:08:33 1998,I.O.D.U.,,756194433,1442539
- ▼ ▼
+Sat Dec 18 07:00:33 1993,I.O.D.U.,,756194433,1442539
+ ▲ ▲
The characters used for the markers can be defined in your configuration file
as C<chr_old> (the character used as marker under removed characters) and
C<chr_new> (the character used as marker under added characters).
If C<--ellipsis> is also in effect and either the C<chr_eli> is longer than
one character or C<--verbose> level is over 2, this option is automatically
disabled.
=item --ascii -a
Use (colored) ASCII indicators instead of Unicode. The default indicators are
Unicode characters that stand out better. The markers will have the same color
as added or deleted text.
For the vertical markers (C<-m>) that would look like:
5,5c5,5
-Sat Dec 18 07:08:33 1998,I.O.D.U.,,756194433,1442539
- ^ ^
+Sat Dec 18 07:00:33 1993,I.O.D.U.,,756194433,1442539
+ ^ ^
For the positional indicators, I did consider using U+034e (COMBINING UPWARDS
ARROW BELOW), but as most terminals are probably unable to show it due to line
height changes, I did not pursue the idea.
=item --pink -p
Change the default C<red> for deleted text to the color closest to pink that
is supported by L<Term::ANSIColor>: C<magenta>.
=item --reverse -r
Reverse/invert the foreground and background for the colored indicators.
If the foreground color has C<bold>, it will be stripped from the new background
color.
=item --swap -s
Swap the colors for new and old.
=item --list-colors
List available colors and exit.
=item --no-colors
Disable all colors. Useful for redirecting the diff output to a file that is to
be included in documentation.
This is the default if the environment variable C<$NO_COLOR> has a true value or
if the environment variable C<$CLICOLOR> is set to a false value. If set,
C<$CLICOLOR_FORCE> will overrule the default of C<$NO_COLOR>.
=item --old=color
Define the foreground color for deleted text.
=item --new=color
Define the foreground color for added text.
=item --bg=color
Define the background color for changed text.
=item --index --idx -I
Prefix position indicators with an index.
[001] 5,5c5,5
-Sat Dec 18 07:08:33 1998,I.O.D.U.,,756194433,1442539
+Sat Dec 18 07:00:33 1993,I.O.D.U.,,756194433,1442539
If a positive number is passed (C<--index=4> or C<-I 4>), display just the
chunk with that index, using the C<verbose> color:
This is useful in combination with C<--verbose>.
=item --threshold=2 -t 2
Defines the number of lines a change block may differ before the fall-back of
horizontal diff to vertical diff.
If a chunk describes a change, and the number of lines in the original block
has fewer or more lines than the new block and that difference exceeds this
threshold, C<ccdiff> will fall-back to vertical diff.
=item --heuristics=n -h n
Defines the percentage of character-changes a change block may differ before
the fall-back of horizontal diff to vertical diff.
This percentage is calculated as C<(characters removed + characters added) /
(2 * characters unchanged))>.
=item --ellipsis=n -e n
Defines the number of characters to keep on each side of a horizontal-equal
segment. The default is C<0>, meaning do not compress.
If set to a positive number, and the length of a segment of equal characters
inside a horizontal diff is longer than twice this value, the middle part is
replaced with C<┈ U02508 \N{BOX DRAWINGS LIGHT QUADRUPLE DASH HORIZONTAL}>
(instead of … U02026, as HORIZONTAL ELLIPSIS does not stand out enough).
With C<-u0me3> that would be like
5,5c5,5
-Sat┈07:08:33┈ 1998,I.┈539
- ▼ ▼
+Sat┈07:00:33┈ 1993,I.┈539
+ ▲ ▲
With C<-u0e3 -v2> like
5,5c5,5
-Sat↤9↦07:0↱0↰:33 199↱3↰,I.↤23↦539
- -- verbose : DIGIT ZERO, DIGIT THREE
+Sat↤9↦07:0↱8↰:33 199↱8↰,I.↤23↦539
+ -- verbose : DIGIT EIGHT, DIGIT EIGHT
The text used for the replaced text can be defined in your configuration file
as C<chr_eli> and/or C<chr_eli_v>.
=item --ignore-case -i
Ignore case on comparison.
=item --ignore-all-space -w
Ignore all white-space changes. This will set all options C<-b>, C<-Z>, C<-E>,
and C<-B>.
=item --ignore-trailing-space -Z
Ignore changes in trailing white-space (tabs and spaces).
=item --ignore-ws|ignore-space-change -b
Ignore changes in horizontal white-space (tabs and spaces). This does not
include white-space changes that split non-white-space or remove white-space
between two non-white-space elements.
=item --ignore-tab-expansion -E
NYI
=item --ignore-blank-lines -B
B<Just Partly Implemented> (WIP)
=back
=head2 Configuration files
In order to be able to overrule the defaults set in C<ccdiff>, one can set
options specific for this login. The following option files are looked for
in this order:
- $HOME/ccdiff.rc
- $HOME/.ccdiffrc
- $HOME/.config/ccdiff
and evaluated in that order. Any options specified in a file later in that
chain will overwrite previously set options.
Option files are only read and evaluated if they are not empty and not writable
by others than the owner.
The syntax of the file is one option per line, where leading and trailing
white-space is ignored. If that line then starts with one of the options
listed below, followed by optional white-space followed by either an C<=> or