How can I trust Git merge?

﹥>﹥吖頭↗ 提交于 2020-01-23 06:41:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23523713/how-can-i-trust-git-merge

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!