Skip to content

Commit

Permalink
Try and be smart about what line the diff starts on
Browse files Browse the repository at this point in the history
1: @@ -1,4 +1,4 @@
2: @@ -1,5 +1,5 @@
3: @@ -1,6 +1,6 @@
4: @@ -1,7 +1,7 @@
5: @@ -2,6 +2,7 @@
6: @@ -3,7 +3,7 @@

Starting from the end (last line (10), second to last line (9))...
-1: @@ -7,4 +7,4 @@
-2: @@ -6,5 +6,5 @@
-3: @@ -5,6 +5,6 @@
-4: @@ -4,7 +4,7 @@
  • Loading branch information
Scott Baker committed Mar 15, 2016
1 parent c1d24c4 commit d1a4c91
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/diff-so-fancy.pl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@
my ($orig_offset, $orig_count, $new_offset, $new_count) = parse_hunk_header($hunk_header);
$last_file_seen = basename($last_file_seen);

# Plus three line for context
print "@ $last_file_seen:" . ($new_offset + 3) . " \@${remain}\n";
# Figure out the start line
my $start_line = start_line_calc($new_offset,$new_count);
print "@ $last_file_seen:$start_line \@${remain}\n";

###################################
# Remove any new file permissions #
Expand Down Expand Up @@ -203,3 +204,27 @@ sub get_git_config {

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;

# 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;
}

0 comments on commit d1a4c91

Please sign in to comment.