0

I have a file in my repo which is essentially a compressed XML file.

I've worked out how to specify the method of diffing so that the diffs between two versions are human readable, by adding a line to .gitattributes.

I was wondering if there was a similar way to decompress the file before merging, so that it could be merged as if it were an XML file, rather than a binary file.

Many thanks

FoxGlove
  • 81
  • 4

1 Answers1

2

You can easily write a custom merge driver that decompresses the inputs first. However:

... so that it could be merged as if it were an XML file

It's extremely difficult (read: impossible) to correctly merge XML with the tools provided with Git. See Can git be made to mostly auto-merge XML order-insensitive files? for details. In other words, once you have decompressed the three input files, there is no one correct action to take to merge the XML.

You do say as if it were, so perhaps a plain-text merge will work for your non-XML-but-XML-ish text. In this case, see git merge-file, which will do a three-way merge on user-supplied text files. If the merge goes well you can re-compress the result, remove the decompressed intermediate files, and call the result sufficient. If not, you probably should leave the original three de-compressed files for the user to manually merge. (This part is of course up to you.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for the response! I should have been more clear in that, it _is_ XML so I fear that I will succumb to your first point. – FoxGlove May 28 '17 at 16:39
  • 1
    Another option here is to decompress, run `git merge-file`, and then always fail (leaving the three inputs handy as well). That's probably not ideal, but might be nicer than just treating the file as binary (which also just fails). – torek May 28 '17 at 16:42
  • Excuse me if I'm being naive, but would it be possible to losslessly convert the XML to JSON, merge, and then convert back? – FoxGlove May 28 '17 at 17:56
  • 1
    @FoxGlove: JSON has some of the same merge issues (though a substantially less-fragile *syntax*, which I suspect will help a lot—XML lets you break tags across lines, e.g., ``, which is bad for Git and Mercurial line-oriented merge). It's probably worth trying if your case fits well. Not all XML will encode straightforwardly in JSON though. Consider, e.g., encoding `contents` and `contents`. – torek May 28 '17 at 20:07