4

I set up some git filters in order to preprocess certain files (in my case IPython Notebooks) before committing them. To be more exact I'm following these instructions: https://stackoverflow.com/a/20844506/578770

This works fine and the files are correctly filtered if I commit my changes or if I review the change using the command line "git diff ".

However, if I'm reviewing my changes using meld, the files are not filtered. I tried several way to set up meld as a diff tool for git:

But none of the solutions I've found for using meld as a diff tool enables me to view the change of the file after the git filters is applied.

Anybody has an idea on how to achieve this?

Community
  • 1
  • 1
F-A
  • 643
  • 3
  • 10

1 Answers1

1

Here's a hack solution for this problem. The original git filter you are referring to has been formalized as the package nbstripout (pip3 install nbstripout), but you could put any filter into this script and it would work the same. I'll assume you want to configure this for the user rather than a particular repo.

In ~/.gitconfig, add a new diff driver named git-nb-clean-diff:

[diff "git-nb-clean-diff"]
    command = git-nb-clean-diff

In ~/.config/git/attributes, configure notebooks to be diffed with that diff driver:

*.ipynb diff=git-nb-clean-diff

Now we need to make the actual diff driver! In ~/bin/git-nb-clean-diff (must have this filename but the location is optional):

#!/bin/bash
# pass the stripped working tree file and the repo copy 
# to meld for diffing
meld <(cat $1 | nbstripout) $2

Lastly, we make this file executable

chmod +x ~/bin/git-nb-clean-diff

and add it to the path so git can find our diff driver when it runs

echo "PATH=$PATH:~/bin" >> ~/.bashrc
# reload the edited .bashrc
source ~/.bashrc
user126350
  • 505
  • 5
  • 11
  • Thank for your help, using a git-driver was the solution! Concerning the notebook filter, I started using the nbdime packages (http://nbdime.readthedocs.io) instead of nbstripout. It provides a browser interface for diffing the notebook with the pictures. – F-A May 11 '17 at 08:27
  • You're welcome! Are you using nbdime's output filtering as well, or still storing the outputs? – user126350 May 11 '17 at 13:45
  • I'm storing/commiting the outputs now. This way I can still see the same plots on all my devices but I do not have ugly diff. – F-A May 11 '17 at 15:11
  • Hi, before opening another question, I would like to know if this procedure is still working with the latest nbstripout and meld. I can't make it working, but I am also keeping some default configurations of nbstripout like `[filter "nbstripout"] clean = \"/home/me/anaconda3/bin/python3.8\" -m nbstripout smudge = cat [diff "ipynb"] textconv = \"/home/me/anaconda3/bin/python3.8\" -m nbstripout -t` and an attribute file `*.ipynb filter=nbstripout *.ipynb diff=ipynb`. – giammi56 Jun 08 '21 at 11:42