如何从“git diff-tree --stdin”获得任何输出?

How to get any output from “git diff-tree --stdin”?

git diff-tree 的手册页建议我可以对我选择的一堆树对象进行组合差异 (--cc)。您必须为此使用 --stdin 选项。但是我根本无法--stdin输出任何东西。

示例:

$ git diff-tree --stdin
a b
a b
$

这里我输入了abCtrl-D:它甚至没有抱怨 ab 是错误的输入……但即使使用有效的提交哈希 and/or 更多选项我根本没有得到任何输出。

我做错了什么?

您必须向 git diff-tree --stdin:

提供提交或树对象的 完整 哈希值
  $ git log -3 --pretty=%H
  a30ec5de57bbfa0c19045f3c094ec6eb4d808eb4
  f0939d956ad9ef2a00360139a6f1d1ad66accbe5
  fcdb73de293b2442231e5d8ce19f9c7d1640d186

  $ # lines typed on stdin are marked by ->
  $ git diff-tree --stdin
->fcdb73de293b2442231e5d8ce19f9c7d1640d186 a30ec5de57bbfa0c19045f3c094ec6eb4d808eb4
  fcdb73de293b2442231e5d8ce19f9c7d1640d186
  :040000 040000 e2aed2af18fb5293903a0d0b78c23e00a893394d c56666f973b77d92d52b68a14c5a26ac3508571a M    example
  :040000 000000 7cc3373fd5cad3bfe6ec261e9dcc3a9e97efe488 0000000000000000000000000000000000000000 D    prc
  :000000 040000 0000000000000000000000000000000000000000 275872c00a9f51016d2273419345b3b1d7535630 A    src
->275872c00a9f51016d2273419345b3b1d7535630 7cc3373fd5cad3bfe6ec261e9dcc3a9e97efe488
  275872c00a9f51016d2273419345b3b1d7535630 7cc3373fd5cad3bfe6ec261e9dcc3a9e97efe488
  :100644 100644 f26b2198fd9a0103f57a5bd828e58043507ea7b7 c52116f6d185548061058099dfe4c9e50d523aff M    chord.cpp
  :100644 100644 fcbfcdc036fb53733176ed30fd82eb261a990d5b 403e323f332a18b45dfdebfd3a8bfb1a62158bb4 M    chord.hpp
  :100644 100644 11a931f795b44cd916e1607d819eed4d5342edba 333b596faf596d73758bce949e6d86e596153126 M    polyphonic_track.cpp
  :100644 100644 7196835de2385d3f1e0b20073d327cb432ed436c f9fbbac93ef6fc19ae99113ff39a06f4df50720e M    polyphonic_track.hpp

另一种方式:

git rev-list HEAD | git diff-tree --stdin 

但是,在 Git 2.27(2020 年第 2 季度)之前,“git diff-tree --pretty --notes”曾经遇到断言失败,因为它忘记了初始化注释子系统。

参见 commit 5778b22 (21 Apr 2020) by Taylor Blau (ttaylorr)
(由 Junio C Hamano -- gitster -- in commit 5a96715 合并,2020 年 4 月 28 日)

diff-tree.c: load notes machinery when required

Reported-by: Jeff King
Signed-off-by: Taylor Blau
Acked-by: Jeff King

Since its introduction in 7249e91 ("[revision.c](https://github.com/git/git/blob/5778b22b3d690495e724276663c36ccd5329da4d/revision.c): support --notes command-line option", 2011-03-29, Git v1.7.6-rc0 -- merge listed in batch #0), combining '--notes' with any option that causes us to format notes (e.g., '--pretty', '--format="%N"', etc) results in a failed assertion at runtime.

$ git rev-list HEAD | git diff-tree --stdin --pretty=medium --notes
commit 8f3d9f354286745c751374f5f1fcafee6b3f3136
git: notes.c:1308: format_display_notes: Assertion `display_notes_trees' failed.
Aborted

This failure is due to diff-tree not calling 'load_display_notes' to initialize the notes machinery.

Ordinarily, this failure isn't triggered, because it requires passing both '--notes' and another of the above mentioned options.
In the case of '--pretty', for example, we set 'opt->verbose_header', causing 'show_log()' to eventually call 'format_display_notes()', which expects a non-NULL 'display_note_trees'.

Without initializing the notes machinery, 'display_note_trees' remains NULL, and thus triggers an assertion failure.

Fix this by initializing the notes machinery after parsing our options.