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

StackOverflow https://stackoverflow.com/questions/16043063

  •  04-04-2022
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top