如何获得有用的 git Libre Office Writer 保存的文件差异,并在命令行中输出?

How can I get useful git diff of files saved by Libre Office Writer, with output in the command line?

默认 .odt 文件的 git diff 默认版本未显示已更改的内容

Binary files i/filename.odt and w/filename.odt differ

有没有办法显示真正更改的内容并保持文件可由 Libre Office 直接编辑?

不要在 git 中存储 odt 文件。您可以 unzip 它们并存储内容而不是 XML。您可能需要按原样向 XML 文件添加换行符,IIRC,只是 XML 一行。

注意,理想情况下应该避免对二进制文件进行版本控制, 当他们比较、整合和解决冲突时 more difficult.


在git中可以配置一个diff driver具体到每个office文件来 在比较它们之前将它们转换为纯文本表示形式。

以下是一些可以使用的工具示例:

  • catdoc(用于 Word)
  • catppt(用于幻灯片)
  • odt2txt(对于 Writer)
  • xls2csv(对于 Excel)

首先,每个office文件的文件类型可以在全局配置 $HOME/.config/git/attributes 文件:

*.doc binary diff=doc
*.odt binary diff=odt
*.ppt binary diff=ppt
*.xls binary diff=xls

然后,为每个 文件全局配置 diff 驱动程序 类型:

git config --global diff.doc.textconv catdoc
git config --global diff.odt.textconv odt2txt
git config --global diff.ppt.textconv catppt
git config --global diff.xls.textconv xls2csv

来源:https://medium.com/@mbrehin/git-advanced-diff-odt-pdf-doc-xls-ppt-25afbf4f1105

对于基础知识,要区分任何压缩-xml 格式的文本,您可以使用 xmllint 格式化 xml 并区分它们,假设您已经完成

git show master:summary.odt >${file1=`mktemp`}
git show feature:summary.odt >${file2=`mktemp`}
7z x -o ${extract1=`mktemp -d`} $file1
7z x -o ${extract2=`mktemp -d`} $file2
find $extract1 $extract2 -iname \*.xml -execdir xmllint --format {} -o {}.pretty \;

您现在可以比较 .pretty 以查看发生了什么变化。用通常的脚手架打包它,你就得到了一个基本的 diff 驱动程序。您甚至可以将 xml 替换为美化后的 xml,对其进行编辑,重新打包,一切正常。

您也可以使用 Libreoffice 建议的平面 xml 格式。

.fodt 文件格式。请参阅 Libreoffice and version control or this answer,它提供了良好的 links。

来自link:

If a document is saved as .fodt file it keeps the same data the .odt file would contain. Only that this time the data is represented as human-readable text (which makes the work much easier for the version control system) and not compressed. So saving a document as flat xml makes it possible to keep server space requirements and network load low at the relatively low cost of wasting a few kilobytes on the local hard disks.

请注意,微小的变化通常仍会导致巨大的差异,因此它并不能完全解决问题。

我使用以下方法在 git 中管理 odt 和其他 MS 和 Libre Office 文件,并在提交之前“git 区分”它们。

安装“Libre Office 到文本”转换器:

$ sudo apt install unoconv catdoc
$ pip install python-pptx

https://gitlab.com/wolframroesler/snippets/-/blob/master/git-pptx-textconv.py 复制到您选择的位置并使其可执行。

将以下内容添加到 ~/.gitconfig

[diff "doc"]
    textconv=catdoc
[diff "odt"]
    textconv=odt2txt
[diff "odp"]
    textconv=odp2txt
[diff "ods"]
    textconv=ods2txt
[diff "ppt"]
    textconv=catppt
[diff "pptx"]
    textconv=/location/of/git-pptx-textconv.py

将以下内容添加到 ~/.config/git/attributes(或者,添加到项目根目录中的 .gitattributes 文件):

*.doc diff=doc
*.odp diff=odp
*.ods diff=ods
*.odt diff=odt
*.ppt diff=ppt
*.pptx diff=pptx

更多详情:https://gitlab.com/wolframroesler/snippets#manage-office-files-in-git