1

As an extension of this post: Can git ignore a specific line?, I am trying to ignore multiple lines in the same file type.

Is there a way to either;

  • set multiple filters to the same .gitattributes filter for the same file or file types? (like below) OR
*.rs filter=filterA
*.rs filter=filterB

 // to something like
*.rs filter=filterA + filterB
  • add multiple rules to the same filter in .git/config? (like below)
[filter "filterA"]
    clean = sed "/abc/d"
[filter "filterB"]
    clean = sed "/def/d"

 // to something like
[filter "filterC"]
    clean = sed "/abc/d"
    clean = sed "/def/d"

The above causes the last filter in .gitattributes to overwrite the changes made from the first filter. I have also attempted something like clean = sed "/abc*def/d", but this did not work.

phd
  • 82,685
  • 13
  • 120
  • 165
kenta_desu
  • 303
  • 1
  • 9

1 Answers1

1

If you want to ignore entire lines my advice is to use grep -v instead of sed //d. The answers at the linked question use sed because they hide only parts of lines, a task for which sed is exactly suitable.

So 1st lets rewrite your two simple filters:

[filter "filterA"]
    clean = grep -v "abc"
[filter "filterB"]
    clean = grep -v "def"

Please be warned that if your placeholders abc and def contain regular expression metacharacters you need to escape them using backslash. Like \*grep interprets this as a literal asterisk while * is a metacharacter. See man grep.

Now to the more complex filter: combine the two expressions into one complex regular expression:

[filter "filterC"]
    clean = grep -v "abc\|def"

\| separates branches in regular expressions in basic (default) grep syntax. In case of extended RE the syntax is

[filter "filterC"]
    clean = grep -Ev "abc|def"
phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    thank you, super detailed and helpful! I liked your filterC examples, but couldn't seem to get it working. So I changed it to this `clean = "grep -v 'abc' | grep -v 'def'"` to get it done. – kenta_desu Jun 30 '23 at 00:54