From 796ccc45f0bcabb31c1ac5df36d2ced082ad294d Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 23 Mar 2025 16:06:23 +0100 Subject: rc git: fix blaming when in subdirectory When $PWD is different from $(git rev-parse --show-toplevel, recursive blaming is broken. For example (assuming k moves to a delted line): kak -e 'cd src; edit main.cc; git blame-jump; hook -once g NormalIdle .* %{ exec k; git blame-jump }' Fix this by 1. computing the correct path from diff-parse (we already require the git command be run in a git directory). 2. passing absolute paths to the recursive "git blame" invocations --- rc/tools/git.kak | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'rc') diff --git a/rc/tools/git.kak b/rc/tools/git.kak index 05e6ff14..df7137b1 100644 --- a/rc/tools/git.kak +++ b/rc/tools/git.kak @@ -229,7 +229,10 @@ define-command -params 1.. \ } require-module diff try %{ - diff-parse END %{ + diff-parse BEGIN %{ + $directory = qx(git rev-parse --show-toplevel); + chomp $directory; + } END %{ my $filename = $other_file; my $line = $other_file_line; if (not defined $commit) { @@ -246,8 +249,10 @@ define-command -params 1.. \ $line = $file_line; } $line = $line or 1; - printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %d %d", - $commit, quote($filename), $line, ('${kak_cursor_column}' - 1); + my $filename_relative = substr($filename, length "$directory/"); + printf "echo -to-file '${kak_response_fifo}' -quoting shell %s %s %s %d %d", + $commit, quote($filename), quote($filename_relative), + $line, ('${kak_cursor_column}' - 1); } } catch %{ echo -to-file '${kak_response_fifo}' -quoting shell -- %val{error} @@ -261,25 +266,26 @@ define-command -params 1.. \ exit fi commit=$1 - file=${2#"$PWD/"} - cursor_line=$3 - cursor_column=$4 - shift 4 + file_absolute=$2 + file_relative=$3 + cursor_line=$4 + cursor_column=$5 + shift 5 # Log commit and file name because they are only echoed briefly # and not shown elsewhere (we don't have a :messages buffer). - message="Blaming $file as of $(git rev-parse --short $commit)" + message="Blaming $file_relative as of $(git rev-parse --short $commit)" echo "echo -debug -- $(kakquote "$message")" on_close_fifo=" execute-keys -client ${kak_client} ${cursor_line}g${cursor_column}lh evaluate-commands -client ${kak_client} %{ - set-option buffer git_blob $(kakquote "$commit:$file") + set-option buffer git_blob $(kakquote "$commit:$file_absolute") git blame $(for arg; do kakquote "$arg"; printf " "; done) hook -once window NormalIdle .* %{ execute-keys vv echo -markup -- $(kakquote "{Information}{\\}$message. Press to jump to blamed commit") } } - " show_git_cmd_output show "$commit:$file" + " show_git_cmd_output show "$commit:$file_relative" exit } fi if [ -n "${kak_opt_git_blob}" ]; then { @@ -570,6 +576,8 @@ define-command -params 1.. \ try %{ diff-parse BEGIN %{ $version = "-"; + $directory = qx(git rev-parse --show-toplevel); + chomp $directory; } END %{ if ($diff_line_text !~ m{^[ -]}) { print quote "git blame-jump: recursive blame only works on context or deleted lines"; -- cgit v1.2.3