Skip to content

Commit d1a4c91

Browse files
author
Scott Baker
committed
Try and be smart about what line the diff starts on
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 @@
1 parent c1d24c4 commit d1a4c91

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

lib/diff-so-fancy.pl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@
7373
my ($orig_offset, $orig_count, $new_offset, $new_count) = parse_hunk_header($hunk_header);
7474
$last_file_seen = basename($last_file_seen);
7575

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

7980
###################################
8081
# Remove any new file permissions #
@@ -203,3 +204,27 @@ sub get_git_config {
203204

204205
return \%hash;
205206
}
207+
208+
# Try and be smart about what line the diff hunk starts on
209+
sub start_line_calc {
210+
my ($line_num,$diff_context) = @_;
211+
my $ret;
212+
213+
# Git defaults to three lines of context
214+
my $default_context_lines = 3;
215+
# Three lines on either side, and the line itself = 7
216+
my $expected_context = ($default_context_lines * 2 + 1);
217+
218+
# The first three lines
219+
if ($line_num == 1 && $diff_context < $expected_context) {
220+
$ret = $diff_context - $default_context_lines;
221+
} else {
222+
$ret = $line_num + $default_context_lines;
223+
}
224+
225+
if ($ret < 1) {
226+
$ret = 1;
227+
}
228+
229+
return $ret;
230+
}

0 commit comments

Comments
 (0)