术语"hunk"确实不是Git特有的,而是来自于Gnu diffutil format。更简洁地说:
每个“hunk”显示文件差异的一个区域。
但对于Git来说,挑战在于确定"hunk"的正确边界。
答案的其余部分帮助说明了Git中"hunk"的样子:
经过各种启发式算法(例如紧凑算法,已在Git 2.12中被删除),Git维护者选择了缩进算法,该算法在2016年9月引入了Git 2.11,提交为433860f。
Some groups of added/deleted lines in diffs can be slid up or down,
because lines at the edges of the group are not unique. Picking good shifts for such groups is not a matter of correctness but definitely has a big effect on aesthetics.
For example, consider the following two diffs.
The first is what standard Git emits:
--- a/9c572b21dd090a1e5c5bb397053bf8043ffe7fb4:git-send-email.perl
+++ b/6dcfa306f2b67b733a7eb2d7ded1bc9987809edb:git-send-email.perl
@@ -231,6 +231,9 @@ if (!defined $initial_reply_to && $prompting) {
}
if (!$smtp_server) {
+ $smtp_server = $repo->config('sendemail.smtpserver');
+}
+if (!$smtp_server) {
foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
if (-x $_) {
$smtp_server = $_;
The following diff is equivalent, but is obviously preferable from an
aesthetic point of view:
--- a/9c572b21dd090a1e5c5bb397053bf8043ffe7fb4:git-send-email.perl
+++ b/6dcfa306f2b67b733a7eb2d7ded1bc9987809edb:git-send-email.perl
@@ -230,6 +230,9 @@ if (!defined $initial_reply_to && $prompting) {
$initial_reply_to =~ s/(^\s+|\s+$)//g;
}
+if (!$smtp_server) {
+ $smtp_server = $repo->config('sendemail.smtpserver');
+}
if (!$smtp_server) {
foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
if (-x $_) {
This patch teaches Git to pick better positions for such "diff sliders" using heuristics that take the positions of nearby blank lines and the indentation of nearby lines into account.
从Git 2.14版本(2017年第三季度)开始,这种缩进启发式方法将成为默认设置!
请查看由Jeff King (peff)于2017年5月8日提交的提交 1fa8a66。
请查看由Stefan Beller (stefanbeller)于2017年5月8日提交的提交 33de716。
请查看由Marc Branchaud于2017年5月8日提交的提交 37590ce,提交 cf5e772。
(在提交53083f8中由Junio C Hamano -- gitster --合并,于2017年6月5日)
diff:默认启用缩进启发式
该功能已在v2.11中包含(发布于2016年11月29日),我们没有收到任何负面反馈。相反,我们得到的所有反馈都是积极的。
默认启用它。不喜欢这个功能的用户可以通过设置 diff.indentHeuristic 来关闭它。
在 Git 2.24(2019年第四季度)中,“缩进启发式算法”决定分割差异块的位置的文档已经得到了修正。
请参见提交记录64e5e1f(由SZEDER Gábor (szeder)于2019年8月15日发布)。
(由Junio C Hamano -- gitster --合并于提交记录e115170,2019年9月9日)
diff: 'diff.indentHeuristic'不再是实验性的
缩进启发式算法最初是作为实验性功能推出的,但自从33de716(diff: enable indent heuristic by default, 2017-05-08, Git v2.14.0-rc0),它已成为默认的差异启发式算法。
遗憾的是,该提交没有更新文档,并且 'diff.indentHeuristic' 配置变量的描述仍然暗示它是实验性的而不是默认值。
更新 'diff.indentHeuristic' 的描述,使其清楚表示它是默认的差异启发式算法。
相关的 '--indent-heuristic' 选项的描述已经在 这个答案中 更新了。
文档现在将会显示:
diff.indentHeuristic:
将此选项设置为false以禁用默认启发式算法,该算法会移动差异块边界以使补丁更易读。
在 Git 2.25 (2020年第一季度) 中,您甚至不需要再指定--indent-heuristic(因为它已经是默认设置了一段时间)。
请参见 提交44ae131 (2019年10月28日),作者为SZEDER Gábor (szeder)。
(由Junio C Hamano -- gitster --于提交532d983中合并,2019年12月1日)
builtin/blame.c: 从用法字符串中删除“--indent-heuristic”
Signed-off-by: SZEDER Gábor
自33de716387("diff: enable indent heuristic by default",2017-05-08,Git v2.14.0-rc0 - merge列在batch #7中)以来,缩进启发式是我们的默认差异启发式,但'git blame'的用法字符串仍将其称为“实验启发式”。
我们可以简单地更新与选项相关联的短帮助,但根据选项声明上面的注释,“只包含在“-h”输出中”,这使得它被包含在内。
当该功能仍处于试验阶段并且我们想要更多地暴露它时,这是有道理的,但现在不必要了。
因此,让我们从'git blame'的用法字符串中删除'--indent-heuristic'选项。
请注意,'git blame'仍将接受此选项,因为它在parse_revision_opt()中解析。
敏锐的读者可能会注意到,此补丁删除了一个提到“以下两个选项”的注释,但它只删除了一个选项。
原因是该注释已过时:另一个选项是'--compaction-heuristic',它已在3cde4e02ee(diff: retire "compaction" heuristics, 2016-12-23)中被删除,但该提交忘记更新此注释。