Thursday, May 10, 2012

A git diff driver for core data model files

Whenever you make a bunch of changes to core data model (xcdatamodel) files, its impossible to know with git diff, what exactly changed. Usually, the output looks like:

diff --git a/Recipes.xcdatamodel/elements b/Recipes.xcdatamodel/elements
index 35a20f3..939bc61 100644
Binary files a/Recipes.xcdatamodel/elements and b/Recipes.xcdatamodel/elements differ
diff --git a/Recipes.xcdatamodel/layout b/Recipes.xcdatamodel/layout
index 052f0e8..18906a4 100644
Binary files a/Recipes.xcdatamodel/layout and b/Recipes.xcdatamodel/layout differ

Git, however, provides a way to diff binary files as if they were text. Using a diff driver and a binary to text converter[1], we can control git's diff output so that we get this:

diff --git a/Recipes.xcdatamodel/elements b/Recipes.xcdatamodel/elements
index 35a20f3..939bc61 100644
--- a/Recipes.xcdatamodel/elements
+++ b/Recipes.xcdatamodel/elements
@@ -2,8 +2,8 @@ Entity: Image (NSManagedObject)
   Att: image                     Transformable                                            O   dafQGIhBFATAxE1hyQd5z5cpRg0OLP4+M+W3+xhA6jU=
   Rel: recipe                    Recipe          image                            Nullify   I fVH2lmmkHE4j/FvzfJ2et3KsNxcA8p5BJp2d/xd4hH0=
 
-Entity: Ingredient (Ingredient)                                                               5tCLlA1r9p1UWdOTr3XJ+ZzBr6bxraWAs3Vt9zCAZek=
-  Att: amount                    String                                                   O   b9/jjR2iJtm4oldVJwj25X+/hpEL6/1CM5hLhgV48Iw=
+Entity: Ingredient (Ingredient)                                                               7nbTTDaWG1lAddHYHum4xP4IA/N90NYv9GLkGK+sueQ=
+  Att: amount                    Integer16                                                O   RmH9Sk61kxsb8+GUEqlEkIuV4tDjxxhMNsHUu/tBW6I=
   Att: displayOrder              Integer16                                                    kMPJ+qU+fnBipO5Ajep+KT3rKB9zeOPrav0q4rMSt7k=
   Att: name                      String                                                       jLmWXAAxrGiROYTzEQlBrZZTlb6f2bF9575UvHrUaJA=
   Rel: recipe                    Recipe          ingredients                      Nullify O I GvmjTsOh76OGkr0Lmnxdh8u6FO4E+iuEYa0mRZPuKJQ=
diff --git a/Recipes.xcdatamodel/layout b/Recipes.xcdatamodel/layout
index 052f0e8..18906a4 100644
Binary files a/Recipes.xcdatamodel/layout and b/Recipes.xcdatamodel/layout differ

The great thing is that, this works not only for git diff, but any git command which displays diff output e.g. git log -p, git show, etc.

To do this for your own iOS or Mac OS X projects, try XCDataModelPrinter -- a small little project I wrote. The README should help you get started. If you have any questions, drop me a message at mail at chaitanyagupta dot com.

1. This technique is explained eloquently in the book Pro Git

Labels: ,



This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]