'Patch semantics

I've made a patch by using diff:

diff -u /home/user/onderzoeksstage/omf/Rakefile /home/user/onderzoeksstage/Rakefile > rakefile2.patch

I've placed this rakefile2.patch in another directory: /home/user/onderzoeksstage/omf/confine/patches. Now, I was under the assumption that I could go to that directory where all my patches are collected, call patch < rakefile2.patch and patch would known where to find the file to patch (the original file /home/user/onderzoeksstage/omf/Rakefile) by reading out the rakefile2.patch header.

But when doing that, patch says that it does not find the file to patch:

[user@localhost patches]$ patch < rakefile2.patch 
can't find file to patch at input line 3
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|--- /home/user/onderzoeksstage/omf/Rakefile    2013-02-12 14:11:49.809792527 +0100
|+++ /home/user/onderzoeksstage/Rakefile    2013-02-12 12:17:50.314831492 +0100
--------------------------
File to patch: ...
...

So my assumption was obviously wrong, but so how does patch work? When going to /home/user/onderzoeksstage/omf/ and calling patch < rakefile2.patch does work. Does patch only look at the header for the filename at the end of the path and not take in account the directory? And so what I try to accomplish will never work? Why is this; is this because that way a patch could be applied to any file called Rakefile (e.g. in my case) and so make it a more "generic" patch?

Thanks



Solution 1:[1]

Does patch only look at the header for the filename at the end of the path and not take in account the directory?

That's what it does by default. See the description of -p option in man patch. Looks like -p0 is what you want here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton Kovalenko