undo-files: extract the cleanup code from strip in a function...
undo-files: extract the cleanup code from strip in a function
This logic is duplicated in multiple places and it missing some important parts.
So lets start dealing with the duplication first.