- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
当我在 C# 文件上使用 git diff
时,我看到如下内容:
diff --git a/foo.cs b/foo.cs
index ff61664..dd8a3e3 100644
--- a/foo.cs
+++ b/foo.cs
@@ -15,6 +15,7 @@ static void Main(string[] args)
string name = Console.ReadLine();
}
Console.WriteLine("Hello {0}!", name);
+ Console.WriteLine("Goodbye");
}
}
}
hunk 标题行包含当前方法的第一行 (static void Main(string[] args)
),这很棒。然而它似乎不是很可靠......我看到很多情况下它不起作用。
所以我想知道,这段摘录是从哪里来的? git diff
是否以某种方式识别语言语法?有没有办法自定义它?
最佳答案
Is there a way to customize it?
配置在 .gitattributes
, section "Defining a custom hunk-header"中定义:
First, in
.gitattributes
, you would assign thediff
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.
There are a few built-in patterns to make this easier, and
tex
is one of them, so you do not have to write the above in your configuration file (you still need to enable this with the attribute mechanism, via.gitattributes
).
('csharp
'是当前内置模式的一部分)
Where does this excerpt come from?
Doesgit diff
somehow recognize the language syntax?
最初,该算法对于函数名称检测非常粗糙:
参见 commit acb7257 (Git 1.3.0,2006 年 4 月,作者:Mark Wooding)
xdiff
: 在 block 头中显示函数名称。The speed of the built-in diff generator is nice; but the function namesshown by
diff -p
are really nice. And I hate having to choose.
So, we hackxdiff
to find the function names and print them.The function names are parsed by a particularly stupid algorithm at themoment: it just tries to find a line in the 'old' file, from before thestart of the hunk, whose first character looks plausible. Still, it'smost definitely a start.
它是用 get_func_line() 精炼的, 本身来自 commit f258475 (Git 1.5.3,2007 年 9 月,作者 Junio C Hamano ( gitster
))
您可以在该提交中看到测试 t/t4018-diff-funcname.sh
, 以测试自定义 diff 函数名称模式。
This makes "
diff -p
" hunk headers customizable viagitattributes
mechanism.
It is based on Johannes's earlier patch that allowed to define a singleregexp to be used for everything.The mechanism to arrive at the regexp that is used to define hunk headeris the same as other use of
gitattributes
.
You assign an attribute,funcname
(because "diff -p
" typically uses the name of the function the patch is about as the hunk header), a simple string value.
This can be one of the names of built-in pattern (currently,java
" is defined) or a custom pattern name, to be looked up from the configuration file.(in .gitattributes)
*.java funcname=java
*.perl funcname=perl
(in .git/config)
[funcname]
java = ... # ugly and complicated regexp to override the built-in one.
perl = ... # another ugly and complicated regexp to define a new one.
当前xfuncname
commit 45d9414 中介绍了语法, Git 1.6.0.3,2008 年 10 月,由 Brandon Casey 撰写
diff.*.xfuncname
它使用“扩展的”正则表达式来选择大块标题Currently, the hunk headers produced by '
diff -p
' are customizable bysetting thediff.*.funcname
option in the config file. The 'funcname
' option takes a basic regular expression. This functionality was designed using the GNU regex library which, by default, allows using backslashed versions of some extended regular expression operators, even in Basic Regular Expression mode. For example, the following characters, when backslashed, are interpreted according to the extended regular expression rules:?
,+
, and|
.
As such, the builtinfuncname
patterns were created using some extendedregular expression operators.Other platforms which adhere more strictly to the POSIX spec do notinterpret the backslashed extended RE operators in Basic Regular Expressionmode. This causes the pattern matching for the builtin funcname patterns tofail on those platforms.
Introduce a new option '
xfuncname
' which uses extended regular expressions, and advertise it instead offuncname
.
Since most users are on GNU platforms, the majority offuncname
patterns are created and tested there.
Advertising onlyxfuncname
should help to avoid the creation of non-portable patterns which work with GNU regex but not elsewhere.Additionally, the extended regular expressions may be less ugly andcomplicated compared to the basic RE since many common special operators do not need to be backslashed.
For example, the GNU Basic RE:
^[ ]*\\(\\(public\\|static\\).*\\)$
becomes the following Extended RE:
^[ ]*((public|static).*)$
最后,它被扩展为commit 14937c2 ,适用于 git 1.7.8(2011 年 12 月),由 René Scharfe 撰写。
diff
: 添加选项以将整个函数显示为上下文Add the option
-W
/--function-context
togit diff
.
It is similar to the same option ofgit grep
and expands the context of change hunks so that the whole surrounding function is shown.
This "natural" context can allow changes to be understood better.
它仍在 Git 2.15(2017 年第 4 季度)中进行调整
The built-in pattern to detect the "function header" for HTML didnot match
<H1>..<H6>
elements without any attributes, which hasbeen fixed.
在 2.15 之前,它无法匹配 <h1>...</h1>
, 而 <h1 class="smth">...</h1>
匹配。
参见 commit 9c03cac (2017 年 9 月 23 日)Ilya Kantor ( iliakan
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 376a1da ,2017 年 9 月 28 日)
检测函数边界的模式称为 xfuncref
.
参见 commit a807200 (2019 年 11 月 8 日)Łukasz Niemier ( hauleth
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 376e730,2019 年 12 月 1 日),适用于 Git 2.25(2020 年第一季度)
userdiff
: add Elixir to supported userdiff languagesSigned-off-by: Łukasz Niemier
Acked-by: Johannes Sixt
Adds support for
xfuncref
in Elixir language which is Ruby-like language that runs on Erlang Virtual Machine (BEAM).
和:
参见 commit d1b1384 (2019 年 12 月 13 日)Ed Maste ( emaste
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit ba6b662 ,2019 年 12 月 25 日)
userdiff
: remove empty subexpression fromelixir
regexSigned-off-by: Ed Maste
Reviewed-by: Jeff King
Helped-by: Johannes Sixt
The regex failed to compile on FreeBSD.
Also add
/* -- */
mark to separate the two regex entries given to thePATTERNS()
macro, to make it consistent with patterns for other content types.
Git 2.27(2020 年第二季度)添加了 Markdown 文档的 userdiff 模式。
参见 commit 09dad92 (2020 年 5 月 2 日)Ash Holland ( sersorrel
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit dc4c393,2020 年 5 月 8 日)
userdiff
: support MarkdownSigned-off-by: Ash Holland
Acked-by: Johannes Sixt
It's typical to find Markdown documentation alongside source code, and having better context for documentation changes is useful; see also commit 69f9c87d4 ("
userdiff
: add support for Fountain documents", 2015-07-21, Git v2.6.0-rc0 -- merge listed in batch #1).
The pattern is based on the CommonMark specification 0.29, section 4.2 https://spec.commonmark.org/ but doesn't match empty headings, as seeing them in a hunk header is unlikely to be useful.
Only ATX headings are supported, as detecting setext headings would require printing the line before a pattern matches, or matching a multiline pattern. The word-diff pattern is the same as the pattern for HTML, because many Markdown parsers accept inline HTML.
在 Git 2.30(2021 年第一季度)中,userdiff 模式学会了识别 POSIX shell 中的函数定义和 bash
.
参见 commit 2ff6c34 (2020 年 10 月 22 日)Victor Engmark ( l0b0
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 292e53f,2020 年 11 月 2 日)
userdiff
: support BashSigned-off-by: Victor Engmark
Acked-by: Johannes Sixt
Support POSIX, bashism and mixed function declarations, all four compound command types, trailing comments and mixed whitespace.
Even though Bash allows locale-dependent characters in function names, only detect function names with characters allowed by POSIX.1-2017 for simplicity.
This should cover the vast majority of use cases, and produces system-agnostic results.Since a word pattern has to be specified, but there is no easy way to know the default word pattern, use the default
IFS
characters for a starter. A later patch can improve this.
gitattributes
现在包含在其 man page 中:
bash
suitable for source code in the Bourne-Again SHell language.
Covers a superset of POSIX shell function definitions.
在 Git 2.32(2021 年第 2 季度)中,添加了“方案”的用户差异模式。
参见 commit a437390 (2021 年 4 月 8 日)Atharva Raykar ( tfidfwastaken
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 6d7a62d,2021 年 4 月 20 日)
userdiff
: add support for SchemeSigned-off-by: Atharva Raykar
Add a diff driver for Scheme-like languages which recognizes top level and local
define
forms, whether it is a function definition, binding, syntax definition or a user-defineddefine-xyzzy
form.Also supports R6RS
library
forms,module
forms along with class and struct declarations used in Racket (PLT Scheme).Alternate "def" syntax such as those in Gerbil Scheme are also supported, like defstruct, defsyntax and so on.
The rationale for picking
define
forms for the hunk headers is because it is usually the only significant form for defining the structure of the program, and it is a common pattern for schemers to have local function definitions to hide their visibility, so it is not only the top leveldefine
's that are of interest.
Schemers also extend the language with macros to provide their own define forms (for example, something like adefine-test-suite
) which is also captured in the hunk header.Since it is common practice to extend syntax with variants of a form like
module+
,class*
etc, those have been supported as well.The word regex is a best-effort attempt to conform to R7RS (section 2.1) valid identifiers, symbols and numbers.
gitattributes
现在包含在其 man page 中:
scheme
suitable for source code in the Scheme language.
在 Git 2.33(2021 年第 3 季度)中,C# 的 userdiff 模式学习了 token “record
”。
参见 commit c4e3178 (2021 年 3 月 2 日)作者:Julian Verdurmen ( 304NotModified
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit f741069 ,2021 年 7 月 8 日)
userdiff
: add support for C# record typesSigned-off-by: Julian Verdurmen
Reviewed-by: Johannes Schindelin
Records are added in C# 9
Code example :
public record Person(string FirstName, string LastName);
For more information, see https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9
在 Git 2.34(2021 年第 4 季度)中,“java”语言的 userdiff 模式已经更新。
参见 commit a8cbc89 (2021 年 8 月 11 日)Tassilo Horn ( tsdh
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit a896086 ,2021 年 8 月 30 日)
userdiff
: improve java hunk header regexSigned-off-by: Tassilo Horn
Currently, the
git diff
(man) hunk headers show the wrong method signature if the method has a qualified return type, an array return type, or a generic return type because the regex doesn't allow dots(.)
,[]
, or<
and>
in the return type.
Also, type parameter declarations couldn't be matched.Add several t4018 tests asserting the right hunk headers for different cases:
- enum constant change
- change in generic method with bounded type parameters
- change in generic method with wildcard
- field change in a nested class
并且,仍然使用 Git 2.34(2021 年第 4 季度),更新了 C++ 语言的用户差异模式。
参见 commit 386076e (2021 年 10 月 24 日),commit c4fdba3 , commit 637b80c , commit bfaaf19 (2021 年 10 月 10 日),和 commit 350b87c , commit 3e063de , commit 1cf9384 (2021 年 10 月 8 日)Johannes Sixt ( j6t
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit f3f157f,2021 年 10 月 25 日)
例如:
userdiff-cpp
: permit the digit-separating single-quote in numbersSigned-off-by: Johannes Sixt
Since C++17, the single-quote can be used as digit separator:
3.141'592'654
1'000'000
0xdead'beafMake it known to the word regex of the cpp driver, so that numbers are not split into separate tokens at the single-quotes.
关于git - git diff hunk header 中的摘录来自哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28111035/
我正在尝试创建一个补丁,从 makefile 中删除最后 20 行左右,但我收到“Hunk #1 FAILED at 14”。以及以下拒绝文件。 *************** *** 14,35 *
在 Sublime Merge 中,在查看文件历史记录时,将鼠标悬停在代码差异区域时会出现 Hunk history按钮。 此按钮提供什么功能? 最佳答案 它将历史限制为仅显示该特定文件中那些特定行的
我使用 git add -p 将我的代码更改拆分为多个提交。但是,之后执行 git commit 会提交所有 更改,包括未暂存的更改。我看了一些关于 SO 的问题,但没有发现任何明显的错误。你能帮我理
我是 Eclipse PyDev + EGit 的重度用户,希望切换到 PyCharm。在 Eclipse 中,我可以选择仅将文件的某些 block /行添加到 git 索引,与在终端中使用 git
解决合并冲突时,一个相当常见的模式是我和另一个人都修改了列表或通常被附加到的其他代码部分。如: global.registerFeature(fc); global.registerFeature(f
我从另一个团队那里得到了大约 200 个补丁的巨大补丁。我已将它们分成单独的补丁并应用它们。 对于某些补丁,我看到输出为“补丁文件 aa/bb/cc.c”对于其他一些人,我看到输出为“Hunk #1
如果我正在查看日志消息并选择一个特定的提交来查看差异,是否可以恢复(或反向应用)该提交的特定 block ?我知道有一个还原提交选项,但我只对其中的一小部分感兴趣。 提前致谢。 最佳答案 是的,无论是
这个问题在这里已经有了答案: Can I split an already split hunk with git? (4 个答案) 关闭 3 年前。 我跑过: git add -p 然后我想通过输
显然,SourceTree 只显示修改文件中的第一个 hunks。 如何让它显示所有帅哥? 是否有定义限制的设置? 最佳答案 看起来这是由选项窗口中 Diff 选项卡上的 Max Diff Line
我希望从一个独特的提交中制作一个交互式补丁。我想使用类似于 的输出 git diff --word-diff=porcelain [file] 用于交互式修补。命令 git add --edit 只推
如果我做了两个逻辑上的改动,这在代码上是连续的。 Git 在交互式添加时将其显示为一个大块。 有没有办法在 add --patch 期间专门在 block 中只添加几行? 最佳答案 git add -
在 git 中,如果我的索引中有来自同一个文件的几个 hunk,我如何以交互方式取消其中一个? 是否有任何替代方法可以取消暂存整个文件,然后重新暂存我想保留的 hunk,或者手动撤消对工作副本的更改,
我通常使用 git add -p 添加更改,而且很多时候有几个代码块的大块头,由空行分隔。 但是,git 不会进一步拆分 大块头,我不得不求助于手动编辑。 如何增加 hunk 的粒度,以便每个代码块都
我非常喜欢 git add -p 和 git stash 但我偶尔会遇到以下问题,可以通过以下命令序列重现: git add -p my_file:然后我手动编辑一个大块头(使用e)因为git建议的拆
git add -p 用于暂存更改,git checkout -p 用于交互式丢弃更改。如何取消 hunks 对索引的更改? (我认为 git unstage -p 或 git reset HEAD
我已经通读了源代码,并尝试在线研究它,但我在完全理解它方面遇到了瓶颈。 我的意思是,它与区域分配有何不同? “zone”是否用于小内存,而“hunk”用于模型等较大的东西? 谢谢 最佳答案 是的,hu
我刚刚尝试了 2018.2 EAP,但我没有在我的 gutter 中看到用于部分提交的复选框。而且它也不在提交对话框中。 没有IDE异常,这里提到的其他新特性:https://blog.jetbrai
这可能吗?如果我不必在将 hunk 分解为不同的提交时在 git-add -i 和 git commit 之间来回切换,那就太好了。有一个更好的方法吗?还是我做错了什么? 最佳答案 如 git boo
情况 我在 repo 中提交了一个名为 testing.txt 的文件,其中包含以下内容: First line 我正在尝试应用此补丁,它修改了两个文件,testing.txt 和 nonexiste
当我在 C# 文件上使用 git diff 时,我看到如下内容: diff --git a/foo.cs b/foo.cs index ff61664..dd8a3e3 100644 --- a/fo
我是一名优秀的程序员,十分优秀!