From 1276e67ef720b19bfe5354495ae72e08c00226ee Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 22 Jan 2024 06:58:20 +0100 Subject: [PATCH] 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. --- rc/tools/patch-range.pl | 8 +++++++- test/tools/patch/ignore-first-file/cmd | 1 + test/tools/patch/ignore-first-file/in | 11 +++++++++++ test/tools/patch/ignore-first-file/out | 19 +++++++++++++++++++ test/tools/patch/ignore-first-file/rc | 1 + 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/tools/patch/ignore-first-file/cmd create mode 100644 test/tools/patch/ignore-first-file/in create mode 100644 test/tools/patch/ignore-first-file/out create mode 100644 test/tools/patch/ignore-first-file/rc diff --git a/rc/tools/patch-range.pl b/rc/tools/patch-range.pl index 10cc74f4..b3c3dc91 100755 --- a/rc/tools/patch-range.pl +++ b/rc/tools/patch-range.pl @@ -18,6 +18,7 @@ my $reverse = grep /^(--reverse|-R)$/, @ARGV; my $lineno = 0; my $original = ""; +my $diff_header = ""; my $wheat = ""; my $chaff = ""; my $state = undef; @@ -41,6 +42,10 @@ sub compute_hunk_header { sub finish_hunk { return unless defined $hunk_header; if ($hunk_wheat =~ m{^[-+]}m) { + if ($diff_header) { + $wheat .= $diff_header; + $diff_header = ""; + } $wheat .= (compute_hunk_header $hunk_header, $hunk_wheat). $hunk_wheat; } $chaff .= (compute_hunk_header $hunk_header, $hunk_chaff) . $hunk_chaff; @@ -53,6 +58,7 @@ while () { if (m{^diff}) { finish_hunk(); $state = "diff header"; + $diff_header = ""; } if (m{^@@}) { finish_hunk(); @@ -63,7 +69,7 @@ while () { next; } if ($state eq "diff header") { - $wheat .= $_; + $diff_header .= $_; $chaff .= $_; next; } diff --git a/test/tools/patch/ignore-first-file/cmd b/test/tools/patch/ignore-first-file/cmd new file mode 100644 index 00000000..2b5ac0b6 --- /dev/null +++ b/test/tools/patch/ignore-first-file/cmd @@ -0,0 +1 @@ +%:patch tee applied.diff!echo Applied:; cat applied.diff; echo; echo Updated buffer: diff --git a/test/tools/patch/ignore-first-file/in b/test/tools/patch/ignore-first-file/in new file mode 100644 index 00000000..5738a9b9 --- /dev/null +++ b/test/tools/patch/ignore-first-file/in @@ -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 diff --git a/test/tools/patch/ignore-first-file/out b/test/tools/patch/ignore-first-file/out new file mode 100644 index 00000000..11d3d3af --- /dev/null +++ b/test/tools/patch/ignore-first-file/out @@ -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 diff --git a/test/tools/patch/ignore-first-file/rc b/test/tools/patch/ignore-first-file/rc new file mode 100644 index 00000000..62ee8585 --- /dev/null +++ b/test/tools/patch/ignore-first-file/rc @@ -0,0 +1 @@ +source "%val{runtime}/rc/tools/patch.kak"