Skip to content

Instantly share code, notes, and snippets.

@c-mauderer
Forked from bradkrane/odt2txt.cmd.md
Created January 17, 2023 15:47
Show Gist options
  • Save c-mauderer/577b353e43980d1e83d5b28edccbdbfd to your computer and use it in GitHub Desktop.
Save c-mauderer/577b353e43980d1e83d5b28edccbdbfd 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!

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