3

Given a patch (without having both files, e.g.) git show -c > some_patch.c, is it possible to view this patch in vimdiff with a side-by-side view?

Or does this require having both source and destination files?

1 Answer 1

3

Do you have the original file and the patch file? If yes, you can open the original file in Vim and run

:diffpatch patch-file

If you only have the patch file you can try this command for a start:

:g/^--- .*\n+++ .*/+2;/\(\n--- \|\%$\)/y | tab new | put | %s/^\([- ]\|+.*\n\)// | set nomodified | diffthis | vert new | put | %s/^\([+ ]\|-.*\n\)// | set nomodified | diffthis | tabrewind

This yanks all lines between the start of a diff block and the start of the next diff block or the end of file into the default registers. Then it creates a new tab where the old and the new state will be shown in diff mode in two windows. In both windows the complete patch is inserted. Then in the first window the markers for unchanged and deleted lines (leading spaces and minuses) are removed and all lines added by the patch are completely removed. In the second window the markers for unchanged and added lines (leading spaces and pluses) are removed, as are the complete lines deleted by the patch.

Both windows are put into diff mode and marked as unmodified (just to make it easier to close the windows).

This command assumes that you start it from the first tab. Otherwise it will only create a tab for the first file in a patch, probably because the :g command is aborted by changing to another buffer and the :tabrewind at the end will not go back to the correct tab.

1
  • No, I don't necessarily have the original file. Some programs delta for example: github.com/dandavison/delta can show diff's without having the original files.
    – ideasman42
    Jan 20 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.