I am interested in what the rules are for the Git conflict markers? Are they stacked? What are the rules to opening (<<<<<<<) and closing (>>>>>>>)? I tried searching the Git docs, but there is nothing explicit about these and the only examples I find are simple, single, non-stacked conflicts.
Context: I need to sync two independent branches that have the same directory structure partially. I am creating a patch from one and applying it to the other branch. Conflicts are possible, but I always want to take 'ours'.
For applying the patch I use:
git apply --3way --ignore-whitespace --whitespace=fix <patchfile>
Then for each file that contains conflict markers I do this:
perl -i -0777 -pe '<{7} ours\r?\n((?:(?!<{7})(?!={7})(?!>{7}).*\r?\n)*?)={7}\r?\n(?:(?!<{7})(?!={7})(?!>{7}).*\r?\n)*?>{7} theirs\r?\n'
Basically I assume that the markers are stacked (from what I noticed) and try to solve them from inside out (innermost to outermost). This is how the file is after applying the patch:
<<<<<<< ours
<<<<<<< ours
<<<<<<< ours
{chunk of text}
<<<<<<< ours
=======
{chunk of text}
>>>>>>> theirs
=======
{chunk of text}
>>>>>>> theirs
{chunk of text}
<<<<<<< ours
=======
=======
>>>>>>> theirs
{chunk of text}
<<<<<<< ours
>>>>>>> theirs
=======
>>>>>>> theirs
=======
>>>>>>> theirs
{rest of file}
The problem is that now I found a file that after 2 substitution iterations reaches this state:
<<<<<<< ours
<<<<<<< ours
{text chunk}
<<<<<<< ours
=======
=======
>>>>>>> theirs
{text chunk}
<<<<<<< ours
>>>>>>> theirs
=======
>>>>>>> theirs
=======
>>>>>>> theirs
<rest of file>
... and I don't know what to make of this. This doesn't seem stacked. How should I resolve these conflicts?
Note: I also ran the regex manually iteration by iteration in an online debugger and it matches ok.
Edit for clarifications: When I wrote this I failed to mention that I am using git format-patch to generate the patch and this actually generates a patch for each commit in order to keep metadata, hence the multiple conflict markers. @torek nailed it in his answer without having the proper information.