2023-10-22 22:03:32 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my $min_line = $ARGV[0];
|
|
|
|
shift @ARGV;
|
|
|
|
my $max_line = $ARGV[0];
|
|
|
|
shift @ARGV;
|
|
|
|
|
|
|
|
my $patch_cmd;
|
|
|
|
if (defined $ARGV[0] and $ARGV[0] =~ m{^[^-]}) {
|
|
|
|
$patch_cmd = "@ARGV";
|
|
|
|
} else {
|
|
|
|
$patch_cmd = "patch @ARGV";
|
|
|
|
}
|
|
|
|
my $reverse = grep /^(--reverse|-R)$/, @ARGV;
|
|
|
|
|
|
|
|
my $lineno = 0;
|
|
|
|
my $original = "";
|
2024-01-22 06:58:20 +01:00
|
|
|
my $diff_header = "";
|
2023-10-22 22:03:32 +02:00
|
|
|
my $wheat = "";
|
|
|
|
my $chaff = "";
|
|
|
|
my $state = undef;
|
|
|
|
my $hunk_wheat = undef;
|
|
|
|
my $hunk_chaff = undef;
|
|
|
|
my $hunk_header = undef;
|
2024-01-25 00:57:00 +01:00
|
|
|
my $hunk_remaining_lines = undef;
|
|
|
|
my $signature = "";
|
2023-10-22 22:03:32 +02:00
|
|
|
|
|
|
|
sub compute_hunk_header {
|
|
|
|
my $original_header = shift;
|
|
|
|
my $hunk = shift;
|
|
|
|
my $old_lines = 0;
|
|
|
|
my $new_lines = 0;
|
|
|
|
for (split /\n/, $hunk) {
|
|
|
|
$old_lines++ if m{^[ -]};
|
|
|
|
$new_lines++ if m{^[ +]};
|
|
|
|
}
|
|
|
|
my $updated_header = $original_header =~ s/^@@ -(\d+),\d+\s+\+(\d+),\d+ @@(.*)/@@ -$1,$old_lines +$2,$new_lines @\@$3/mr;
|
|
|
|
return $updated_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub finish_hunk {
|
|
|
|
return unless defined $hunk_header;
|
|
|
|
if ($hunk_wheat =~ m{^[-+]}m) {
|
2024-01-22 06:58:20 +01:00
|
|
|
if ($diff_header) {
|
|
|
|
$wheat .= $diff_header;
|
|
|
|
$diff_header = "";
|
|
|
|
}
|
2023-10-22 22:03:32 +02:00
|
|
|
$wheat .= (compute_hunk_header $hunk_header, $hunk_wheat). $hunk_wheat;
|
|
|
|
}
|
2024-01-25 00:57:00 +01:00
|
|
|
$chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff . $signature;
|
2023-10-22 22:03:32 +02:00
|
|
|
$hunk_header = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (<STDIN>) {
|
|
|
|
++$lineno;
|
|
|
|
$original .= $_;
|
|
|
|
if (m{^diff}) {
|
|
|
|
finish_hunk();
|
|
|
|
$state = "diff header";
|
2024-01-22 06:58:20 +01:00
|
|
|
$diff_header = "";
|
2023-10-22 22:03:32 +02:00
|
|
|
}
|
2024-01-25 00:57:00 +01:00
|
|
|
if ($state eq "signature") {
|
|
|
|
$signature .= $_;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (m{^@@ -\d+(?:,(\d)+)? \+\d+(?:,\d+)? @@}) {
|
|
|
|
$hunk_remaining_lines = $1 or 1;
|
2023-10-22 22:03:32 +02:00
|
|
|
finish_hunk();
|
|
|
|
$state = "diff hunk";
|
|
|
|
$hunk_header = $_;
|
|
|
|
$hunk_wheat = "";
|
|
|
|
$hunk_chaff = "";
|
2024-01-25 00:57:00 +01:00
|
|
|
$signature = "";
|
2023-10-22 22:03:32 +02:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($state eq "diff header") {
|
2024-01-22 06:58:20 +01:00
|
|
|
$diff_header .= $_;
|
2023-10-22 22:03:32 +02:00
|
|
|
$chaff .= $_;
|
|
|
|
next;
|
|
|
|
}
|
2024-01-25 00:57:00 +01:00
|
|
|
if ($hunk_remaining_lines == 0 and m{^-- $}) {
|
|
|
|
$state = "signature";
|
|
|
|
$signature .= $_;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
--$hunk_remaining_lines if m{^[ -]};
|
2023-10-22 22:03:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($reverse ? m{^\+} : m{^-}) {
|
|
|
|
$hunk_wheat .= " " . substr $_, 1;
|
|
|
|
}
|
|
|
|
$hunk_chaff .= $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish_hunk();
|
|
|
|
|
|
|
|
open PATCH_COMMAND, "|-", "$patch_cmd 1>&2" 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;
|