3

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?

2

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

  • If I reverse the file names in the patch header, i.e., first one is gradle, second one is /usr/bin/gradle, then the behaviour is the same. Is this what you mean? – Alex Oct 13 '15 at 15:59
  • Re permissions, yes, running with sudo. – Alex Oct 13 '15 at 16:01
  • See what you mean now, yes it does apply in my original directory in that case. Shouldn't it apply in /usr/bin if the second line is /usr/bin/gradle? – Alex Oct 13 '15 at 16:04
  • Bingo: lrwxrwxrwx 1 root root 26 Sep 9 2013 /usr/bin/gradle -> ../share/gradle/bin/gradle – Alex Oct 13 '15 at 16:09
  • Except now when I update the patch to look for /usr/share/bin/gradle, it doesn't see the path "couldn't find file to patch at input line 3". Why is it looking at line 3, aren't the first two lines the file paths? – Alex Oct 13 '15 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.