Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore whitespace when doing a merge in mercurial

We're hitting a problem when merging in Mercurial where whitespace changes are causing merge conflicts which mask any "real" conflicts we may have and makes merging a nightmare. We've recently conformed to a formatting style which changed the indentation of files in some branches and merging has become almost impossible since.

As an example, try:

hg init testrepo
cd testrepo/

echo "This is text." > newfile.txt
hg add newfile.txt
hg commit -m "Created a file."
hg branch newbranch
echo "This is some more text." > newfile.txt
hg commit -m "Changed text in the file."
hg update default
echo "   This is text." > newfile.txt
hg commit -m "Added indentation whitespace."

This results in two branches, one with whitespace changes, the other with textual changes:

@  2     "   This is text".
|    
|
| o  1   "This is some more text."
|/     
|
o  0     "This is text."

When trying an hg merge on this I get a merge conflict. If we have conflicts on every line it becomes difficult and time consuming to sort out the "real" conflicts. What I'd prefer is for the merge process to think "OK, changeset 2 line 1 differs from the parent only in whitespace so consider it unchanged. Pick changeset 1 line 1 as the merged result."

like image 275
Mark Silberbauer Avatar asked Apr 07 '11 15:04

Mark Silberbauer


1 Answers

Ignoring whitespace or not is a choice your merge tool makes. You can configure all manner of merge tools for use with mercurial as shown here: MergeToolConfiguration

Mercurial's internal pre-merge won't/can't ignore whitespace, but if your external merge tool does and it finds only whitespace changes it will exit immediately, and if it finds other that whitespace changes it can hide the whitespace changes when it does load.

For example, with the popular kdiff3 merge tool you'd enable the "White space 2/3-file merge" setting and tell it whether to pick left or right.

Tl;Dr: turn this on in your merge tool, not in mercurial.

like image 146
Ry4an Brase Avatar answered Sep 19 '22 14:09

Ry4an Brase