Patch Set 2: (1 inline comment)

The algorithm is improved but there are still situations which are not handled correctly. I attached two example repos to bug 300282.

We could nevertheless submit this change as it is an improvement.