summaryrefslogtreecommitdiff
path: root/rc
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2024-09-14 12:25:01 +0200
committerMaxime Coste <mawww@kakoune.org>2024-09-16 15:23:18 +1000
commitaac32e0f5ad3332775a4f3e3092a4fd8a6c0db62 (patch)
tree0cf3235244a462d1ac0208120f0c674bddded2c2 /rc
parent4f5001b4d315404697d2324a400ebfd124626d49 (diff)
patch-range.pl: extract peculiarities
We pipe the output of the patch program to stderr (to have it show up in *debug*) and print to stdout the remaining diff (the part of the diff that was not passed to patch). The next patch wants to use this script in a different way, so move these decisions up.
Diffstat (limited to 'rc')
-rwxr-xr-xrc/tools/patch-range.pl42
-rw-r--r--rc/tools/patch.kak2
2 files changed, 28 insertions, 16 deletions
diff --git a/rc/tools/patch-range.pl b/rc/tools/patch-range.pl
index 978f45c3..2d54ab26 100755
--- a/rc/tools/patch-range.pl
+++ b/rc/tools/patch-range.pl
@@ -3,6 +3,12 @@
use strict;
use warnings;
+my $print_remaining = 0;
+if ($ARGV[0] eq "-print-remaining-diff") {
+ $print_remaining = 1;
+ shift @ARGV;
+}
+
my $min_line = $ARGV[0];
shift @ARGV;
my $max_line = $ARGV[0];
@@ -20,13 +26,13 @@ my $lineno = 0;
my $original = "";
my $diff_header = "";
my $wheat = "";
-my $chaff = "";
+my $chaff = "" if $print_remaining;
my $state = undef;
my $hunk_wheat = undef;
-my $hunk_chaff = undef;
+my $hunk_chaff = undef if $print_remaining;
my $hunk_header = undef;
my $hunk_remaining_lines = undef;
-my $signature = "";
+my $signature = "" if $print_remaining;
sub compute_hunk_header {
my $original_header = shift;
@@ -50,7 +56,9 @@ sub finish_hunk {
}
$wheat .= (compute_hunk_header $hunk_header, $hunk_wheat). $hunk_wheat;
}
- $chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff . $signature;
+ if ($print_remaining) {
+ $chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff . $signature;
+ }
$hunk_header = undef;
}
@@ -63,7 +71,7 @@ while (<STDIN>) {
$diff_header = "";
}
if ($state eq "signature") {
- $signature .= $_;
+ $signature .= $_ if $print_remaining;
next;
}
if (m{^@@ -\d+(?:,(\d)+)? \+\d+(?:,\d+)? @@}) {
@@ -72,42 +80,46 @@ while (<STDIN>) {
$state = "diff hunk";
$hunk_header = $_;
$hunk_wheat = "";
- $hunk_chaff = "";
- $signature = "";
+ if ($print_remaining) {
+ $hunk_chaff = "";
+ $signature = "";
+ }
next;
}
if ($state eq "diff header") {
$diff_header .= $_;
- $chaff .= $_;
+ $chaff .= $_ if $print_remaining;
next;
}
if ($hunk_remaining_lines == 0 and m{^-- $}) {
$state = "signature";
- $signature .= $_;
+ $signature .= $_ if $print_remaining;
next;
}
--$hunk_remaining_lines if m{^[ -]};
my $include = m{^ } || ($lineno >= $min_line && $lineno <= $max_line);
if ($include) {
$hunk_wheat .= $_;
- $hunk_chaff .= $_ if m{^ };
- if ($reverse ? m{^[-]} : m{^\+}) {
- $hunk_chaff .= " " . substr $_, 1;
+ if ($print_remaining) {
+ $hunk_chaff .= $_ if m{^ };
+ if ($reverse ? m{^[-]} : m{^\+}) {
+ $hunk_chaff .= " " . substr $_, 1;
+ }
}
} else {
if ($reverse ? m{^\+} : m{^-}) {
$hunk_wheat .= " " . substr $_, 1;
}
- $hunk_chaff .= $_;
+ $hunk_chaff .= $_ if $print_remaining;
}
}
finish_hunk();
-open PATCH_COMMAND, "|-", "$patch_cmd 1>&2" or die "patch-range.pl: error running '$patch_cmd': $!";
+open PATCH_COMMAND, "|-", "$patch_cmd" or die "patch-range.pl: error running '$patch_cmd': $!";
print PATCH_COMMAND $wheat;
if (not close PATCH_COMMAND) {
print $original;
print STDERR "patch-range.pl: error running:\n" . "\$ $patch_cmd << EOF\n$wheat" . "EOF\n";
exit 1;
}
-print $chaff;
+print $chaff if $print_remaining;
diff --git a/rc/tools/patch.kak b/rc/tools/patch.kak
index a481ff41..4ac50f24 100644
--- a/rc/tools/patch.kak
+++ b/rc/tools/patch.kak
@@ -52,7 +52,7 @@ define-command patch -params .. -docstring %{
# a shell where it expands to nothing.
eval set -- "$kak_quoted_reg_a"
- perl "${kak_runtime}"/rc/tools/patch-range.pl $min_line $max_line "$@" ||
+ perl "${kak_runtime}"/rc/tools/patch-range.pl -print-remaining-diff $min_line $max_line "$@" ">&2" ||
echo >$kak_command_fifo "set-register e fail 'patch: failed to apply selections, see *debug* buffer'"
}
execute-keys |<ret>