簡體   English   中英

是否可以使用 `git cat-file -p` 的 `master^{tree}` 語法來打印 blob?

[英]Is it possible to use the `master^{tree}` syntax of `git cat-file -p` to print blobs?

通過閱讀博客文章,我熟悉了git cat-file -p master^{tree}中的master^{tree}語法,用於打印與master分支(提交)對應的樹 object。

我可以使用這種語法來打印這棵樹中的 blob 嗎? 我嘗試了以下命令,但這些命令不起作用:

git cat-file -p master^{blob}git cat-file -p master^{tree}^{blob}

在 Git man頁中,我可以閱讀有關master^{tree}語法的更多信息嗎? git cat-file --help中沒有描述。

nlykkei:~/projects/demos/git-test (change-config)$ git cat-file -p master^{tree}
100644 blob 980a0d5f19a64b4b30a87d4206aade58726b60e3    hello-world
100644 blob 6bbf1b1052278a9507b22f1cac84503a05eb935c    package.json
100644 blob e82dbc14b98ed7d67e88381d58732b00459c180f    random-file
100644 blob b07036b6d12da665c096d9fe3b4d8e3551147e31    some-file
nlykkei:~/projects/demos/git-test (change-config)$ git cat-file -p master^{blob}
error: master^{blob}: expected blob type, but the object dereferences to tree type
fatal: Not a valid object name master^{blob}
nlykkei:~/projects/demos/git-test (change-config)$ git cat-file -p master^{tree}^{blob}
error: master^{tree}^{blob}: expected blob type, but the object dereferences to tree type
fatal: Not a valid object name master^{tree}^{blob}

通過閱讀一篇博文,我熟悉了git cat-file -p master^{tree}中的master^{tree}語法,用於打印master分支(提交)對應的樹 object。

從技術上講,這與git cat-file本身無關。

后綴^{ type } ,其中type是 Git 的四種 object 類型中的任何一種 - tagcommittreeblob - 實際上是修訂解析器的指令。 這些指令在gitrevisions 文檔中進行了描述。

修訂解析器本身可以使用git rev-parse單獨調用:

git rev-parse master^{tree}

例如。 這是 Git 的 Git 存儲庫的克隆結果:

$ git rev-parse master
d61d20c9b413225793f8a0b491bbbec61c184e26
$ git rev-parse master^{tree}
a09123ee85e55b1de2e3c70c43588f10d885cacb

這顯示了master^{tree}如何引用與master不同的 hash ID。 使用git cat-file -t ,我們可以看到為什么會這樣:

$ git cat-file -t master
commit
$ git cat-file -t master^{tree}
tree

Object d61d20c9b413225793f8a0b491bbbec61c184e26 is a commit object, and object a09123ee85e55b1de2e3c70c43588f10d885cacb is a tree object. 因此,當我們要求git cat-file打印該對象的內容(使用-p )時,我們會得到提交內容或樹內容。

我可以使用這種語法來打印這棵樹中的 blob 嗎?

不。

提交 object 本身包含如下數據:

$ git cat-file -p master | sed 's/@/ /'
tree a09123ee85e55b1de2e3c70c43588f10d885cacb
parent d2ea03ddeeeab6f703290af30ba89d5606858673
author Junio C Hamano <gitster pobox.com> 1588202142 -0700
committer Junio C Hamano <gitster pobox.com> 1588202142 -0700

The fifth batch

Signed-off-by: Junio C Hamano <gitster pobox.com>

這里有一些重要的背景項目需要了解:

  • 每個提交 object 都被限制為只包含一棵樹 object。 該樹代表提交的快照。

  • 每個分支名稱都被限制為解析為現有提交的 hash ID。

因此,如果master決定提交 hash H ,我們總是可以通過這種方式找到一棵樹 hash T Git therefore allows master^{tree} which tells Git: find the object identified by the name, then do whatever is required to find a tree object from that object . 這總是有效的,因為該名稱是一個分支名稱,它解析為一個提交,其中恰好包含一棵樹。

然而,樹通常包含許多子樹和/或許多 blob。 在您的情況下,您顯示的樹包含四個條目:

100644 blob 980a0d5f19a64b4b30a87d4206aade58726b60e3    hello-world
100644 blob 6bbf1b1052278a9507b22f1cac84503a05eb935c    package.json
100644 blob e82dbc14b98ed7d67e88381d58732b00459c180f    random-file
100644 blob b07036b6d12da665c096d9fe3b4d8e3551147e31    some-file

要將樹解析為該樹的特定條目,我們必須為修訂解析器提供name

在我的例子中,與分支名稱master關聯的樹在從提交 object 解析為樹 object 后,包含數百個名稱( wc -l報告 459 個名稱)。 以下是最后十個:

$ git cat-file -p master^{tree} | tail
100644 blob 95851b85b6b7181130f0cd441c2bd7ac0bfb89da    wrap-for-bin.sh
100644 blob 3a1c0e052677dca8d1bc19121d609e0c226e88ee    wrapper.c
100644 blob eab8c8d0b9aab55c8435b9f451efd81e715131f8    write-or-die.c
100644 blob 6e69877f25791632d98bf7b109a2eaebd04c96af    ws.c
100644 blob 98dfa6f73f9d7cd41867f97fdae41bd4dc5ec2a1    wt-status.c
100644 blob 73ab5d4da1c0b4766cb63501b9db06c0a7fea934    wt-status.h
100644 blob 4d20069302b25a133869380bd685e921ddc0bacc    xdiff-interface.c
100644 blob 93df26900c2bfa923f1e871924ef319a6786fa72    xdiff-interface.h
040000 tree 82fc725b3f0affab0f034a80baaf9e589b870942    xdiff
100644 blob d594cba3fc9d82d94b9277e886f2bee265e552f6    zlib.c

要從master到 tree 解析為zlib.c的 blob,我們必須包含文字字符串zlib.c

$ git rev-parse master:zlib.c
d594cba3fc9d82d94b9277e886f2bee265e552f6

即使是只包含一個條目的樹 object 也是如此; 僅當hash已解析為 blob object 時,才允許使用hash ^{blob}

相同的語法適用於大多數 Git 命令,包括git cat-file

$ git cat-file -t master:zlib.c
blob

仔細閱讀 gitrevisions 文檔。 這里有大量的信息,用相對較少的詞表示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM