How to apply a Perforce patch?

2020-03-09 09:15发布

I generated a patch some time ago using the p4 diff command.

However, now that I want to apply it, I realize that there is no way to apply a patch in Perforce.

Since I did not use the -du option, the patch is in that obscure perforce format and can't be applied with patch(1). The original changes have been lost. Also, that patch is over 300kb long so manual editing isn't really an option.

What are my options ? Are there patch converters, or some Perforce extension allowing the application of these kind of patchs ?

3条回答
家丑人穷心不美
2楼-- · 2020-03-09 09:24

Just found your question after saving some of my changes in a patch and reverting them in order to test some of the original code...

Assuming you're using a decent terminal emulator, this one time operation could take you up to an hour or so.

The good thing is that you can't run into this on a daily basis, as you probably will now use "-du" from now on, at least me I'm sure I will. Just copy and paste the file name as prompted:

$ patch -i cucu.diff 
can't find file to patch at input line 2
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|==== //depot/foo/boo.cpp#1 - /home/who/perforce/foo/boo.cpp ====
--------------------------
File to patch: 

<double-click!> to copy
<middle-click!> to paste

File to patch: /home/who/perforce/foo/boo.cpp <CR>

Perhaps you will have 100 files in the patch... keep clicking :-)

If you're looking for a subst expression (vim, sed):

s/.* - \(.*\) ====/+++: \1/

But - beware if you added sources (whole files, p4 add) their content probably won't be printed in the diff - in which case nobody will be able to re-patch them in...

查看更多
Ridiculous、
3楼-- · 2020-03-09 09:24

Another answer:The p4 diff2 command supports a -u flag to generate diffs in GNU diff format.

From p4 help diff2:

The -u flag uses the GNU diff -u format and displays only files that differ. The file names and dates are in Perforce syntax, but the output can be used by the patch program.

查看更多
闹够了就滚
4楼-- · 2020-03-09 09:37

Perhaps Perforce's diff output format has changed, but @RumburaK's answer didn't work for me.

I had to modify it to convert the ==== header to diff's +++ and --- header format.

sed -Ee 's|==== (//.*)#[0-9]+(.*)|+++ \1\n--- \1|' < infile.txt > outfile.txt

infile.txt was generated with the following command (where 12335 was a shelved changelist): p4 describe -du -S 12345

outfile.txt was applied with the following command: patch -p3 -l < outfile.txt

查看更多
登录 后发表回答