-
Notifications
You must be signed in to change notification settings - Fork 335
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
line number of diff is incorrect. #120
Comments
This is a known bug that I don't have a good work around for. The raw chunk header for that request is I haven't found a good work around unless we want to scrape the entire diff first to calculate the real offsets. But that would add a lot of overhead for very little gain. |
# of lines of context
This number is configurable via some flags to hunk context
I was skeptical this was a good idea, but after trying to make use of the line numbers without the +3, it was really confusing. btw, the hunk context doesnt necessarily show the source from that line, but will often show the function name. this depends on the language used. see http://stackoverflow.com/questions/28111035/where-does-the-excerpt-in-the-git-diff-hunk-header-come-from AFAICT, the latest answer to this is here: https://github.com/git/git/blob/master/xdiff/xemit.c#L128-L149 also, there appear to be some language-specific parsers in git's ( i threw this stuff into our wiki to document it: https://github.com/so-fancy/diff-so-fancy/wiki ) ANYWAY, since the context line can be showing something that's VERY different with the changes in the first lines of a file
detecting that we're in the first three is easy enough. We can fix those values, IMO. I think we should do that. |
@scottchiefbaker can we adjust the line calculation if N is <= 3? |
I'll need some help with the logic. If I have a change on the first line the (raw) diff looks like this:
Currently we iterate over each line of the file, when we find a diff hunk we mangle that to output the new header. I won't know how many lines of context unless we parse the actual body of the diff, which gets a lot more complex. Or is there another way I'm missing. |
If I make a change on line two I get: |
Ok I think maybe I got it... What do you think about this raw data:
Using that logic I wrote this function: sub line_calc {
my ($line_num,$diff_context) = @_;
# 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) {
return $diff_context - $default_context_lines;
} else {
return $line_num + $default_context_lines;
}
} Just to clarify the current method only falls apart on the first 3 lines, everything else is fine. This should solve all the corners cases, except files that are less than 7 lines. |
Ok I'm replying to myself because all the cool kids are doing that these days. I think I have the logic applied in a branch on my repo. Will someone please test this out and see if it does what you want it to: d1a4c91 If so I'll submit a pull request. |
I'll add tests for this. |
👍 |
The bug still exists in
diff --git a/dotfiles/.config/git/config b/dotfiles/.config/git/config
index eaf6af0..17c61af 100644
--- a/dotfiles/.config/git/config
+++ b/dotfiles/.config/git/config
@@ -1,12 +1,12 @@
+; vim: fdm=marker foldlevel=0 sw=0 ts=4 sts=4 noet
+; Note:
+; - section could be defined multiple times, the latter overwrite the former.
[user] With ────────────────────────────────────────────────────────────────────────────────────>
modified: dotfiles/.config/git/config
────────────────────────────────────────────────────────────────────────────────────>
@ dotfiles/.config/git/config:4 @
; vim: fdm=marker foldlevel=0 sw=0 ts=4 sts=4 noet
; Note:
; - section could be defined multiple times, the latter overwrite the former.
[user] Line number 1 becomes 4. Related git config for ; diff-so-fancy {{{
; https://github.com/so-fancy/diff-so-fancy
[core]
; Remove -F from less, which implies -X only on less 530+
; -X means no screen clearing. -F will not clear content
pager = diff-so-fancy | less -g -i -J -M -R -S -w -x4 -z-4
[interactive]
diffFilter = diff-so-fancy --patch
[color]
ui = true
[color "diff"]
meta = 11
frag = magenta bold
commit = yellow bold
old = red bold
new = green bold
whitespace = red reverse
func = 146 bold
[color "diff-highlight"]
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22
oldNormal = red bold
; }}} |
@laggardkernel Is that the whole diff? Seems like it's missing another 8 lines of context and 3 removed? |
@ package.json:4 @
should be@ package.json:3 @
The text was updated successfully, but these errors were encountered: