rewrite: fix issue6599
Explanation inside.
Differential Revision: https://phab.mercurial-scm.org/D11685