如何在 git diff header 中控制哈希长度

How to control hash length in git diff header

git diff 的输出包含一个 header,如下所示:

index f8fdb16de,78132574a..000000000

git help diff中是这样解释的:

2. It is followed by one or more extended header lines (this example shows a merge with two parents):

       index <hash>,<hash>..<hash>
       mode <mode>,<mode>..<mode>
       new file mode <mode>
       deleted file mode <mode>,<mode>

我想使用 git diff 来创建补丁,并且我希望这些补丁具有可预测的格式以便进行比较。为此,我需要 "index .." header.

中的哈希的固定长度

如何控制这些散列的长度?

我试过--abbrev=7,好像没有效果

我仍然看到我的补丁是这样更新的:

-index 52a2a35..7813257 100755
+index 52a2a357e..78132574a 100755

--abbrev 选项仅适用于 "diff-raw format output and diff-tree header lines"。对于标准补丁输出,您可以使用 git diff --full-index 来获取完整的、未缩写的 blob ID。来自 man page:

--full-index
Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

这将产生如下输出:

diff --git a/foo b/foo
index c7bc37b70c7e29e3e4ed048c22ca3929367aa171..ab10096fde76d8c1d6172bd09d0dc4a18fb2c2fa 100644
Binary files a/foo and b/foo differ

I tried --abbrev=7, but it seems to have no effect.
so it is not possible to get a fixed abbrev length?

是,但仅适用于 Git 2.29(2020 年第 4 季度):之前,命令“diff”系列的输出包含补丁中涉及的 blob 的缩写对象名称,但它的长度 不受 --abbrev 选项的影响。
现在是。

参见 commit 3046c7f (21 Aug 2020) by Đoàn Trần Công Danh (sgn)
参见 commit fc7e73d (21 Aug 2020) by brian m. carlson (bk2204)
(由 Junio C Hamano -- gitster -- in commit 096c948 合并,2020 年 8 月 31 日)

diff: index-line: respect --abbrev in object's name

Signed-off-by: Đoàn Trần Công Danh

A handful of Git's commands respect --abbrev for customizing length of abbreviation of object names.

For diff-family, Git supports 2 different options for 2 different purposes:

  • --full-index for showing diff-patch object's name in full, and
  • --abbrev to customize the length of object names in diff-raw and diff-tree header lines, without any options to customise the length of object names in diff-patch format.

When working with diff-patch format, we only have two options, either full index, or default abbrev length.

Although, that behaviour is documented, it doesn't stop users from trying to use --abbrev with the hope of customising diff-patch's objects' name's abbreviation.

Let's allow the blob object names shown on the "index" line to be abbreviated to arbitrary length given via the "--abbrev" option.

To preserve backward compatibility with old script that specify both --full-index and --abbrev, always show full object id if --full-index is specified.

diff-options 现在包含在其 man page 中:

In diff-patch output format, --full-index takes higher precedence, i.e. if --full-index is specified, full blob names will be shown regardless of --abbrev.

Non default number of digits can be specified with --abbrev=<n>.


关于“--abbrev=<n>”选项的文档没有说明输出可能比“<n>”十六进制数,这一点已通过Git 2.30(2021 年第一季度)。

参见 commit cda34e0 (04 Nov 2020) by Junio C Hamano (gitster)
(由 Junio C Hamano -- gitster -- in commit ee13beb 合并,2020 年 11 月 11 日)

doc: clarify that --abbrev=<n> is about the minimum length

Helped-by: Derrick Stolee

Early text written in 2006 explains the "--abbrev=<n>" option to "show only a partial prefix", without saying that the length of the partial prefix is not necessarily the number given to the option to ensure that the output names the object uniquely.

Update documentation for the diff family of commands, "blame", "branch --verbose", "ls-files" and "ls-tree" to stress that the short prefix must uniquely refer to an object, and is merely the mininum number of hexdigits used in the prefix.

diff-options 现在包含在其 man page 中:

lines, show the shortest prefix that is at least '<n>' hexdigits long that uniquely refers the object.

git blame 现在包含在其 man page 中:

abbreviated object name, use <m>+1 digits, where <m> is at least <n> but ensures the commit object names are unique.

git branch 现在包含在其 man page 中:

--abbrev=<n>

In the verbose listing that show the commit object name, show the shortest prefix that is at least '<n>' hexdigits long that uniquely refers the object.