Git windows 上的清理过滤器 python 脚本
Git clean-filter python script on windows
我正在尝试添加一个 git 清理过滤器以忽略我的 IPython 笔记本文件中的输出和 execution_count。
基本都关注了this article (based on this SO answer) and modified it slightly for my needs. Also, I'm on Windows so the part about making the python script executable isn't needed as far as I know (see Python FAQ).
我想将它捆绑到我的存储库中,以便其他贡献者也能得到它。
我已将 ipynb_drop_output.py
保存在存储库的根目录下,并将 gitconfig 和 gitattributes 文件保存在同一位置,因此在根目录下我有:
.gitignore
.gitconfig
.gitattributes
ipynb_drop_output.py
MyNotebook.ipynb
在.gitattributes
中:
*.ipynb filter=clean_ipynb
在.gitconfig
中:
[filter "clean_ipynb"]
clean = ipynb_drop_output.py
smudge = cat
我已经手动测试了我的 ipynb_drop_output 的代码,它运行得非常好。然而 git diff
仍然显示 execution_count 并且输出发生了变化。看起来脚本根本不是 运行。
我认为这可能是因为 clean = ipynb_drop_output.py
部分,但我已经尝试了所有变体:不包括 .py,包括完整路径 "C...\ipynb_drop_output.py",也带有正斜杠等等
我的第二个理论是 git 只是没有查看 .gitconfig 文件,但我不清楚如何告诉它 and/or 如何检查它是实际上看着它。我认为 git config --file .gitconfig filter.clean_ipynb.clean ipynb_drop_output
的意义在于做到这一点...
请问如何让它在 Windows 上运行?
假设您已在以下位置检出存储库:~/myrepo/
。
您需要告诉 git 在哪里可以找到您希望所有用户使用的 repo-wide 自定义 .gitconfig
。你这样做 运行:
cd ~/myrepo/
git config --local include.path ../.gitconfig
请注意 ../
,因为 .gitconfig
和 .git/config
不在同一个目录中,所以您尝试使这项工作丢失。您的 ~/myrepo/
的布局将具有:
.git/config
.gitconfig
.gitattributes
您需要提交到您的存储库的最后 2 个文件。
您的所有用户都必须在克隆您的存储库后立即从上方执行 git config
命令,以告知 git 信任 ~/myrepo/.gitconfig
。出于安全原因,无法代表他们执行此操作。
最后,您的手动不正确配置悄无声息地失败的原因是因为 git 以这种方式设计,以允许可选的配置文件。因此,在撰写本文时,如果您的 .git/config
中有:
[include]
path = ../.gitconfig
和../.gitconfig
不存在,git会默默跳过。因此,如果您输入了错误的路径,它将跳过它。
这方面有新进展,the jury is still out。希望将来会有一种方法来诊断此类 git 问题。
我正在尝试添加一个 git 清理过滤器以忽略我的 IPython 笔记本文件中的输出和 execution_count。
基本都关注了this article (based on this SO answer) and modified it slightly for my needs. Also, I'm on Windows so the part about making the python script executable isn't needed as far as I know (see Python FAQ).
我想将它捆绑到我的存储库中,以便其他贡献者也能得到它。
我已将 ipynb_drop_output.py
保存在存储库的根目录下,并将 gitconfig 和 gitattributes 文件保存在同一位置,因此在根目录下我有:
.gitignore
.gitconfig
.gitattributes
ipynb_drop_output.py
MyNotebook.ipynb
在.gitattributes
中:
*.ipynb filter=clean_ipynb
在.gitconfig
中:
[filter "clean_ipynb"]
clean = ipynb_drop_output.py
smudge = cat
我已经手动测试了我的 ipynb_drop_output 的代码,它运行得非常好。然而 git diff
仍然显示 execution_count 并且输出发生了变化。看起来脚本根本不是 运行。
我认为这可能是因为 clean = ipynb_drop_output.py
部分,但我已经尝试了所有变体:不包括 .py,包括完整路径 "C...\ipynb_drop_output.py",也带有正斜杠等等
我的第二个理论是 git 只是没有查看 .gitconfig 文件,但我不清楚如何告诉它 and/or 如何检查它是实际上看着它。我认为 git config --file .gitconfig filter.clean_ipynb.clean ipynb_drop_output
的意义在于做到这一点...
请问如何让它在 Windows 上运行?
假设您已在以下位置检出存储库:~/myrepo/
。
您需要告诉 git 在哪里可以找到您希望所有用户使用的 repo-wide 自定义 .gitconfig
。你这样做 运行:
cd ~/myrepo/
git config --local include.path ../.gitconfig
请注意 ../
,因为 .gitconfig
和 .git/config
不在同一个目录中,所以您尝试使这项工作丢失。您的 ~/myrepo/
的布局将具有:
.git/config
.gitconfig
.gitattributes
您需要提交到您的存储库的最后 2 个文件。
您的所有用户都必须在克隆您的存储库后立即从上方执行 git config
命令,以告知 git 信任 ~/myrepo/.gitconfig
。出于安全原因,无法代表他们执行此操作。
最后,您的手动不正确配置悄无声息地失败的原因是因为 git 以这种方式设计,以允许可选的配置文件。因此,在撰写本文时,如果您的 .git/config
中有:
[include]
path = ../.gitconfig
和../.gitconfig
不存在,git会默默跳过。因此,如果您输入了错误的路径,它将跳过它。
这方面有新进展,the jury is still out。希望将来会有一种方法来诊断此类 git 问题。