Skip to content

Instantly share code, notes, and snippets.

@bradkrane
Last active April 27, 2024 01:46
Show Gist options
  • Save bradkrane/7f7f7d1460821d489d4f2179a6502bd7 to your computer and use it in GitHub Desktop.
Save bradkrane/7f7f7d1460821d489d4f2179a6502bd7 to your computer and use it in GitHub Desktop.
Git diff for LibreOffice ODT files for Windows Setup

Batch Script and Git Setup for text diff of ODT files with LibreOffice

git Windows will diff docx files without any additional configuration but surprisingly, this is not the case for ODT files. This simple Window shell script and git attributes configuration allows for diffing ODT files with LibreOffice

Windows Shell Script

Copy the script below locally, I use %USERPROFILE%\scripts as the reference destination in the Configure Git section below, but you can choose whatever you want replacing the path with your own throughout the document.

Waring: this script assumes that the there is no .txt file that matches the filename less extension of the ODT file being diffed. This is a temporary work around for the --cat option of soffice.exe not working as advertized. Bug 129713

odt2txt.cmd

@ECHO OFF
"%ProgramFiles%\LibreOffice\program\soffice.exe" --headless --convert-to txt:Text "%~f1"
type "%~n1.txt"
del "%~n1.txt"

Once the --cat bug is fixed:

odt2txt.cmd

@ECHO OFF
"%ProgramFiles%\LibreOffice\program\soffice.com" --cat "%~f1"

Configure Git

Adapted from https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

Create the file below in your git working dir. You'll need to do this for each repo,

.gitattributes

*.odt diff=opendoc

or you can make a globat .gitattribues file %USERPROFILE%\\.gitattributes and configure git to reference it by

git config --global core.attributesfile %USERPROFILE%\\.gitattributes

Finally tell git to use the shell script for opendoc type diffs with the one time configuration command below:

git config diff.opendoc.textconv C:\\Users\\Brad\\scripts\\odt2txt.cmd

The %USERPROFILE% variable expansion includes single backslashes when expanded but the command above requires escaped back slashes.

Results

You should now see the following output for ODT file diffs:

diff --git a/testing.odt b/testing.odt
index f50b554..9bbd588 100644
--- a/testing.odt
+++ b/testing.odt
@@ -1,4 +1,5 @@
 This is a ODT file diff test.

-ODT files were once treated as binary blobs.
+ODT files were once treated as binary blobs but then there was a text diff!

The text diffs are retroactive as git diffs are generated and not stored.

Happy diffing!

@ryumada
Copy link

ryumada commented Jan 11, 2023

I'm wondering why I can't do this:

git config diff.opendoc.textconv %USERPROFILE%\\scripts\\odt2txt.cmd

@bradkrane
Copy link
Author

Using %USERPROFILE% wont work due to the single non-escaped slash characters that this variable expands to. Try replacing %USERPROFILE% with result of echo %USERPROFILE% and escape the slashes it contains which should do it.

@MaxMyzer
Copy link

Note that if your user name has a space in it (My Name), you will need to do the following for the config path:
"C:\\Users\\My\ Name\\scripts\\odt2txt.cmd"
(ie, you will need to escape the space)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment