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!