Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary diff improvements #293

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,37 @@ sub do_dsf_stuff {
#########################
# Look for the filename #
#########################
# $4 $5
} elsif ($line =~ /^${ansi_color_regex}diff (-r|--git|--cc) (.+?)(\s|\e|$)/) {
} elsif ($line =~ /^${ansi_color_regex}diff (.*)/) {
my $diff_args = $4;

# Mercurial looks like: diff -r 82e55d328c8c hello.c
if ($4 eq "-r") {
if ($diff_args =~ /^-r [0-9a-f]+ /) {
$is_mercurial = 1;
$meta_color ||= get_config_color("meta");

# Git looks like: diff --git a/diff-so-fancy b/diff-so-fancy
} elsif ($diff_args =~ /^(?:--git|--cc) (.+?)(\s|\e|$)/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?: is a non-capturing parenthesis right? I don't use this syntax very often so this might confuse me in the future. Is there a reason to do this, and not just have $last_file_seen = $2 ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I used it out of a vague sense that it's more efficient, but even if it is, I'm sure it's trivial, so I'll switch it.

$last_file_seen = $1;

# Otherwise, last two args are file names. Assume they have no
# spaces, as "a b c" is ambiguous anyway.
} else {
$last_file_seen = $5;
$diff_args =~ s/\e.*$//;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do these two lines do? Can you at least add a comment on what these regexp replaces accomplish. Maybe a before and after so we know what the strings should look like?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments.

$diff_args =~ /([^ ]+) [^ ]+$/;
$last_file_seen = $1;
}

$last_file_seen =~ s|^\w/||; # Remove a/ (and handle diff.mnemonicPrefix).
$in_hunk = 0;
########################################
# Look for added / removed files (non git, non -N)
########################################
} elsif ($line =~ /^${ansi_color_regex}Only in /) {
unless ($1) {
print get_config_color("meta");
}
print $line;
########################################
# Find the first file: --- a/README.md #
########################################
} elsif (!$in_hunk && $line =~ /^$ansi_color_regex--- (\w\/)?(.+?)(\e|\t|$)/) {
Expand Down Expand Up @@ -238,6 +254,8 @@ sub do_dsf_stuff {

if ($1) {
print $1; # Print out whatever color we're using
} elsif ($manually_color_lines) {
print get_config_color("frag");
}

my ($orig_offset, $orig_count, $new_offset, $new_count) = parse_hunk_header($hunk_header);
Expand Down Expand Up @@ -800,6 +818,8 @@ sub color {
if ($str eq "meta") {
# Default ANSI yellow
$ret = DiffHighlight::color_config('color.diff.meta', color(11));
} elsif ($str eq "frag") {
$ret = DiffHighlight::color_config('color.diff.frag', color('bold') . color(13));
} elsif ($str eq "reset") {
$ret = color("reset");
} elsif ($str eq "add_line") {
Expand Down
5 changes: 5 additions & 0 deletions test/diff-so-fancy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,8 @@ output=$( load_fixture "ls-function" | $diff_so_fancy )
assert_line --index 1 --partial "modified: doc/manual.xml.head"
assert_line --index 3 --partial "@ doc/manual.xml.head:8355 @"
}

@test "arbitrary diff args" {
output=$( load_fixture "unified-recursive" | $diff_so_fancy )
refute_output --partial 'diff -ur'
}
25 changes: 25 additions & 0 deletions test/fixtures/unified-recursive.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Only in new: added.txt
Only in old: deleted.txt
diff -ur old/modified.txt new/modified.txt
--- old/modified.txt 2018-03-22 11:45:03.000000000 -0700
+++ new/modified.txt 2018-03-22 11:45:11.000000000 -0700
@@ -1 +1 @@
-this is the old version
+this is the new version
diff -ur old/modified2.txt new/modified2.txt
--- old/modified2.txt 2018-03-22 13:23:01.000000000 -0700
+++ new/modified2.txt 2018-03-22 13:23:16.000000000 -0700
@@ -1,6 +1,6 @@
context1
context2
-old stuff 1
+new stuff 1
context3
context4
context5
@@ -8,4 +8,4 @@
context7
context8
context9
-old stuff 2
+new stuff 2