Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ TESTS = tests/newline1/run-test \
tests/fullheader3/run-test \
tests/fullheader4/run-test \
tests/whitespace/run-test \
tests/crlf/run-test
tests/crlf/run-test \
tests/whitespace-regression/run-test \
tests/interdiff-whitespace-w/run-test \
tests/patch-ignore-whitespace/run-test \
tests/whitespace-w/run-test

# These ones don't work yet.
# Feel free to send me patches. :-)
Expand Down
9 changes: 6 additions & 3 deletions doc/patchutils.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@
<term><option>-w</option>,
<option>--ignore-all-space</option></term>
<listitem>
<para>Ignore whitespace changes in patches.</para>
<para>Ignore whitespace changes in patches. Also makes patch
application ignore whitespace when reconstructing files.</para>
</listitem>
</varlistentry>

Expand Down Expand Up @@ -520,7 +521,8 @@
<term><option>-w</option>,
<option>--ignore-all-space</option></term>
<listitem>
<para>Ignore whitespace changes in patches.</para>
<para>Ignore whitespace changes in patches. Also makes patch
application ignore whitespace when reconstructing files.</para>
</listitem>
</varlistentry>

Expand Down Expand Up @@ -2977,7 +2979,8 @@ will pipe patch of file #2 to vim - -R
<term><option>-w</option>,
<option>--ignore-all-space</option></term>
<listitem>
<para>Ignore whitespace changes in patches.</para>
<para>Ignore whitespace changes in patches. Also makes patch
application ignore whitespace when reconstructing files.</para>
</listitem>
</varlistentry>

Expand Down
13 changes: 12 additions & 1 deletion src/interdiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,19 @@ apply_patch (FILE *patch, const char *file, int reverted)
else
basename = file;

/* Check if -w option is present in diff_opts */
int has_ignore_all_space = 0;
for (int i = 0; i < num_diff_opts; i++) {
if (strcmp(diff_opts[i], "-w") == 0) {
has_ignore_all_space = 1;
break;
}
}

w = xpipe(PATCH, &child, "w", (char **) (const char *[]) { PATCH,
reverted ? "-Rsp0" : "-sp0", file, NULL });
reverted ? (has_ignore_all_space ? "-Rlsp0" : "-Rsp0")
: (has_ignore_all_space ? "-lsp0" : "-sp0"),
file, NULL });

fprintf (w, "--- %s\n+++ %s\n", basename, basename);
line = NULL;
Expand Down
51 changes: 51 additions & 0 deletions tests/interdiff-whitespace-w/run-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

# This is an interdiff(1) testcase.
# Test: Comprehensive test for -w option enabling patch -l behavior

. ${top_srcdir-.}/tests/common.sh

# Test 1: Basic functionality - whitespace differences should be ignored with -w
cat << EOF > file1
line1
line2
line3
EOF

cat << EOF > file2
line1
line2 modified
line3
EOF

cat << EOF > file3
line1
line2 modified
line3
line4
EOF

${DIFF} -u file1 file2 > patch1 || true
${DIFF} -u file2 file3 > patch2 || true

# Without -w: should show whitespace differences
${INTERDIFF} patch1 patch2 > result-no-w 2>errors-no-w || exit 1

# With -w: should ignore whitespace differences and focus on content
${INTERDIFF} -w patch1 patch2 > result-w 2>errors-w || exit 1

# Both should succeed, but -w version should be cleaner
[ -s result-no-w ] || exit 1
[ -s result-w ] || exit 1

# The -w version should show addition of line4
grep "line4" result-w > /dev/null || exit 1

# Test 2: Regression - ensure other options still work
${INTERDIFF} -b patch1 patch2 > result-b 2>errors-b || exit 1
${INTERDIFF} -B patch1 patch2 > result-B 2>errors-B || exit 1

[ -s result-b ] || exit 1
[ -s result-B ] || exit 1

exit 0
81 changes: 81 additions & 0 deletions tests/patch-ignore-whitespace/run-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/sh

# This is an interdiff(1) testcase.
# Test: Verify that -w option enables patch -l behavior for whitespace tolerance

. ${top_srcdir-.}/tests/common.sh

# Create a scenario where patch application would fail without -l but succeed with -l
# This simulates the core functionality our change provides

# Create original file
cat << EOF > original.txt
function test() {
var x = 1;
var y = 2;
return x + y;
}
EOF

# Create version 1 with a change
cat << EOF > version1.txt
function test() {
var x = 1;
var y = 3;
return x + y;
}
EOF

# Create version 2 with whitespace differences that would cause patch to fail
cat << EOF > version2.txt
function test() {
var x = 1;
var y = 4;
return x + y;
}
EOF

# Create patches
${DIFF} -u original.txt version1.txt > patch1 || true
${DIFF} -u version1.txt version2.txt > patch2 || true

# Add some whitespace variations to make patch application challenging
# Modify patch2 to have different whitespace that would cause normal patch to fail
sed 's/var y = 4;/var y = 4; /' patch2 > patch2-ws

# Test without -w: This might fail due to whitespace issues
${INTERDIFF} patch1 patch2-ws > result-strict 2>errors-strict || STRICT_FAILED=$?

# Test with -w: This should succeed because -w enables patch -l
${INTERDIFF} -w patch1 patch2-ws > result-tolerant 2>errors-tolerant || exit 1

# Verify that -w version succeeded
[ $? -eq 0 ] || exit 1

# The result should show the change from y=3 to y=4
grep -E "(var y = 4|y = 4)" result-tolerant > /dev/null || exit 1

# Test a more direct scenario: patches that only differ in whitespace
cat << EOF > file-tabs.txt
line1
line2
line3
EOF

cat << EOF > file-spaces.txt
line1
line2
line3
EOF

${DIFF} -u original.txt file-tabs.txt > patch-tabs || true
${DIFF} -u original.txt file-spaces.txt > patch-spaces || true

# Without -w, this might show whitespace differences
${INTERDIFF} patch-tabs patch-spaces > result-ws-strict 2>errors-ws-strict || WS_STRICT_FAILED=$?

# With -w, this should handle the whitespace differences gracefully
${INTERDIFF} -w patch-tabs patch-spaces > result-ws-tolerant 2>errors-ws-tolerant || exit 1

# Success if we reach here
exit 0
53 changes: 53 additions & 0 deletions tests/whitespace-regression/run-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

# This is an interdiff(1) testcase.
# Test: Regression test to ensure existing behavior without -w is preserved

. ${top_srcdir-.}/tests/common.sh

# Create a simple scenario that should work the same way as before
cat << EOF > file1.txt
line1
line2
line3
EOF

cat << EOF > file2.txt
line1
line2 changed
line3
EOF

cat << EOF > file3.txt
line1
line2 changed again
line3
EOF

# Create patches
${DIFF} -u file1.txt file2.txt > patch1 || true
${DIFF} -u file2.txt file3.txt > patch2 || true

# Test without any whitespace options - should work as before
${INTERDIFF} patch1 patch2 > result-baseline 2>errors-baseline || exit 1

# Verify the result contains expected changes
grep "line2 changed again" result-baseline > /dev/null || exit 1

# Test with -b option (should still work)
${INTERDIFF} -b patch1 patch2 > result-b 2>errors-b || exit 1

# Test with -B option (should still work)
${INTERDIFF} -B patch1 patch2 > result-B 2>errors-B || exit 1

# Test with -i option (should still work)
${INTERDIFF} -i patch1 patch2 > result-i 2>errors-i || exit 1

# All tests should produce similar results for this simple case
[ -s result-baseline ] || exit 1
[ -s result-b ] || exit 1
[ -s result-B ] || exit 1
[ -s result-i ] || exit 1

# Success - existing behavior is preserved
exit 0
43 changes: 43 additions & 0 deletions tests/whitespace-w/run-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh

# This is an interdiff(1) testcase.
# Test: Verify that -w option makes patch application ignore whitespace

. ${top_srcdir-.}/tests/common.sh

# Simple test to verify -w functionality works
cat << EOF > file1
line1
line2
line3
EOF

cat << EOF > file2
line1
line2 changed
line3
EOF

cat << EOF > file3
line1
line2 changed
line3
line4
EOF

# Create patches
${DIFF} -u file1 file2 > patch1 || true
${DIFF} -u file2 file3 > patch2 || true

# Test basic functionality - both should work
${INTERDIFF} patch1 patch2 > result-no-w 2>errors-no-w || exit 1
${INTERDIFF} -w patch1 patch2 > result-with-w 2>errors-with-w || exit 1

# Both should produce output
[ -s result-no-w ] || exit 1
[ -s result-with-w ] || exit 1

# Should show addition of line4
grep "line4" result-with-w > /dev/null || exit 1

exit 0