Skip to main content

Diff of versioned templates in Magnolia

Posted by rah003 on January 22, 2013 at 12:15 AM PST

In my last blog post, I explained how to add versioning capability to templates. Being able to create, view and restore different versions of templates is great, but sometimes it's not so easy to see what has actually changed. And we might not be as lucky as having comments about the change from the author, or they might be cryptic to the point of being useless.

So let's try and add the diff and patch options to what we created last time. The version list is already capable of generating and displaying diffs. We just need to turn it on and tell the code producing the diff what actually matters when comparing different versions of templates, and what to do with the output. To keep it simple we just instruct the diff servlet to spit out plain text of the template and use the javascript library to produce diff and patch.

The library used here was google-diff-match-patch and as you can see it produces a visual diff of the changed template as well as a patch in unidiff format.

Some extra configuration is also made available for the user to tweak the output of the diff in case the default output is not to their liking. Admittedly I was not very inventive when it came to redefining the description of those options, then again that was not the point of the exercise.

The library is also capable of applying the patch on the source, but this functionality was not explored in the integration.

You can find the code changes necessary for feeding the JS library with two versions of the template in these two tickets: MGNLINTEMPL-26 and MGNLDIFF-25.

AttachmentSize
blog-diff-diff.png52.38 KB
blog-diff-options.png50.1 KB
blog-diff-patch.png121.38 KB