Ever merge with SVN and mess up when resolving conflicts? Read on.

Does this look familiar?

<<<<<<< .mine  
    <span id="roundedBox" class="panel">
=======
    <div id="mainPanel" class="rounded panel">
>>>>>>> .r2348
        <img src="..." class="panelImage" />
<<<<<<< .mine  
    </span>
=======
    </div>
>>>>>>> .r2348

This is what Subversion (SVN) does to your files when it detects conflicts between your working copy and what it is pulling down from the server. It marks them as conflicted and inserts these metadata markers into the file storing both your version and the version from the server. You then have the ability to "edit conflicts" for each file marked as conflicted. Usually this process goes smoothly but as any SVN veteran has experienced, sometimes things go horribly wrong. There are numerous ways that conflicts can be resolved incorrectly such as simply clicking "resolved" instead of "edit conflicts". When this happens SVN marks the file as being resolved but leaves this ugly metadata in the file. It can be a huge pain to go through your files one by one and remove this metadata, leaving only the code you want.

I recently had an encounter with a fail merge and numerous conflicts were marked as resolved. There were hundreds of files now with this metadata that had to be removed and there was no easy way to remove it. In many cases it is clear which version you want, either your working copy version (the code under the metadata marked with "mine") or the server version (the code under the "=======" divider). I sat down and crafted a regular expression for Visual Studio's search and replace window. Simply press CTRL + H to open up the Quick Replace window. In the "Look in:" drop down menu, select the option that is appropriate for your needs. Expand the "Find Options" node at the bottom of the window and ensure that "Use:" is checked and that the drop down has "Regular expressions" selected.

Once you have the window up, paste in the following expression:

\<\<\<\<\<\<\< \.mine\n{(~(\>\>\>\>\>\>\>)(\n|.))+}\n=======\n{(~(\<\<\<\<\<\<\<)(\n|.))+}\n\>\>\>\>\>\>\> \.r:d+\n?

Now all you have to do is click "Find Next" and it will locate all your conflicts that contain this metadata. The expression contains groupings wrapped around the two versions of conflicted code. Depending on which one you want (yours or the server's) you can replace the matched conflict with the grouping of your choice. "\1" will take yours and "\2" will take the server's.

Now you can step through each conflict, taking the one you want. If you want the same version for all your conflicts, simply click "Replace All" and let it do its thing. The expression is so specific there is almost zero chance it will match anything but SVN conflict metadata. If we were to run the expression as it appears in the screen shot against the conflict listed at the top of this post, the result would look like this:

<span id="roundedBox" class="panel">  
    <img src="..." class="panelImage" />
</span>  

If we ran the same expression, but used "\2" instead, it would look like this:

<div id="mainPanel" class="rounded panel">  
    <img src="..." class="panelImage" />
</div>  

Hopefully this saves you a lot of time. I know it saved me and my team a lot!