Diff for .maxpat?

Peter Castine's icon

Has anyone ever built a diff specifically for .maxpat files?

Yes, I know you can use any standard diff for text files on the Max JSON format, but the problem is that very minor changes in the Max patch can result in a re-ordering of the objects in the .maxpat that will completely confuse a conventional diff. I have in front of me a large patch where I've made very few changes, but a text diff lists 9 pages of modifications.

What's needed is something that can parse enough of the JSON format to recognize it as a list of "objects" with id numbers and then compare the properties of identical ids, regardless of the order they occur in either file.

Ideally it would be able to differentiate between deletions/insertions ("major" changes) and things like moving an object (more minor, but of course with the potential for effecting left-to-right operation) and cosmetic issues (color). And so on.

Anyone old enough to remember "resource forks" and ResDiff will recognize what I'm looking for.

Sometimes minor changes to a patch can have catastrophic effects on how it works, and a tool like this would help in tracking down this kind of problem.

Any takers?

Roman Thilenius's icon

convert to max4, save as .txt, ... ?
:D :D
sorry.

Andrew Pask's icon

;max sortpatcherdictonsave 1

is probably going to be your friend.

Cheers

-A

Peter Castine's icon

Thanks, Andrew! sortpatcherdictonsave reduces the diff list by almost 80%. This is a big help.

There are still lots of spurious "differences" that aren't really differences, though. The order of boxes, for instance, is still arbitrary. So in one version you might have a comment box with id obj-71 at the beginning of the file, and in the next version the same object can be at the end. And because diff works on a line-by-line basis it ends up matching lines with braces while flagging differences on lines with text. All of this makes it harder to work out where there's a real change to the patch.

Seems sortpatcherdictonsave is the best we've got and it will be my pal. A more capable "MaxpatDiff", if there ever is one, would be my soulmate.

And anyone willing to take on programming a good diff for .maxpat-files will be a Max superhero!

sequadion's icon

I do have some experience in developing semantic comparison tools, so I might have what it takes to become a Max superhero. :)

Semantic comparison tools parse and interpret the contents of the files, as opposed to treating them as plain text. This enables the tool to detect the actual changes in meaning as opposed to changes in content. The basic version would list these changes in a meaningful, human-readable way, which is already useful by itself. However, there is no reason to stop there. Since the patches contain the graphical coordinates as well, the changes could be displayed and highlighted on a diagram as well. Again, I have done this before, so something like this would be right up my alley.

The big question is what programming language and runtime environment to use. Most of my experience is in Java, so that would probably be the easiest. However, I would happily use this as a learning opportunity to pick up a new language and/or application framework.
It should preferably:

  • work on Windows and Mac OS X

  • have no third-party dependencies that the user needs to install separately

Any thought, ideas, suggestions?

Peter Castine's icon

Java probably has a lot going for it.

JavaScript would have the possible advantage of being able to parse JSON with eval(). Although JavaScript is prototype-based, rather than class-based the way Java is, you can emulate a class hierarchy by using prototypes.

It's hard to say, offhand, which is the "better" choice, or how much a hierarchical class structure will help you. The tool you're most familiar with might be the one to go with.

Also note that, although JavaScript looks a lot like Java, there are some significant differences that will thoroughly trip up the unwary. If you decide to try JScript, spend some quality time with the language spec.

PS: It would be very cool if this works out!

thispatcher's icon

I'm also very interested in this, and am curious about where this ended up?
An alternative might be to try and build a maxpat diff driver for git:
https://github.com/git/git/blob/ab5d01a29eb7380ceab070f0807c2939849c44bc/userdiff.c#L15-L152

I'm afraid I don't quite understand at this point how to build such a thing, so am posting it here for now in case this piques another's interest!

Diemo Schwarz's icon

After a few attempts this finally works out! I could interface my old brute-force max differ with git. It is here: https://github.com/diemoschwarz/diff-for-max

Torsten's icon

thanks Diemo for sharing.
On running (diffmax -m A B) in a shell on Mac I get "json-sort.pl: command not found"
When commenting out line 51 to force diffmaxmunge.pl, 4 empty diffmax… files are written to /tmp. What's missing?

Diemo Schwarz's icon

right, that little script was missing, now pushed!

Torsten's icon

it works, thanks!