-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff-so-fancy
executable file
·597 lines (482 loc) · 15.7 KB
/
diff-so-fancy
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
#!/usr/bin/env perl
my $VERSION = "1.0.0";
#################################################################################
use strict;
use warnings FATAL => 'all';
use File::Basename; # for dirname
use Cwd; # For realpath()
# Set the input (STDIN) as UTF8, but don't give warnings about unknown chars
use open qw(:std :utf8); # http://stackoverflow.com/a/519359
no warnings 'utf8';
# Set the output to always be UTF8
binmode STDOUT,':encoding(UTF-8)';
my $remove_file_add_header = 1;
my $remove_file_delete_header = 1;
my $clean_permission_changes = 1;
my $change_hunk_indicators = git_config_boolean("diff-so-fancy.changeHunkIndicators","true");
my $strip_leading_indicators = git_config_boolean("diff-so-fancy.stripLeadingSymbols","true");
my $mark_empty_lines = git_config_boolean("diff-so-fancy.markEmptyLines","true");
my $use_unicode_dash_for_ruler = git_config_boolean("diff-so-fancy.useUnicodeRuler","true");
my $git_strip_prefix = git_config_boolean("diff.noprefix","false");
my $has_stdin = has_stdin();
# We only process ARGV if we don't have STDIN
my $input;
if (!$has_stdin) {
my $args = argv();
if ($args->{v} || $args->{version}) {
die(version());
} elsif ($args->{colors}) {
die(output_default_colors());
} elsif (!%$args || $args->{help} || $args->{h}) {
die(usage());
} else {
die("Missing input on STDIN\n");
}
} else {
$input = filter_stdin_through_diff_highlight();
}
#################################################################################
my $ansi_color_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,3})[mK])?/;
my $dim_magenta = "\e[38;5;146m";
my $reset_color = "\e[0m";
my $bold = "\e[1m";
my $horizontal_color = "";
my $columns_to_remove = 0;
my ($file_1,$file_2);
my $last_file_seen = "";
my $i = 0;
my $in_hunk = 0;
while (my $line = <$input>) {
######################################################
# Pre-process the line before we do any other markup #
######################################################
# If the first line of the input is a blank line, skip that
if ($i == 0 && $line =~ /^\s*$/) {
next;
}
######################
# End pre-processing #
######################
#######################################################################
####################################################################
# Look for git index and replace it horizontal line (header later) #
####################################################################
if ($line =~ /^${ansi_color_regex}index /) {
# Print the line color and then the actual line
$horizontal_color = $1;
print horizontal_rule($horizontal_color);
#########################
# Look for the filename #
#########################
} elsif ($line =~ /^${ansi_color_regex}diff --(git|cc) (.*?)(\s|\e|$)/) {
$last_file_seen = $5;
$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
$in_hunk = 0;
########################################
# Find the first file: --- a/README.md #
########################################
} elsif (!$in_hunk && $line =~ /^$ansi_color_regex--- (\w\/)?(.+?)(\e|\t|$)/) {
if ($git_strip_prefix) {
my $file_dir = $4 || "";
$file_1 = $file_dir . $5;
} else {
$file_1 = $5;
}
# Find the second file on the next line: +++ b/README.md
my $next = <$input>;
$next =~ /^$ansi_color_regex\+\+\+ (\w\/)?(.+?)(\e|\t|$)/;
if ($1) {
print $1; # Print out whatever color we're using
}
if ($git_strip_prefix) {
my $file_dir = $4 || "";
$file_2 = $file_dir . $5;
} else {
$file_2 = $5;
}
if ($file_2 ne "/dev/null") {
$last_file_seen = $file_2;
}
print file_change_string($file_1,$file_2) . "\n";
# Print out the bottom horizontal line of the header
print horizontal_rule($horizontal_color);
########################################
# Check for "@@ -3,41 +3,63 @@" syntax #
########################################
} elsif ($change_hunk_indicators && $line =~ /^${ansi_color_regex}(@@@* .+? @@@*)(.*)/) {
$in_hunk = 1;
my $hunk_header = $4;
my $remain = bleach_text($5);
# The number of colums to remove (1 or 2) is based on how many commas in the hunk header
$columns_to_remove = (char_count(",",$hunk_header)) - 1;
# On single line removes there is NO comma in the hunk so we force one
$columns_to_remove ||= 1;
if ($1) {
print $1; # Print out whatever color we're using
}
my ($orig_offset, $orig_count, $new_offset, $new_count) = parse_hunk_header($hunk_header);
$last_file_seen = basename($last_file_seen);
# Figure out the start line
my $start_line = start_line_calc($new_offset,$new_count);
print "@ $last_file_seen:$start_line \@${bold}${dim_magenta}${remain}${reset_color}\n";
###################################
# Remove any new file permissions #
###################################
} elsif ($remove_file_add_header && $line =~ /^${ansi_color_regex}.*new file mode/) {
# Don't print the line (i.e. remove it from the output);
######################################
# Remove any delete file permissions #
######################################
} elsif ($remove_file_delete_header && $line =~ /^${ansi_color_regex}deleted file mode/) {
# Don't print the line (i.e. remove it from the output);
################################
# Look for binary file changes #
################################
} elsif ($line =~ /^Binary files (\w\/)?(.+?) and (\w\/)?(.+?) differ/) {
my $change = file_change_string($2,$4);
print "$horizontal_color$change (binary)\n";
print horizontal_rule($horizontal_color);
#####################################################
# Check if we're changing the permissions of a file #
#####################################################
} elsif ($clean_permission_changes && $line =~ /^${ansi_color_regex}old mode (\d+)/) {
my ($old_mode) = $4;
my $next = <$input>;
if ($1) {
print $1; # Print out whatever color we're using
}
my ($new_mode) = $next =~ m/new mode (\d+)/;
print "$last_file_seen changed file mode from $old_mode to $new_mode\n";
#####################################
# Just a regular line, print it out #
#####################################
} else {
# Mark empty line with a red/green box indicating addition/removal
if ($mark_empty_lines) {
$line = mark_empty_line($line);
}
# Remove the correct number of leading " " or "+" or "-"
if ($strip_leading_indicators) {
$line = strip_leading_indicators($line,$columns_to_remove);
}
print $line;
}
$i++;
}
######################################################################################################
# End regular code, begin functions
######################################################################################################
# Courtesy of github.com/git/git/blob/ab5d01a/git-add--interactive.perl#L798-L805
sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = $line =~ /^\@\@+(?: -(\d+)(?:,(\d+))?)+ \+(\d+)(?:,(\d+))? \@\@+/;
$o_cnt = 1 unless defined $o_cnt;
$n_cnt = 1 unless defined $n_cnt;
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
}
# Mark the first char of an empty line
sub mark_empty_line {
my $line = shift();
my $reset_color = "\e\\[0?m";
my $reset_escape = "\e\[m";
my $invert_color = "\e\[7m";
$line =~ s/^($ansi_color_regex)[+-]$reset_color\s*$/$invert_color$1 $reset_escape\n/;
return $line;
}
# String to boolean
sub boolean {
my $str = shift();
$str = trim($str);
if ($str eq "" || $str =~ /^(no|false|0)$/i) {
return 0;
} else {
return 1;
}
}
# Memoize getting the git config
{
my $static_config;
sub git_config_raw {
if ($static_config) {
# If we already have the config return that
return $static_config;
}
my $cmd = "git config --list";
my @out = `$cmd`;
$static_config = \@out;
return \@out;
}
}
# Fetch a textual item from the git config
sub git_config {
my $search_key = lc($_[0] || "");
my $default_value = lc($_[1] || "");
my $out = git_config_raw();
# If we're in a unit test, use the default (don't read the users config)
if (in_unit_test()) {
return $default_value;
}
my $raw = {};
foreach my $line (@$out) {
if ($line =~ /=/) {
my ($key,$value) = split("=",$line,2);
$value =~ s/\s+$//;
$raw->{$key} = $value;
}
}
# If we're given a search key return that, else return the hash
if ($search_key) {
return $raw->{$search_key} || $default_value;
} else {
return $raw;
}
}
# Fetch a boolean item from the git config
sub git_config_boolean {
my $search_key = lc($_[0] || "");
my $default_value = lc($_[1] || 0); # Default to false
# If we're in a unit test, use the default (don't read the users config)
if (in_unit_test()) {
return boolean($default_value);
}
my $result = git_config($search_key,$default_value);
my $ret = boolean($result);
return $ret;
}
# Check if we're inside of BATS
sub in_unit_test {
if ($ENV{BATS_CWD}) {
return 1;
} else {
return 0;
}
}
sub get_less_charset {
my @less_char_vars = ("LESSCHARSET", "LESSCHARDEF", "LC_ALL", "LC_CTYPE", "LANG");
foreach (@less_char_vars) {
return $ENV{$_} if defined $ENV{$_};
}
return "";
}
sub should_print_unicode {
if (-t STDOUT) {
# Always print unicode chars if we're not piping stuff, e.g. to less(1)
return 1;
}
# Otherwise, assume we're piping to less(1)
my $less_charset = get_less_charset();
if ($less_charset =~ /utf-?8/i) {
return 1;
}
return 0;
}
# Return git config as a hash
sub get_git_config_hash {
my $out = git_config_raw();
my %hash;
foreach my $line (@$out) {
my ($key,$value) = split("=",$line,2);
$value =~ s/\s+$//;
my @path = split(/\./,$key);
my $last = pop @path;
my $p = \%hash;
$p = $p->{$_} ||= {} for @path;
$p->{$last} = $value;
}
return \%hash;
}
# Try and be smart about what line the diff hunk starts on
sub start_line_calc {
my ($line_num,$diff_context) = @_;
my $ret;
if ($line_num == 0 && $diff_context == 0) {
return 1;
}
# Git defaults to three lines of context
my $default_context_lines = 3;
# Three lines on either side, and the line itself = 7
my $expected_context = ($default_context_lines * 2 + 1);
# The first three lines
if ($line_num == 1 && $diff_context < $expected_context) {
$ret = $diff_context - $default_context_lines;
} else {
$ret = $line_num + $default_context_lines;
}
if ($ret < 1) {
$ret = 1;
}
return $ret;
}
# Remove + or - at the beginning of the lines
sub strip_leading_indicators {
my $line = shift(); # Array passed in by reference
my $columns_to_remove = shift(); # Don't remove any lines by default
if ($columns_to_remove == 0) {
return $line; # Nothing to do
}
$line =~ s/^(${ansi_color_regex})[ +-]{${columns_to_remove}}/$1/;
return $line;
}
# Count the number of a given char in a string
sub char_count {
my ($needle,$str) = @_;
my $len = length($str);
my $ret = 0;
for (my $i = 0; $i < $len; $i++) {
my $found = substr($str,$i,1);
if ($needle eq $found) { $ret++; }
}
return $ret;
}
# Remove all ANSI codes from a string
sub bleach_text {
my $str = shift();
$str =~ s/\e\[\d*(;\d+)*m//mg;
return $str;
}
# Remove all trailing and leading spaces
sub trim {
my $s = shift();
if (!$s) { return ""; }
$s =~ s/^\s*|\s*$//g;
return $s;
}
# Print a line of em-dash or line-drawing chars the full width of the screen
sub horizontal_rule {
my $color = $_[0] || "";
my $width = `tput cols`;
my $uname = `uname -s`;
if ($uname =~ /MINGW32|MSYS/) {
$width--;
}
# em-dash http://www.fileformat.info/info/unicode/char/2014/index.htm
#my $dash = "\x{2014}";
# BOX DRAWINGS LIGHT HORIZONTAL http://www.fileformat.info/info/unicode/char/2500/index.htm
my $dash;
if ($use_unicode_dash_for_ruler && should_print_unicode()) {
$dash = "\x{2500}";
} else {
$dash = "-";
}
# Draw the line
my $ret = $color . ($dash x $width) . "\n";
return $ret;
}
sub file_change_string {
my $file_1 = shift();
my $file_2 = shift();
# If they're the same it's a modify
if ($file_1 eq $file_2) {
return "modified: $file_1";
# If the first is /dev/null it's a new file
} elsif ($file_1 eq "/dev/null") {
return "added: $file_2";
# If the second is /dev/null it's a deletion
} elsif ($file_2 eq "/dev/null") {
return "deleted: $file_1";
# If the files aren't the same it's a rename
} elsif ($file_1 ne $file_2) {
return "renamed: $file_1 to $file_2";
# Something we haven't thought of yet
} else {
return "$file_1 -> $file_2";
}
}
# Check to see if STDIN is connected to an interactive terminal
sub has_stdin {
my $i = -t STDIN;
my $ret = int(!$i);
return $ret;
}
# We use this instead of Getopt::Long because it's faster and we're not parsing any
# crazy arguments
# Borrowed from: https://www.perturb.org/display/1153_Perl_Quick_extract_variables_from_ARGV.html
sub argv {
my $ret = {};
for (my $i = 0; $i < scalar(@ARGV); $i++) {
# If the item starts with "-" it's a key
if ((my ($key) = $ARGV[$i] =~ /^--?([a-zA-Z_]\w*)/) && ($ARGV[$i] !~ /^-\w\w/)) {
# If the next item does not start with "--" it's the value for this item
if (defined($ARGV[$i + 1]) && ($ARGV[$i + 1] !~ /^--?\D/)) {
$ret->{$key} = $ARGV[$i + 1];
# Bareword like --verbose with no options
} else {
$ret->{$key}++;
}
}
}
# We're looking for a certain item
if ($_[0]) { return $ret->{$_[0]}; }
return $ret;
}
# Output the command line usage for d-s-f
sub usage {
my $out = "Usage:
# Print diff-so-fancy version
diff-so-fancy --version
# One off fanciness
git diff --color | diff-so-fancy
# Configure git to use d-s-f for *all* diff operations
git config --global core.pager \"diff-so-fancy | less --tabs=4 -RFX\"
# If core.pager config is set, this can opt-out of using d-s-f
git --no-pager <command>
# Create a git alias to run d-s-f on demand
git config --global alias.dsf '!f() { [ -z \"\$GIT_PREFIX\" ] || cd \"\$GIT_PREFIX\" && git diff --color \"\$@\" | diff-so-fancy | less --tabs=4 -RFX; }; f'\n\n";
return $out;
}
sub output_default_colors {
my $out = "Recommended default colors for diff-so-fancy\n";
$out .= "--------------------------------------------\n";
$out .= 'git config --global color.ui true
git config --global color.diff-highlight.oldNormal "red bold"
git config --global color.diff-highlight.oldHighlight "red bold 52"
git config --global color.diff-highlight.newNormal "green bold"
git config --global color.diff-highlight.newHighlight "green bold 22"
git config --global color.diff.meta "227"
git config --global color.diff.frag "magenta bold"
git config --global color.diff.commit "227 bold"
git config --global color.diff.old "red bold"
git config --global color.diff.new "green bold"
git config --global color.diff.whitespace "red reverse"
';
return $out;
}
# Output the current version string
sub version {
my $ret = "Diff-so-fancy: https://github.com/so-fancy/diff-so-fancy\n";
$ret .= "Version : $VERSION\n\n";
return $ret;
}
# Feed the raw git input through diff-highlight to get line level highlights
#
# Note: This returns a file handle
sub filter_stdin_through_diff_highlight {
my $dh = find_diff_highlight();
# Pipe the STDIN of this to diff-highlight
my $pid = open(my $cmd_output, "-|", $dh);
return $cmd_output;
}
# Find the location of diff-highlight
sub find_diff_highlight {
my $dh = "diff-highlight";
my $dh_in_path = trim(`which $dh 2>/dev/null`);
my $ret;
# We check for diff-highlight in three places:
# 1) System path
# 2) Same dir as d-s-f
# 3) third_party/diff-highlight/
# This is #1
if ($dh_in_path) {
$ret = $dh_in_path;
# This is #2 and #3
} else {
$ret = dirname(Cwd::realpath($0)) . "/diff-highlight";
if (!-X $ret) {
$ret = dirname(Cwd::realpath($0)) . "/third_party/$dh/diff-highlight";
}
}
if (!-X $ret) {
die("Error locating diff-highlight\n");
}
return $ret;
}