Viewing git filters output when using meld as a di

2019-02-10 14:22发布

问题:

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:

  • by calling git difftool
  • by calling meld from a custom script

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?

回答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