Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trust Git merge?

Tags:

git

merge

I am not certain I can trust Git to merge automatically. Here is a scenario.

Create a program in the master:

    MOVE 0 TO I.
A.
    PERFORM X-PROC.
    IF I IS EQUAL TO 25 THEN GO TO A.

Developer 1 makes a branch and notices that there is a bug: an infinite loop. He fixes it:

    MOVE 0 TO I.
A.
    ADD 1 TO I.
    PERFORM X-PROC.
    IF I IS EQUAL TO 25 THEN GO TO A.

Meanwhile Developer 2 makes a branch and fixes the bug in her own way:

    MOVE 0 TO I.
A.
    PERFORM X-PROC.
    ADD 1 TO I.
    IF I IS EQUAL TO 25 THEN GO TO A.

Both developers test their code and find it correct. Both merge to the master:

    MOVE 0 TO I.
A.
    ADD 1 TO I.
    PERFORM X-PROC.
    ADD 1 TO I.
    IF I IS EQUAL TO 25 THEN GO TO A.

The infinite loop is back.

It seems to me that this problem must happen often in any sort of distributed development environment. When I tested this, Git did not report a merge conflict. Sometimes this problem could go undetected for a long time. A regression test should find it, but regression tests are merged in Git too, so we can't trust them either.

What can I do about this? Do I need to do a code reading after every merge?

like image 299
Mark Lutton Avatar asked May 07 '14 16:05

Mark Lutton


1 Answers

Do I need to do a code reading after every merge?

Yes, of course.

Automerge algorithms are helpful, but they're not magic; they just include the changes that were made to both sides of a file if they don't conflict. There's no guarantee that the resultant change compiles or is even not gibberish. There's no guarantee that the logic isn't a complete trainwreck. (Some have speculated that the Heartbleed bug was the result of an automerge changing the logic subtly and was not caught in review.)

This is true of any version control tool that does an automerge (which, presuming you're using something that was written in the last 15 years or so, almost certainly does.) Though this is not to impeach automerge, which resolves two changes to the same file and generally does a fine job; this is also true of a merge in general. If you modify some file A and I modify some file B, there's no guarantee that the merge makes sense.

Best practice: you should always review your merges before you commit or push them, even when they automerge successfully.

like image 124
Edward Thomson Avatar answered Sep 22 '22 13:09

Edward Thomson