2

I know a number of ChangeList of my friend. I want to pass this CL number to a Bash script, which will generate a diff file read for the patch program (which will recreate the change in the git repo).

So far I have this:

function p4_shelved_cl_to_diff()
{
    p4 describe -S -du $1 > p4_diff.patch
}

It generates the diff, but headers are in p4 format:

==== //p4_repo/dir_in_repo/dir/file.cpp#123 (text) ===

whereas they should be in

--- dir/file.cpp
+++ dir/file.cpp

So what I'm looking for is a special p4 syntax (googled extensively, checked p4 manual – little hope) or rather a sed/awk/whatever script that will do the change for me.

Any ideas, or maybe someone has this already written?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Koshmaar
  • 166
  • 13

1 Answers1

3

This substitution works for your example:

$ sed 's|^====.*/\([^/]*/[^#]*\).*===$|--- \1\n+++ \1|' infile
--- dir/file.cpp
+++ dir/file.cpp

It works on lines starting and ending with ==== (actually ending with ===, but I believe that's just a typo in the question, and it will also work for lines ending with ====).

It captures everything between the second to last / and the #, then adds the --- and +++ on separate lines.

Note that I haven't looked into the general format of p4 diff headers, so this might break in other cases – the better overall solution would be to fix what generates the wrong headers.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Thanks! I'm not sed wizard, but looks good, I will try it in two weeks when back at work :) "Second to last /" and general format - in the real diffs that I got, the dir/file.cpp was actually sth like dir1/dir2/dir3/file.cpp . I think script should be getting parameter - name of last dir on repo, or the number of / to remove, to make it work in the more general sense. And yes, best would be to get p4 generate normal unified diff. I guess now it's not possible and I would have to very loudly complain on their forums and then wait few weeks/months for the fix. – Koshmaar Feb 28 '16 at 10:23