在 git diff 中自定义标题

发布于 2024-09-13 02:23:40 字数 211 浏览 9 评论 0原文

当使用 git diff 查看差异时,每个更改都以如下行开头:

@@ -28,41 +20,10 @@ namespace ConsoleApplication1

对于 C,最后部分非常具有描述性 - 例如,它显示了发生更改的函数。但对于 C#,它只显示名称空间,这不是很有用。我认为这是因为它显示最后一行没有缩进。有没有办法如何配置这个?

When viewing a diff using git diff, each change starts with line like:

@@ -28,41 +20,10 @@ namespace ConsoleApplication1

For C, the final part is quite descriptive – it shows for example the function in which the change occurred. But for C#, it only ever shows the namespace, which isn't very useful. I think that's because it shows last line that isn't indented. Is there a way how to configure this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-09-20 02:23:40

我找到了它,该行称为 hunk header ,文档说明了如何自定义它:

定义自定义块头

文本差异输出中的每组更改(称为“块”)都以以下形式的行作为前缀:

<前><代码>@@ -k,l +n,m @@ 文本

这称为大块标头。 “TEXT”部分默认是以字母、下划线或美元符号开头的行;这与 GNU diff -p 输出使用的内容匹配。但这种默认选择并不适合某些内容,您可以使用自定义模式进行选择。

首先,在 .gitattributes 中,您将为路径分配 diff 属性。

<前><代码>*.tex diff=tex

然后,您将定义“diff.tex.xfuncname”配置来指定与您希望显示为块标头“TEXT”的行相匹配的正则表达式。在您的 $GIT_DIR/config 文件(或 $HOME/.gitconfig 文件)中添加一个部分,如下所示:

<前><代码>[diff“tex”]
xfuncname = "^(\\\\(sub)*section\\{.*)$"

注意。单层反斜杠会被配置文件解析器吃掉,因此您需要将反斜杠加倍;上面的模式选择以反斜杠开头的行,以及零次或多次出现的 sub,后跟节,后跟左大括号,直到行尾。

I have found it, the line is called hunk header and the documentation says how to customize it:

Defining a custom hunk-header

Each group of changes (called a "hunk") in the textual diff output is prefixed with a line of the form:

@@ -k,l +n,m @@ TEXT

This is called a hunk header. The "TEXT" portion is by default a line that begins with an alphabet, an underscore or a dollar sign; this matches what GNU diff -p output uses. This default selection however is not suited for some contents, and you can use a customized pattern to make a selection.

First, in .gitattributes, you would assign the diff attribute for paths.

*.tex   diff=tex

Then, you would define a "diff.tex.xfuncname" configuration to specify a regular expression that matches a line that you would want to appear as the hunk header "TEXT". Add a section to your $GIT_DIR/config file (or $HOME/.gitconfig file) like this:

[diff "tex"]
        xfuncname = "^(\\\\(sub)*section\\{.*)$"

Note. A single level of backslashes are eaten by the configuration file parser, so you would need to double the backslashes; the pattern above picks a line that begins with a backslash, and zero or more occurrences of sub followed by section followed by open brace, to the end of line.

愛放△進行李 2024-09-20 02:23:40

请参阅 gitattributes 手册页,“生成差异文本”部分,以及例如 diff.csharp .xfuncname 配置(搜索 xfuncname)。

See gitattributes manpage, the "Generating diff text" section, and e.g. diff.csharp.xfuncname configuration (search for xfuncname).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文