Josh Haas's Web Log

Archive for June, 2014

How to merge json files using git

without comments

For my startup bubble, I do a lot of work using json. I have a number of very large, frequently-changing json files checked into git.

This causes a fair amount of pain, because git’s merge algorithm doesn’t know how to interpret or preserve the json tree structure, so it reports conflicts for things such as adding different keys to the same parent object, which textually can look like a conflict but semantically isn’t, at least for my purposes.

It got to the point where I’d be coordinating with my teammate to make sure we didn’t touch the same json files at the same time. Since the big win with git is being able to skip that kind of coordination, I got fed up and wrote a custom driver for merging json files.

It requires coffee-script to use (although porting it to javascript should be as simple as dumping it into coffeescript.org’s converter). Installation instructions are in comments at the top of the file.

The way it works is it recursively walks the structure of the file we’re merging into ours, and checks that each value matches the value of our file. If the value differs, it looks at the ancestor file (the version from before the two branches diverged) to see whether our version or their version was changed… if our version matches the ancestor file, then it goes with their version, and visa-versa.

If neither our version nor their version matches the ancestor, it reports that as a conflict. It reports a conflict by replacing the node with an object that looks like this:

{
   "CONFLICT": "<<<<<<<<>>>>>>>>",     //this is to make conflicts easy to find
   "OURS": ...our version of the node...
   "THEIRS": ...their version of the node...
   "ANCESTOR": ...the ancestor version....
   "PATH": a .-seperated list of keys, indicating where in the file we are
}

Resolving the conflict is as simple as copying our version or their version and pasting it over the conflict object.

The final, merged version of the file is then pretty-printed (it should be pretty easy to modify the code if pretty-printing isn’t what you want). And you’re all set!

Written by jphaas

June 27th, 2014 at 2:44 am

Posted in Uncategorized

The revenge of the sheeple

without comments

Sheeeple, Sheeeeeeeeeeeple, wake up!!!!!!!!!!!!!!!!

But seriously, is the protestor right? Are we all sheep?

Yes, and maybe no.

A sheep is a tamed creature. It follows behavior patterns that its shepherd designed for it. Those patterns serve the shepherd’s interests; they only serve the sheep’s interests insofar as those interests align with the shepherd’s. Once those interests diverge… it’s time for some lamb chop. Read the rest of this entry »

Written by jphaas

June 3rd, 2014 at 4:02 pm

Posted in Uncategorized