we had a block of code in `filetools.apply_patch`, I hoisted that into a dedicated function so I can reuse it