繁体   English   中英

git中的`cat-file`代表什么?

[英]What does `cat-file` stand for in git?

git,这个命令中的cat-file代表什么?

$ git cat-file <...>

我的第一个想法是“连接文件”,因为 Unix 命令cat代表“连接”,但这并不对应于git cat-file的 function。

虽然cat确实代表“连接”,但它实际上只是按照它们在cat的命令行参数中出现的顺序显示一个或多个文件。 在 Linux 或 *nix 系统上查看文件内容的常见模式是:

cat <file>

cat和 Git 的cat-file之间的主要区别在于它只显示一个文件(因此是-file部分)。 Git 的cat-file并不真正代表“连接”; 它只是对cat命令行为的引用。

git-cat-file - 为存储库对象提供内容或类型和大小信息

从技术上讲,如果您使用批量输出模式,您可以使用git cat-file连接文件:

批量输出

如果--batch--batch-checkcat-file将从标准输入读取对象,每行一个,并打印有关它们的信息。 默认情况下,整行被视为一个对象,就好像它被馈送到git-rev-parse [1]。

读取 git 对象的内容(或 blob)

git cat-file -p <SHA1>

读取它的类型

git cat-file -t <SHA1>

cat 和 Git 的 cat-file 的主要区别在于它只显示一个文件(因此是 -file 部分)

单个文件,...或单个文件list

在第二种形式中,对象列表(由换行符分隔)在 stdin 上提供,每个对象的 SHA-1、类型和大小打印在 stdout 上。
可以使用可选的<format>参数覆盖输出格式。

当您考虑git cat-file --batch时,这一点很重要,它会打印stdin 上提供的每个对象的对象信息和内容。

另请参阅git cat-files --batch-command与 Git 2.36 (Q2 2022)

并且随着 Git 2.34(2021 年第四季度),驱动“ git for-each-refman及其朋友的“ --format ”选项的“ ref-filter ”机制不断发展,可用于git cat-file --batch “。

请参阅Junio C Hamano ( gitster )提交 bff9703 (2021 年 7 月 1 日)。
参见胡哲宁的提交 b9dee07、提交e85fcb3提交 7121c4d提交 bd0708c提交 311d0b8 (2021 年 7 月 26 日) adlternative
(由Junio C Hamano -- gitster --提交 bda891e中合并,2021 年 8 月 24 日)

ref-filter : 添加 %(rest) 原子

审核人:雅各布·凯勒
推荐人:Jacob Keller
指导者:Christian Couder
指导者: Hariom Verma
签字人:胡哲宁

%(rest)是用于 cat-file 批处理模式的原子,它可以在第一个空白边界处拆分输入行,该空白之前的所有字符都被认为是对象名称; 第一次运行空格之后的字符(即,行的“其余”)将代替 %(rest) 原子输出。

为了让“ cat-file --batch=%(rest) ”使用ref-filter接口,为ref-filter添加%(rest) atom。

引入reject_atom()来拒绝 " git for-each-ref " ( man ) 、" git branch " ( man ) 、" git tag " ( man )git verify-tag " 的原子%(rest)

所以两个命令都应该返回相同的结果:

git cat-file commit refs/tags/testtag^{} >expected &&
git for-each-ref --format="%(*raw)" refs/tags/testtag 

basic atom: refs/tags/testtag *raw

同样适用于:

git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)%(raw)" refs/mytrees/first

请注意,使用 Git 2.36(2022 年第二季度),“ git cat-file --helpman更加清晰。

See commit 5fb2490 , commit 83dc443 (10 Jan 2022), and commit 245b948 , commit 9ce6000 , commit 57d6a1c , commit b3fe468 , commit 485fd2c , commit 5a40417 , commit 97fe725 , commit fa476be , commit 68c69f9 , commit ddf8420 (28 Dec 2021) by Ævar Arnfjörð Bjarmason ( avar )
(由Junio C Hamano -- gitster --提交 008028a中合并,2022 年 2 月 5 日)

cat-file :更正和改进使用信息

签字人:Ævar Arnfjörð Bjarmason

将“ git cat-fileman -h 上发出的使用输出更改为对相关选项进行分组,让用户清楚哪些选项与其他选项搭配。

新的输出是:

 Check object existence or emit object contents -e check if <object> exists -p pretty-print <object> content Emit [broken] object attributes -t show object type (one of 'blob', 'tree', 'commit', 'tag', ...) -s show object size --allow-unknown-type allow -s and -t to work with broken/corrupt objects Batch objects requested on stdin (or --batch-all-objects) --batch[=<format>] show full <object> or <rev> contents --batch-check[=<format>] like --batch, but don't emit <contents> --batch-all-objects with --batch[-check]: ignores stdin, batches all known objects Change or optimize batch output --buffer buffer --batch output --follow-symlinks follow in-tree symlinks --unordered do not order objects before emitting them Emit object (blob or tree) with conversion or filter (stand-alone, or with batch) --textconv run textconv on object's content --filters run filters on object's content --path blob|tree use a <path> for (--textconv | --filters ); Not with 'batch'

旧用法是:

 <type> can be one of: blob, tree, commit, tag -t show object type -s show object size -e exit with zero when there's no error -p pretty-print object's content --textconv for blob objects, run textconv on object's content --filters for blob objects, run filters on object's content --batch-all-objects show all objects with --batch or --batch-check --path <blob> use a specific path for --textconv/--filters --allow-unknown-type allow -s and -t to work with broken/corrupt objects --buffer buffer --batch output --batch[=<format>] show info and content of objects fed from the standard input --batch-check[=<format>] show info about objects fed from the standard input --follow-symlinks follow in-tree symlinks (used with --batch or --batch-check) --unordered do not order --batch-all-objects output

虽然更短,但我认为新的更容易理解,例如“ --allow-unknown-type ”与“ -t ”和“ -s ”组合在一起,因为它只能与这些选项结合使用。
--buffer ”、“ --unordered ”等也是如此。


仍然使用 Git 2.36(2022 年第二季度),使用特定于 git 中默认格式的硬编码格式化逻辑优化strbuf_expand()调用,即 cat-file 的--batch--batch-check选项。

请参阅John Cai ( john-cai )提交 eb54a33 (2022 年 3 月 15 日)。
(由Junio C Hamano -- gitster --提交 889860e中合并,2022 年 3 月 23 日)

cat-file : 跳过扩展默认格式

签字人:Ævar Arnfjörð Bjarmason
签字人:蔡强

当 format 传入--batch--batch-check--batch-command时,格式会被扩展。
当没有传入任何内容时,将设置默认格式并调用expand_format()

我们可以通过硬编码在没有传递任何格式或传递默认格式时如何打印信息来节省这些周期。
不需要使用默认的完全扩展格式。
由于batch_object_write()发生在批处理模式下提供的每个对象上,我们得到了很好的性能改进。

 git rev-list --all > /tmp/all-obj.txt git cat-file --batch-check </tmp/all-obj.txt

带头^:

 Time (mean ± σ): 57.6 ms ± 1.7 ms [User: 51.5 ms, System: 6.2 ms] Range (min … max): 54.6 ms … 64.7 ms 50 runs

带头:

 Time (mean ± σ): 49.8 ms ± 1.7 ms [User: 42.6 ms, System: 7.3 ms] Range (min … max): 46.9 ms … 55.9 ms 56 runs

如果没有提供任何格式参数,或者如果传递了默认格式,则跳过格式扩展并使用默认格式打印对象信息。

请参阅此讨论

添加到@Matoeil 答案,您只需要指定<SHA1>的 5 个字符。

$ tree .git/

.git/
├── COMMIT_EDITMSG
├── HEAD
├── config
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           ├── master
│           └── testBranch
├── objects
│   ├── 1e
│   │   └── e2a78c0b40dd8e5c6b08e31171a3ce1e8d931b
│   ├── 29
│   │   └── 33b9017f79a27ff5ad3c4e154f67b44ae8482c
│   ├── 4a
│   │   └── 6a376085b9b3b8e6e73d2cdcc5281cf6915c58
│   ├── 4b
│   │   └── 825dc642cb6eb9a060e54bf8d69288fbee4904
│   ├── 7e
│   │   └── 6965a8b2ff07da3e632a24ee024b9d2ec5245d
│   ├── ae
│   │   └── 853f7ece778281c463c1f0c603ef9d47a425b7
│   ├── info
│   └── pack
└── refs
├── heads
│   ├── master
│   └── testBranch
└── tags

17 directories, 28 files

$ git cat-file -t ae853
tree 

$ git cat-file -p ae853
100644 blob 7e6965a8b2ff07da3e632a24ee024b9d2ec5245d    fil1.txt 

这里解释的很好。

git cat-file - Cat(连接)命令它从文件中读取数据并输出内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM