How to apply simple patch (reverse detected)

diff()patch

I'm trying to create and apply a simple patch to fix a bug with gradle.

I copy the file (/usr/bin/gradle) to my working directory, then make the change. I then create the patch:

diff -u /usr/bin/gradle gradle > gradle.patch

Then I attempt to apply it:

sudo patch < gradle.patch
Reversed (or previously applied) patch detected!  Assume -R? [n]

If I proceed anyway, it fails. I have tried generating the patch with the files reversed, and in this case it applies "successfully," but does nothing.

The generated patch looks like this:

--- /usr/bin/gradle 2013-09-09 16:47:58.000000000 -0400
+++ gradle  2015-10-13 11:39:36.005269565 -0400
@@ -67,7 +67,8 @@

 CLASSPATH=$APP_HOME/lib/gradle-launcher-1.4.jar

-export JAVA_HOME=/usr/lib/jvm/default-java
+# commenting out as per (https://stackoverflow.com/a/22309017)
+# export JAVA_HOME=/usr/lib/jvm/default-java

 # Determine the Java command to use to start the JVM.
 if [ -n "$JAVA_HOME" ] ; then

I have tried editing the file paths at the top of the patch to both be /usr/bin/gradle, however this doesn't help.

I have also tried passing -p0, in which case it give:

Ignoring potentially dangerous file name /usr/bin/gradle
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- /usr/bin/gradle    2013-09-09 16:47:58.000000000 -0400
|+++ /usr/bin/gradle    2015-10-13 11:39:36.005269565 -0400
--------------------------
File to patch: /usr/bin/gradle
File /usr/bin/gradle is not a regular file -- refusing to patch
1 out of 1 hunk ignored -- saving rejects to file /usr/bin/gradle.rej

When I specify the file in this case, it still fails.

This seems like a simple task, what am I doing wrong?

Best Answer

You need to patch the right file.

The way you call it, it will try to patch the new file and thus correctly assumes a reversed patch.

Try to copy an old version of the file to your current directory and the patch will apply.

Regarding your -p0 test: do you have permission to modify /usr/bin/gradle? Do you have permission to create files in /usr/bin?

Also note that there are deviating rules for getting the filename to patch.

The rules used by gpatch are different from both the original patch and the POSIX patch standard.

Note that the "not a regular file" message is specific to gpatch. Please list the file /usr/bin/gradle. The message is only printed when the file is not a regular file, so something looks strange.

So the solution is:

patch only patches plain files but the file you like to patch is a symbolic link.

If you have a copy of the plain (old version) file in your current directory, patch will do what you expect.

BTW: the usual rule for getting the file name to patch is to look for the shortest name as the usual naming convention is to use:

file.orig

and

file