patch
creates new file, that's why it holds effective user credentials.
A workaround: use patch -o
to have temporary file created, then simply cat
tmp file to original file.
You don't need patch
for this; it's for extracting changes and sending them on without the unchanged part of the file.
The tool for merging two versions of a file is merge
, but as @vonbrand
wrote, you need the "base" file from which your two versions diverged. To do a merge without it, use diff
like this:
diff -DVERSION1 file1.xml file2.xml > merged.xml
It will enclose each set of changes in C-style #ifdef
/#ifndef
"preprocessor" commands, like this:
#ifdef VERSION1
<stuff added to file1.xml>
#endif
...
#ifndef VERSION1
<stuff added to file2.xml>
#endif
If a line or region differs between the two files, you'll get a "conflict", which looks like this:
#ifndef VERSION1
<version 1>
#else /* VERSION1 */
<version 2>
#endif /* VERSION1 */
So save the output in a file, and open it in an editor. Search for any places where #else
comes up, and resolve them manually. Then save the file and run it through grep -v
to get rid of the remaining #if(n)def
and #endif
lines:
grep -v '^#if' merged.xml | grep -v '^#endif' > clean.xml
In the future, save the original version of the file. merge
can give you much better results with the help of the extra information. (But be careful: merge
edits one of the files in-place, unless you use -p
. Read the manual).
Best Answer
If you're not giving any option to
patch
other than-pN
, it only creates those files when a patch fails to apply cleanly.So, one option is to stop creating (or accepting) bad patches. :)
Back in the real world, this is a feature. When
patch(1)
fails to apply a patch segment to the original file, it saves the temporary original file copy out durably as*.orig
, dumps the rejected segment to*.rej
, and continues trying to apply patch segments. The idea is that you can open the*.rej
file and complete the patch process manually by copying bits and pieces over to the patched file. The*.orig
file can also be useful when the patch process accidentally wrecks something, and you need to refer to the original version to fix it.I don't always fix a bad patch with text from the
*.rej
and*.orig
files, but it's nice to have them in case I need them.Once I've fixed up a bad patch, I run the following script at the project root to quickly clean things up:
I call it
cleanup-after-bad-patch
because the long name partially insures against running this accidentally, since it could remove files you still need. To be honest, though, I normally run it by typingclean
TabEnter, that being sufficient to find this script in thePATH
on my development machines.The additional patterns it checks for are for the files output by my version control system of choice when it encounters the same problem during a merge operation. You may wish to adjust it for your VCS/SCM tools.