rc tools patch: skip spurious diff header for files with no changes

Since :patch transforms its inputs into context-only lines, we can
easily get into a state where a file diff has only context lines.
git apply does not accept a "diff" without any hunk, so let's skip
that.
This commit is contained in:
Johannes Altmanninger 2024-01-22 06:58:20 +01:00
parent 869e89b3e5
commit 1276e67ef7
5 changed files with 39 additions and 1 deletions

View File

@ -18,6 +18,7 @@ my $reverse = grep /^(--reverse|-R)$/, @ARGV;
my $lineno = 0; my $lineno = 0;
my $original = ""; my $original = "";
my $diff_header = "";
my $wheat = ""; my $wheat = "";
my $chaff = ""; my $chaff = "";
my $state = undef; my $state = undef;
@ -41,6 +42,10 @@ sub compute_hunk_header {
sub finish_hunk { sub finish_hunk {
return unless defined $hunk_header; return unless defined $hunk_header;
if ($hunk_wheat =~ m{^[-+]}m) { if ($hunk_wheat =~ m{^[-+]}m) {
if ($diff_header) {
$wheat .= $diff_header;
$diff_header = "";
}
$wheat .= (compute_hunk_header $hunk_header, $hunk_wheat). $hunk_wheat; $wheat .= (compute_hunk_header $hunk_header, $hunk_wheat). $hunk_wheat;
} }
$chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff; $chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff;
@ -53,6 +58,7 @@ while (<STDIN>) {
if (m{^diff}) { if (m{^diff}) {
finish_hunk(); finish_hunk();
$state = "diff header"; $state = "diff header";
$diff_header = "";
} }
if (m{^@@}) { if (m{^@@}) {
finish_hunk(); finish_hunk();
@ -63,7 +69,7 @@ while (<STDIN>) {
next; next;
} }
if ($state eq "diff header") { if ($state eq "diff header") {
$wheat .= $_; $diff_header .= $_;
$chaff .= $_; $chaff .= $_;
next; next;
} }

View File

@ -0,0 +1 @@
%:patch tee applied.diff<ret><ret>!echo Applied:; cat applied.diff; echo; echo Updated buffer:<ret>

View File

@ -0,0 +1,11 @@
diff -ur a/file1 b/file1
--- a/file1
+++ b/file1
@@ -1 +1 @@
file1 here
diff -ur a/file2 b/file2
--- a/file2
+++ b/file2
@@ -1 +1 @@
-file2 here
+modified file2 here

View File

@ -0,0 +1,19 @@
Applied:
diff -ur a/file2 b/file2
--- a/file2
+++ b/file2
@@ -1 +1 @@
-file2 here
+modified file2 here
Updated buffer:
diff -ur a/file1 b/file1
--- a/file1
+++ b/file1
@@ -1 +1 @@
file1 here
diff -ur a/file2 b/file2
--- a/file2
+++ b/file2
@@ -1 +1 @@
modified file2 here

View File

@ -0,0 +1 @@
source "%val{runtime}/rc/tools/patch.kak"