rc diff: pass diff to diff-jump via stdin instead of env

Passing large diff buffers via the environment can quickly result in
the error "execve failed: Argument list too long". Use a pipe like
in format.kak

When running | (or <a-|>), Kakoune does not use %arg{@} to populate
"$@" (missing feature?). Work around this by moving %arg{@} to a
temporary register. Apparently $kak_quoted_reg_a will never be an
empty list, so work around that too.

When diff parsing fails, we take care to run "fail" in the calling
client, unlike :format (probably a bug in format.kak).

(This patch is best viewed while ignoring whitespace changes (diff -w))
This commit is contained in:
Johannes Altmanninger 2022-02-12 16:12:29 +01:00
parent eaaf562ed1
commit 13948ecb94

View File

@ -27,7 +27,7 @@ define-command diff-jump -params .. -docstring %{
- jump to the old file instead of the new file - jump to the old file instead of the new file
-<num> strip <num> leading directory components, like -p<num> in patch(1). Defaults to 1 if there is a 'diff' line (as printed by 'diff -r'), or 0 otherwise. -<num> strip <num> leading directory components, like -p<num> in patch(1). Defaults to 1 if there is a 'diff' line (as printed by 'diff -r'), or 0 otherwise.
} %{ } %{
evaluate-commands -draft -save-regs c %{ evaluate-commands -draft -save-regs ac| %{
# Save the column because we will move the cursor. # Save the column because we will move the cursor.
set-register c %val{cursor_column} set-register c %val{cursor_column}
# If there is a "diff" line, we don't need to look further back. # If there is a "diff" line, we don't need to look further back.
@ -39,111 +39,114 @@ define-command diff-jump -params .. -docstring %{
# or content. # or content.
execute-keys Gk execute-keys Gk
} }
evaluate-commands %sh{ set-register a %arg{@}
printf %s "$kak_selection" | set-register | %{
column=$kak_reg_c perl -we ' [ -n "$kak_reg_a" ] && eval set -- $kak_quoted_reg_a
sub quote { cmd=$(column=$kak_reg_c perl -we '
$SQ = "'\''"; sub quote {
$token = shift; $SQ = "'\''";
$token =~ s/$SQ/$SQ$SQ/g; $token = shift;
return "$SQ$token$SQ"; $token =~ s/$SQ/$SQ$SQ/g;
return "$SQ$token$SQ";
}
sub fail {
$reason = shift;
print "fail " . quote("diff-jump: $reason");
exit;
}
$version = "+", $other_version = "-";
$strip = undef;
$directory = $ENV{PWD};
$seen_ddash = 0;
foreach (@ARGV) {
if ($seen_ddash or !m{^-}) {
$directory = $_;
} elsif ($_ eq "-") {
$version = "-", $other_version = "+";
} elsif (m{^-(\d+)$}) {
$strip = $1;
} elsif ($_ eq "--") {
$seen_ddash = 1;
} else {
fail "unknown option: $_";
} }
sub fail { }
$reason = shift; $have_diff_line = 0;
print "fail " . quote("diff-jump: $reason"); $state = "header";
exit 1; while (<STDIN>) {
} s/^(> )*//g;
$version = "+", $other_version = "-"; $last_line = $_;
$strip = undef; if (m{^diff\b}) {
$directory = $ENV{PWD}; $state = "header";
$seen_ddash = 0; $have_diff_line = 1;
foreach (@ARGV) { if (m{^diff -\S* (\S+) (\S+)$}) {
if ($seen_ddash or !m{^-}) { $fallback_file = $version eq "+" ? $2 : $1;
$directory = $_;
} elsif ($_ eq "-") {
$version = "-", $other_version = "+";
} elsif (m{^-(\d+)$}) {
$strip = $1;
} elsif ($_ eq "--") {
$seen_ddash = 1;
} else {
fail "unknown option: $_";
} }
next;
} }
$have_diff_line = 0; if ($state eq "header") {
$state = "header"; if (m{^[$version]{3} ([^\t\n]+)}) {
while (<STDIN>) { $file = $1;
s/^(> )*//g;
$last_line = $_;
if (m{^diff\b}) {
$state = "header";
$have_diff_line = 1;
if (m{^diff -\S* (\S+) (\S+)$}) {
$fallback_file = $version eq "+" ? $2 : $1;
}
next; next;
} }
if ($state eq "header") { if (m{^[$other_version]{3} ([^\t\n]+)}) {
if (m{^[$version]{3} ([^\t\n]+)}) { $fallback_file = $1;
$file = $1; next;
}
}
if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
$state = "contents";
$line = ($version eq "+" ? $2 : $1) - 1;
} elsif (m{^[ $version]}) {
$line++ if defined $line;
}
}
if (not defined $file) {
$file = $fallback_file;
}
if (not defined $file) {
fail "missing diff header";
}
if (not defined $strip) {
# A "diff -r" or "git diff" adds "diff" lines to
# the output. If no such line is present, we have
# a plain diff between files (not directories), so
# there should be no need to strip the directory.
$strip = $have_diff_line ? 1 : 0;
}
if ($file !~ m{^/}) {
$file =~ s,^([^/]+/+){$strip},, or fail "directory prefix underflow";
$file = "$directory/$file";
}
if (defined $line) {
$column = $ENV{column} - 1; # Account for [ +-] diff prefix.
# If the cursor was on a hunk header, go to the section header if possible.
if ($last_line =~ m{^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+) @@ )([^\n]*)}) {
$hunk_header_prefix = $1;
$hunk_header_from_userdiff = $2;
open FILE, "<", $file or fail "failed to open file: $!: $file";
@lines = <FILE>;
for (my $i = $line - 1; $i >= 0 && $i < scalar @lines; $i--) {
if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
next; next;
} }
if (m{^[$other_version]{3} ([^\t\n]+)}) { $line = $i + 1;
$fallback_file = $1; # Re-add 1 because the @@ line does not have a [ +-] diff prefix.
next; $column = $column + 1 - length $hunk_header_prefix;
} last;
}
if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
$state = "contents";
$line = ($version eq "+" ? $2 : $1) - 1;
} elsif (m{^[ $version]}) {
$line++ if defined $line;
} }
} }
if (not defined $file) { }
$file = $fallback_file;
}
if (not defined $file) {
fail "missing diff header";
}
if (not defined $strip) {
# A "diff -r" or "git diff" adds "diff" lines to
# the output. If no such line is present, we have
# a plain diff between files (not directories), so
# there should be no need to strip the directory.
$strip = $have_diff_line ? 1 : 0;
}
if ($file !~ m{^/}) {
$file =~ s,^([^/]+/+){$strip},, or fail "directory prefix underflow";
$file = "$directory/$file";
}
if (defined $line) { printf "edit -existing -- %s $line $column", quote($file);
$column = $ENV{column} - 1; # Account for [ +-] diff prefix. ' -- "$@")
# If the cursor was on a hunk header, go to the section header if possible. echo "set-register c $cmd" >"$kak_command_fifo"
if ($last_line =~ m{^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+) @@ )([^\n]*)}) {
$hunk_header_prefix = $1;
$hunk_header_from_userdiff = $2;
open FILE, "<", $file or fail "failed to open file: $!: $file";
@lines = <FILE>;
for (my $i = $line - 1; $i >= 0 && $i < scalar @lines; $i--) {
if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
next;
}
$line = $i + 1;
# Re-add 1 because the @@ line does not have a [ +-] diff prefix.
$column = $column + 1 - length $hunk_header_prefix;
last;
}
}
}
printf "set-register c %s $line $column", quote($file);
' -- "$@"
} }
execute-keys <a-|><ret>
evaluate-commands -client %val{client} %{ evaluate-commands -client %val{client} %{
evaluate-commands -try-client %opt{jumpclient} %{ evaluate-commands -try-client %opt{jumpclient} %{
edit -existing -- %reg{c} %reg{c}
} }
} }
} }
@ -156,7 +159,7 @@ define-command \
-docstring %{diff-select-file: Select surrounding patch file} \ -docstring %{diff-select-file: Select surrounding patch file} \
-params 0 \ -params 0 \
diff-select-file %{ diff-select-file %{
evaluate-commands -itersel -save-regs 'ose/' %{ evaluate-commands -itersel -save-regs 'ose/' %{
try %{ try %{
execute-keys '"oZgl<a-?>^diff <ret>;"sZ' 'Ge"eZ' execute-keys '"oZgl<a-?>^diff <ret>;"sZ' 'Ge"eZ'
try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' } try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' }