Annotated diff file with "<<<<<<< mine" and ">>>>>>> yours" type markers
Asked Answered
E

1

6

I am trying to create a diff file using the Linux diff command that has markers like this (taken from diff3 man page):

          <<<<<<< mine
          lines from mine
          =======
          lines from yours
          >>>>>>> yours        

This format is very intuitive for me and allows me to easily fix merge conflicts in vim and it works great when I am trying to merge three files (mine, yours and original) using diff3 but I would like the same format from plain diff. I was hoping this would be simple but I have not been able to get it. I have tried most of the main options (-e,--ed, etc.) and even tried to create a --changed-group-format but was unsuccessful.

Hopefully this is something simple that I just overlooked.

UPDATE:

Two file diff example with added line, removed line and conflict line:

enter image description here

Eridanus answered 16/4, 2013 at 17:11 Comment(4)
The markers show conflicting changes on a file. To have conflicting changes you should have 3 files. I.e. there is a base file A, and two other files B and C which are versions of A. The markers show changes in B and C which are disagree. What could be a conflict in two files??Inharmonic
No, you can have conflicts with just two files. See the example I added.Eridanus
It is not conflict, it is a changed line, will be denoted with + and - in the textual diff.Inharmonic
Kan, line 6 in the example is a conflict as denoted by the "!".Eridanus
I
8

You could play with the diff options. The shell script below

diff \
   --unchanged-group-format='%=' \
   --old-group-format='' \
   --new-group-format='%>' \
   --changed-group-format='<<<<<<< mine
%<=======
%>>>>>>>> yours
' \
orig.txt new.txt

Outputs

This has some stuff
in that gets
modified in
a later version
<<<<<<< mine
and there is a conflict
=======
and there is a conflict (like here)
>>>>>>> yours
about which version
should be used.
newline

For the files as in your screenshot. You could look more formatting options for the diff in a documentation, e.g. here.

One liner (works from command line, at least on linux):

diff --unchanged-group-format="%=" --old-group-format="" --new-group-format="%>" --changed-group-format="<<<<<<< mine%c'\\12'%<=======%c'\\12'%>>>>>>>> yours%c'\\12'" orig.txt new.txt
Inharmonic answered 22/4, 2013 at 20:31 Comment(4)
That is exactly the output I was looking for. I cannot get your diff command to output though. Error is Unmatched '. Did you put that command in a file and source it or did you actually make it an executable? Which shell are you using?Eridanus
Yes, I've created a shell script, this is not easy to do multiline params. "GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)". Please follow the link, it has "%c" formatting, so you could put newline char.Inharmonic
Yah, I spent about half an hour a few days ago trying to get the newline character in with the "%c" formatting but I could never get it to work properly. I guess I will spend some more time trying to do it tonight since I now know it CAN be done...Eridanus
I had to remove the extra '\' on the '\\12' to get the one liner to work but once I did that it worked perfectly. Thanks!Eridanus

© 2022 - 2024 — McMap. All rights reserved.