Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git clean-filter python script on windows

I'm trying to add a git clean-filter in order to ignore outputs and execution_count from my IPython notebook files.

I've basically followed 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).

I want to bundle that to my repository so that other contributors get it too. I've saved the ipynb_drop_output.py at the root of my repository and saved the gitconfig and gitattributes files at the same place so at the root I have:

.gitignore
.gitconfig
.gitattributes
ipynb_drop_output.py
MyNotebook.ipynb

In .gitattributes:

*.ipynb filter=clean_ipynb

In .gitconfig:

[filter "clean_ipynb"]
    clean = ipynb_drop_output.py
    smudge = cat

I've tested the code of my ipynb_drop_output manually and it works like a charm. Yet git diff still shows me execution_count and outputs that changed. It appears the script isn't running at all.

I'm thinking it might be because of the clean = ipynb_drop_output.py section, but I've tried every variation: not include the .py, include the full path "C...\ipynb_drop_output.py", with forward slashes too etc.

My second theory is that git is just not looking at the .gitconfig file but I'm unclear how to tell it to and/or how to check that it is actually looking at it. And I thought the point of git config --file .gitconfig filter.clean_ipynb.clean ipynb_drop_output was to do just this...

How can I make it work on Windows please?

like image 328
Julien Marrec Avatar asked Oct 31 '22 00:10

Julien Marrec


1 Answers

Let's assume that you have your repository checked out under: ~/myrepo/.

You need to tell git where to find your repo-wide custom .gitconfig, that you want all your users to use. You do that by running:

cd ~/myrepo/
git config --local include.path ../.gitconfig

note ../, which is missing from your attempts to make this work, as .gitconfig and .git/config are not in the same directory. The layout of your ~/myrepo/ will have:

.git/config
.gitconfig
.gitattributes

You will need the last 2 files committed to your repo.

All your users will have to execute the git config command from above right after cloning your repo, to tell git to trust ~/myrepo/.gitconfig. It's not possible to do it on their behalf for security reasons.

Finally, the reason your manual incorrect configuration was silently failing, is because git has been designed this way, to allow for optional configuration files. So as of this writing if in your .git/config you have:

[include]
    path = ../.gitconfig

and ../.gitconfig is not there, git will silently skip over it. Therefore, if you typed a wrong path, it will skip over it.

There is a new development on this front, and the jury is still out. Hopefully there will be a way to diagnose such git issues in the future.

like image 164
stason Avatar answered Nov 15 '22 05:11

stason