Subversion

svn diff for png (and other binary) files

Bruno, in Optimisation de PNG, ou encore une bonne raison de préférer git, argues that git is better because you can use gitattributes to specify a program which will convert binary files to text before diffing:

$ echo '*.png diff=exif' >> .gitattributes
$ git config diff.exif.textconv exiftool

This trick had already been reported by Scott, and is included in Pro Git in chapter 7.2.

We’ve seen in the last post how to have a Color-highlighted svn diff output with git default colors, now we’ll see how to svn diff png files.

This is what you’ll get if you try a svn diff on a png file:

$ svn diff preview.png
Index: preview.png
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

And if you carefully read the Subversion FAQ, in the section named How does Subversion handle binary files? you’ll see that for files it has determined as binary (read the FAQ to know how it does), Subversion will:

Not show the differences as part of svn diff

And if you try:

$ svn diff preview.png
Index: preview.png
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

You can see that it will not even try to diff binary files. But you can use the –force option to do it anyway:

$ svn diff --force preview.png
Index: preview.png
===================================================================
Binary files preview.png	(revision 14882) and preview.png	(working copy) differ
svn: 'colordiff' returned 2

But diff (with the colordiff wrapper from the previous post) only tells us whether the two binary files differ or not.

Now here’s the trick adapted from the posts concerning git.

First install ExifTool. Under Debian:

$ sudo aptitude install libimage-exiftool-perl

Then create a wrapper:

$ sudoedit /usr/local/bin/svndiff
 
#!/bin/sh
 
DIFF=`which colordiff`
 
if `file ${6} | grep -q PNG`; then
	LEFT=`mktemp`
	RIGHT=`mktemp`
 
	exiftool ${6} > $LEFT
	exiftool ${7} > $RIGHT
 
	$DIFF -u -L "${3}" -L "${5}" "$LEFT" "$RIGHT" | grep -E -v "ExifTool Version Number|[+-]File Name|[+-]Directory|[+-]File Permissions"
 
	rm ${LEFT}
	rm ${RIGHT}
else
	$DIFF -u -L "${3}" -L "${5}" "${6}" "${7}"
fi

Make it executable:

$ sudo chmod +x /usr/local/bin/svndiff

And finally edit the Subversion configuration file in order to use this new wrapper:

$ vim ~/.subversion/config
 
[helpers]
diff-cmd = svndiff

And now that’s what you’ll get:

$ svn diff --force preview.png
Index: preview.png
===================================================================
--- preview.png	(revision 14882)
+++ preview.png	(working copy)
@@ -1,22 +1,22 @@
-File Size                       : 7.1 kB
-File Modification Date/Time     : 2010:02:02 16:15:46+01:00
+File Size                       : 5.5 kB
+File Modification Date/Time     : 2010:11:16 17:57:50+01:00
 File Type                       : PNG
 MIME Type                       : image/png
 Image Width                     : 150
 Image Height                    : 120
 Bit Depth                       : 8
-Color Type                      : RGB with Alpha
+Color Type                      : RGB
 Compression                     : Deflate/Inflate
 Filter                          : Adaptive
 Interlace                       : Noninterlaced
 SRGB Rendering                  : Perceptual
-Background Color                : 255 255 255
 Pixels Per Unit X               : 2835
 Pixels Per Unit Y               : 2835
 Pixel Units                     : Meters
 Modify Date                     : 2009:04:17 08:37:21
+Background Color                : 255 255 255
 Image Size                      : 150x120

And a screenshot to see the color-highlighted output:

Color-highlighted svn diff of a png file

5 Comments

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

*Required Fields