Render Unified Diff with Python

1.2k Views Asked by At

I have a string which contains svn unified diff. My PyGTK app need to show this diff to user, and I want to render it like other diff tools do, or at least have it colorized.

Do you have something to suggest, external tool, library, custom implementation...? I was loking at http://kafka.fr.free.fr/diff2html/ but I prefer to use some library or sth like that, so users don't need to install third party apps.

I want use this for git and mercurial diffs later as well.

3

There are 3 best solutions below

12
On BEST ANSWER

You could use difflib to generate diffs, and pygtkscintilla for syntax-highlighting, line-numbering, code-folding, etc.

If you only want syntax-highlighting (as opposed to all the editor features offered by pygtkscintilla), then you could also look at pygments.

1
On

The difflib.HtmlDiff class provides facilities for doing this. However, instead of starting with a unified diff file, HtmlDiff wants you to pass the complete "before" and "after" files. These files are easy to get with svn/git/mercurial commands without using the "diff" functionality of those VCS.

0
On

GtkSourceView is a drop in replacement for pygtk's TextView that can syntax-highlight diff files, including unified diffs.