rc tools git: fix initial position of blame of deleted line

When running git blame in a "git show" buffer, we annotate the youngest
version of the file that has the line referenced by the diff line
at cursor.
In case the cursor is on an added or context line, we simply show
the version from the surrounding commit.

When the cursor is on a deleted line, we show the parent commit,
which still has the deleted line.  However there is a bug: we use
the line number in the new version of the file. Fix that.
This commit is contained in:
Johannes Altmanninger 2024-02-12 10:30:28 +01:00 committed by Maxime Coste
parent 22b1d4ec4a
commit 5f0729f155
8 changed files with 41 additions and 2 deletions

View File

@ -225,9 +225,14 @@ define-command -params 1.. \
require-module diff require-module diff
try %{ try %{
diff-parse END %{ diff-parse END %{
$commit = "$commit~" if $diff_line_text =~ m{^[-]}; my $line = $file_line;
if ($diff_line_text =~ m{^[-]}) {
$commit = "$commit~";
$line = $other_file_line;
}
$line = $line or 1;
printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d", printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d",
$commit, quote($file), ($file_line or 1), ('${kak_cursor_column}' - 1); $commit, quote($file), $line, ('${kak_cursor_column}' - 1);
} }
} catch %{ } catch %{
echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error} echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error}

View File

@ -0,0 +1 @@
:run<ret>

View File

@ -0,0 +1,2 @@
#!/bin/sh
command -v git >/dev/null

View File

@ -0,0 +1 @@
../env

View File

@ -0,0 +1,3 @@
line 1
line 2
line 3

View File

@ -0,0 +1,2 @@
line 2

View File

@ -0,0 +1,14 @@
declare-option str jumpclient
source "%val{runtime}/rc/filetype/diff.kak"
source "%val{runtime}/rc/tools/git.kak"
define-command run %{
git init
git add
git commit --message 'initial commit'
execute-keys %{2gIchanged <esc>}
write
git commit --all --message 'changed line 2'
# Show the commit, jumping to the new version of line 2.
git blame-jump
}

View File

@ -0,0 +1,11 @@
ui_out -ignore 7
ui_out -ignore 11
# We've jumped to the new version of line 2. Move to the old version so we
# can annotate the old file.
ui_in '{ "jsonrpc": "2.0", "method": "keys", "params": [ "k:git blame<ret>" ] }'
ui_out -ignore 11
# We should have jumped to the old version of line 2, assert on kak_selection.
ui_in '{ "jsonrpc": "2.0", "method": "keys", "params": [ "x" ] }'
ui_out -ignore 5