简体   繁体   English

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

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

In git, what does cat-file stand for in this command? git,这个命令中的cat-file代表什么?

$ git cat-file <...>

My first thought is "concatenate file" because the Unix command cat stands for "concatenate", but this doesn't correspond to the function of git cat-file .我的第一个想法是“连接文件”,因为 Unix 命令cat代表“连接”,但这并不对应于git cat-file的 function。

While cat does stand for "concatenate", what it actually does is simply display one or multiple files, in order of their appearance in the command line arguments to cat .虽然cat确实代表“连接”,但它实际上只是按照它们在cat的命令行参数中出现的顺序显示一个或多个文件。 The common pattern to view the contents of a file on Linux or *nix systems is:在 Linux 或 *nix 系统上查看文件内容的常见模式是:

cat <file>

The main difference between cat and Git's cat-file is that it only displays a single file (hence the -file part). cat和 Git 的cat-file之间的主要区别在于它只显示一个文件(因此是-file部分)。 Git's cat-file doesn't really stand for "concatenate"; Git 的cat-file并不真正代表“连接”; it simply is a reference to the behavior of the cat command.它只是对cat命令行为的引用。

git-cat-file - Provide content or type and size information for repository objects git-cat-file - 为存储库对象提供内容或类型和大小信息

Technically, you can use git cat-file to concatenate files, if you use Batch Output mode:从技术上讲,如果您使用批量输出模式,您可以使用git cat-file连接文件:

BATCH OUTPUT批量输出

If --batch or --batch-check is given, cat-file will read objects from stdin, one per line, and print information about them.如果--batch--batch-checkcat-file将从标准输入读取对象,每行一个,并打印有关它们的信息。 By default, the whole line is considered as an object, as if it were fed to git-rev-parse [1].默认情况下,整行被视为一个对象,就好像它被馈送到git-rev-parse [1]。

to read the content ( or blob ) of a git object读取 git 对象的内容(或 blob)

git cat-file -p <SHA1>

to read its type读取它的类型

git cat-file -t <SHA1>

The main difference between cat and Git's cat-file is that it only displays a single file (hence the -file part) cat 和 Git 的 cat-file 的主要区别在于它只显示一个文件(因此是 -file 部分)

A single file, ... or a list of single files.单个文件,...或单个文件list

In the second form, a list of objects (separated by linefeeds) is provided on stdin, and the SHA-1, type, and size of each object is printed on stdout.在第二种形式中,对象列表(由换行符分隔)在 stdin 上提供,每个对象的 SHA-1、类型和大小打印在 stdout 上。
The output format can be overridden using the optional <format> argument.可以使用可选的<format>参数覆盖输出格式。

This is important when you consider git cat-file --batch , which prints object information and contents for each object provided on stdin .当您考虑git cat-file --batch时,这一点很重要,它会打印stdin 上提供的每个对象的对象信息和内容。

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

And With Git 2.34 (Q4 2021), the " ref-filter " machinery that drives the " --format " option of " git for-each-ref " ( man ) and its friends evolves, to be used in git cat-file --batch ( man ) ".并且随着 Git 2.34(2021 年第四季度),驱动“ git for-each-refman及其朋友的“ --format ”选项的“ ref-filter ”机制不断发展,可用于git cat-file --batch “。

See commit bff9703 (01 Jul 2021) by Junio C Hamano ( gitster ) .请参阅Junio C Hamano ( gitster )提交 bff9703 (2021 年 7 月 1 日)。
See commit b9dee07 , commit e85fcb3 , commit 7121c4d , commit bd0708c , commit 311d0b8 (26 Jul 2021) by ZheNing Hu ( adlternative ) .参见胡哲宁的提交 b9dee07、提交e85fcb3提交 7121c4d提交 bd0708c提交 311d0b8 (2021 年 7 月 26 日) adlternative
(Merged by Junio C Hamano -- gitster -- in commit bda891e , 24 Aug 2021) (由Junio C Hamano -- gitster --提交 bda891e中合并,2021 年 8 月 24 日)

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

Reviewed-by: Jacob Keller审核人:雅各布·凯勒
Suggected-by: Jacob Keller推荐人:Jacob Keller
Mentored-by: Christian Couder指导者:Christian Couder
Mentored-by: Hariom Verma指导者: Hariom Verma
Signed-off-by: ZheNing Hu签字人:胡哲宁

%(rest) is a atom used for cat-file batch mode, which can split the input lines at the first whitespace boundary, all characters before that whitespace are considered to be the object name; %(rest)是用于 cat-file 批处理模式的原子,它可以在第一个空白边界处拆分输入行,该空白之前的所有字符都被认为是对象名称; characters after that first run of whitespace (ie, the "rest" of the line) are output in place of the %(rest) atom.第一次运行空格之后的字符(即,行的“其余”)将代替 %(rest) 原子输出。

In order to let " cat-file --batch=%(rest) " use the ref-filter interface, add %(rest) atom for ref-filter .为了让“ cat-file --batch=%(rest) ”使用ref-filter接口,为ref-filter添加%(rest) atom。

Introduce the reject_atom() to reject the atom %(rest) for " git for-each-ref " ( man ) , " git branch " ( man ) , " git tag " ( man ) and git verify-tag ".引入reject_atom()来拒绝 " git for-each-ref " ( man ) 、" git branch " ( man ) 、" git tag " ( man )git verify-tag " 的原子%(rest)

So both command should return the same result:所以两个命令都应该返回相同的结果:

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

basic atom: refs/tags/testtag *raw

Same for:同样适用于:

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

Note that with Git 2.36 (Q2 2022), " git cat-file --help " ( man ) is clearer.请注意,使用 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 ) . 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 )
(Merged by Junio C Hamano -- gitster -- in commit 008028a , 05 Feb 2022) (由Junio C Hamano -- gitster --提交 008028a中合并,2022 年 2 月 5 日)

cat-file : correct and improve usage information cat-file :更正和改进使用信息

Signed-off-by: Ævar Arnfjörð Bjarmason签字人:Ævar Arnfjörð Bjarmason

Change the usage output emitted on " git cat-file " ( man ) -h to group related options, making it clear to users which options go with which other ones.将“ git cat-fileman -h 上发出的使用输出更改为对相关选项进行分组,让用户清楚哪些选项与其他选项搭配。

The new output is:新的输出是:

 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'

The old usage was:旧用法是:

 <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

While shorter, I think the new one is easier to understand, as eg " --allow-unknown-type " is grouped with " -t " and " -s ", as it can only be combined with those options.虽然更短,但我认为新的更容易理解,例如“ --allow-unknown-type ”与“ -t ”和“ -s ”组合在一起,因为它只能与这些选项结合使用。
The same goes for " --buffer ", " --unordered " etc. --buffer ”、“ --unordered ”等也是如此。


Still with Git 2.36 (Q2 2022), optimize away strbuf_expand() call with a hardcoded formatting logic specific for the default format in git the --batch and --batch-check options of cat-file".仍然使用 Git 2.36(2022 年第二季度),使用特定于 git 中默认格式的硬编码格式化逻辑优化strbuf_expand()调用,即 cat-file 的--batch--batch-check选项。

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

cat-file : skip expanding default format cat-file : 跳过扩展默认格式

Signed-off-by: Ævar Arnfjörð Bjarmason签字人:Ævar Arnfjörð Bjarmason
Signed-off-by: John Cai签字人:蔡强

When format is passed into --batch , --batch-check , --batch-command , the format gets expanded.当 format 传入--batch--batch-check--batch-command时,格式会被扩展。
When nothing is passed in, the default format is set and the expand_format() gets called.当没有传入任何内容时,将设置默认格式并调用expand_format()

We can save on these cycles by hardcoding how to print the information when nothing is passed as the format, or when the default format is passed.我们可以通过硬编码在没有传递任何格式或传递默认格式时如何打印信息来节省这些周期。
There is no need for the fully expanded format with the default.不需要使用默认的完全扩展格式。
Since batch_object_write() happens on every object provided in batch mode, we get a nice performance improvement.由于batch_object_write()发生在批处理模式下提供的每个对象上,我们得到了很好的性能改进。

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

with HEAD^:带头^:

 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

with HEAD:带头:

 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

If nothing is provided as a format argument, or if the default format is passed, skip expanding of the format and print the object info with a default format.如果没有提供任何格式参数,或者如果传递了默认格式,则跳过格式扩展并使用默认格式打印对象信息。

Seethis discussion .请参阅此讨论

add to @Matoeil answer, You only need to specify 5 characters of your <SHA1> .添加到@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 

here explain very well.这里解释的很好。

git cat-file - The Cat(concatenate) command It reads data from the file and outputs the contents. git cat-file - Cat(连接)命令它从文件中读取数据并输出内容。

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

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