英文:
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"
请注意,如果您的占位符abc
和def
包含正则表达式元字符,您需要使用反斜杠进行转义。例如,\*
- 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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论