0

I have a git repository with the following contents in the repository's root directory:

 .git
 .gitattributes
 .gitconfig
 gitfilters.py
 MyFile.txt

I want to add a filter that overrides any changes made to certain lines in MyFile.txt whose contents look like this:

parameter1 = value1
parameter2 = value2
parameter3 = value3
parameter4 = value4
parameter5 = value5

I want the lines with parameter1 and parameter2 to not reflect on git when changed in the working directory.

I added my filter in the .gitattributes file in order to achieve this:

MyFile.txt filter=MyFilter

I defined MyFilter in my .gitconfig file as below:

[filter "MyFilter"]
    clean = python ../gitfilters.py
    smudge = cat

The gitfilters.py script to replace the lines I don't want to change:

import sys
OVERRIDE_PARAMS = {'parameter1': 'value1','parameter2': 'value2'}

for line in sys.stdin:
    for param, value in OVERRIDE_PARAMS.items():
        if param in line:
            line = f'{param} = {value}\n'
    sys.stdout.write(line)

exit() 

I then included the .gitconfig in my .git/CONFIG file:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[include]
    path = ../.gitconfig

With all these changes I expect that if I change MyFile.txt as below, my git staging area would still be clean

parameter1 = value1NEW
parameter2 = value2NEW
parameter3 = value3
parameter4 = value4
parameter5 = value5

However, that's not the case and I still see the changes for the 2 lines in my git.

I suspect some of the paths are not correct and the filters are not run correctly. Can someone please point one what I'm missing here? Thanks!

locke14
  • 1,335
  • 3
  • 15
  • 36
  • 1
    Your setup works perfectly for me. To debug the include, you can use `git config --list`, which should then show your "MyFilter" config. To debug the commands git actually runs you can use the GIT_TRACE environment variable via `export GIT_TRACE=1`. You should then see all commands, including the filter ones, that git actually runs. An interesting thing to notice is that once your filter has run, it will not be re-run unless the file or the filter changes. You could try `touch MyFile.txt` to make sure that the filter will run. – mmlr Apr 09 '20 at 20:53
  • Before, I was using gitkraken and it showed the lines which were not supposed to change. But from the command line it seems to work fine (git diff doesn't show the difference). Any idea on how to make the filters work with gitkraken? – locke14 Apr 10 '20 at 16:02
  • 1
    I have no personal experience with GitKraken, but from some quick searches on the web it seems like it uses an internal git implementation that doesn't properly support git configs and filters. It supports LFS only [by installing real git](https://support.gitkraken.com/git-workflows-and-extensions/intro-and-requirements/#git-lfs-requirements) and "branching out for it", so it sounds like it indeed is custom and doesn't support generic git filters but has special handling to allow LFS. Maybe ask their support? – mmlr Apr 10 '20 at 21:34

0 Answers0