Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does git rerere figure out the similarities between two conflicts?

Ok, here goes my Q. Does "git rerere" compare hashes of two files to figure out the resolution? That is, say I have an XML file which contains this tag:

<number>12</number>

When I have a conflict, that number usually gets changed to something like 13, 14, etc, so I'm stuck with:

<<<<<<<
<number>12</number>
=======
<number>13</number>
>>>>>>>

Can rerere automatically resolve this conflict even if the numbers are not the same as the last time? I always want it to resolve it in such a way that it takes the higher number (in example above, 13). So if it records the resolution for numbers 12 and 13, will it resolve conflict with different numbers? I have a suspicion it won't, but might as well ask.

like image 279
mart1n Avatar asked Apr 18 '12 22:04

mart1n


2 Answers

Can rerere automatically resolve this conflict even if the numbers are not the same as the last time? I always want it to resolve it in such a way that it takes the higher number (in example above, 13).

No, git rerere can't do that.

When you use git rerere, you're asking it to remember two things:

  • a specific conflict (that is, the literal text of the hunks which are in conflict and the files that they're in); and
  • the resolution that should be applied to that conflict

Because these are hunk literals (e.g. "replace 12 with 13") rather than some kind of transformation function (e.g. "replace N with N + 1"), git is unable to infer that you want to replace each number with a higher one.

If git later encounters a conflict that doesn't match any of its previously remembered conflicts, it's just as if it's never encountered that conflict at all, even if it might be similar (as in your example where the numbers are always one off).

One way out is possible if this XML file is automatically generated. In that case, you might want to consider not keeping it in source control at all, and instead, generating it at runtime. Then you'll never have the corresponding conflicts to clean up.

Another approach is to avoid git rerere altogether, and instead write a custom merge driver that will resolve things for you. I've done this before, but it's a non-trivial amount of work, and it requires you to write and test some code. If you're interested in this strategy, see the link in Karl's answer.

like image 70
John Feminella Avatar answered Nov 05 '22 20:11

John Feminella


Vanilla git doesn't compare files that way. It does a strict string comparison and doesn't know anything about the semantics of it.

However, take a look at "Defining a custom merge driver" in the gitattributes man page. It lets you write your own program to resolve certain merges any way you want, including choosing the higher number.

like image 25
Karl Bielefeldt Avatar answered Nov 05 '22 19:11

Karl Bielefeldt