在 jupyter 笔记本上使用 nbstripout 制作 TortoiseGit 运行 git diff.textconv 过滤器

Make TortoiseGit run git diff.textconv filter with nbstripout on jupyter notebooks

我已将 git 配置为在提交和差异之前使用 nbstripout 删除 jupyter notebook 输出。它在 git bash shell(在 Windows 中)工作得很好。 现在我想在 TortoiseGit 中拥有相同的功能。 输出在提交时被正确剥离,但是 运行 从资源管理器上下文菜单中使用 TortoiseGit 内部差异工具不会触发 git diff.textconv 在 .git/config 中配置的过滤器nbstripout:

[diff "ipynb"]
    textconv = \"D:/Applications/Anaconda3/python.exe\" \"D:/Applications/Anaconda3/lib/site-packages/nbstripout.py\" -t

有什么方法可以在 diff 之前自动 运行 nbstripout?

我目前的解决方法是在 运行ning diff 之前手动删除笔记本输出。

从 2.7.0 开始,TortoiseGit 不支持 diff.textconv 过滤器。在 TortoiseGit 中使用高级差异设置:https://tortoisegit.org/docs/tortoisegit/tgit-dug-settings.html#tgit-dug-settings-Progs-Adv

以下批处理脚本在 .ipynb 文件的高级差异查看器设置下配置为 C:\path\to\script.bat %base %mine 时执行此作业。它首先运行 nbstripout 并在 TortoiseGit diff 查看器中与 %base 版本一起打开剥离文件。

:: Run nbstripout on a jupyter notebook before opening it with TortoiseGit
:: Call Signature: nbstripout_TortoiseGit.bat %base %mine

:: Parameters and options
@echo off
set "python_exe=C:\path\to\python.exe"
set "nbstripout=C:\path\to\nbstripout.py"
set "tmpfilename=%temp%\nbstripout_tempfile_%USERNAME%_%RANDOM%.ipynb"
set "diffViwer=TortoiseGitMerge.exe"

:: Get cmdline arguments
set base=%1
set mine=%2

:: Strip output from notebook
%python_exe% %nbstripout% -t %mine% > %tmpfilename%

:: Open file in diff viewer
%diffViwer% %base% %tmpfilename%

:: Remove tmpfilename
del %tmpfilename%