如何使用gitattribute和git config忽略同一文件类型中的多行代码?

huangapple go评论101阅读模式
英文:

How to ignore multiple lines of code in the same file type using gitattribute and git config?

问题

以下是您要翻译的内容:

作为对这个帖子的扩展:Git是否可以忽略特定行?,我试图忽略同一文件类型中的多行。

是否有以下方式:

  • 为相同的文件或文件类型在同一个.gitattributes筛选器上设置多个筛选器?(如下所示)或
*.rs filter=filterA
*.rs filter=filterB

// 类似于
*.rs filter=filterA + filterB
  • .git/config中为同一个筛选器添加多个规则?(如下所示)
[filter "filterA"]
	clean = sed "/abc/d"
[filter "filterB"]
	clean = sed "/def/d"

// 类似于
[filter "filterC"]
	clean = sed "/abc/d"
	clean = sed "/def/d"

上述内容导致.gitattributes中的最后一个筛选器覆盖了从第一个筛选器进行的更改。我还尝试过类似于clean = sed "/abc*def/d",但这并没有起作用。

英文:

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.

答案1

得分: 1

如果您想忽略整行,我的建议是使用grep -v而不是sed //d。链接问题中的答案使用了sed,因为它只隐藏了行的部分,这正是sed适合的任务。

所以首先让我们重写您的两个简单过滤器:

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

请注意,如果您的占位符abcdef包含正则表达式元字符,您需要使用反斜杠进行转义。例如,\* - grep会将其解释为字面上的星号,而*是一个元字符。请参阅man grep

现在来处理更复杂的过滤器:将这两个表达式组合成一个复杂的正则表达式:

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

\|用于在基本(默认)grep语法中分隔正则表达式的分支。在扩展的正则表达式中,语法如下:

[filter "filterC"]
    clean = grep -Ev "abc|def"
英文:

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"

huangapple
  • 本文由 发表于 2023年6月29日 14:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定