简体   繁体   English

无需“git”命令即可获取 git 提交详细信息

[英]Get git commit details without "git" command

First, I've seen and read Git commit date .首先,我看过并阅读过Git commit date

How can I get the date of the current HEAD commit in git repo without execution any "git" command?如何在执行任何“git”命令的情况下获取 git repo 中当前 HEAD 提交的日期? Example: I'm execution commands from PHP and I cannot call exec , but I want to get current commit details.示例:我是来自 PHP 的执行命令,我无法调用exec ,但我想获取当前的提交详细信息。

I have access to.git folder of this repo, but it doesn't seem to be useful for commit details.我可以访问此 repo 的 .git 文件夹,但它似乎对提交详细信息没有用。 I was only able to get:我只能得到:

  • Current branch from .git/HEAD来自.git/HEAD的当前分支
  • Current commit id (SHA) from .git/refs/heads/<branch>来自.git/refs/heads/<branch>的当前提交 ID (SHA)

But no commit message and no commit date.但是没有提交消息,也没有提交日期。 Is there any way to get it from the fileset?有没有办法从文件集中获取它?

You can't.你不能。

Well, you might be able to, but there are too many complex issues.好吧,你也许可以,但是有太多复杂的问题。

Specifically, each Git object is stored in a database of all objects, indexed by hash-ID keys.具体来说,每个 Git对象都存储在所有对象的数据库中,由哈希 ID 键索引。 Some objects are loose: stored as separate files.有些对象是松散的:存储为单独的文件。 Such a file is zlib-deflated.这样的文件是 zlib-deflated。 Any zlib-inflator can read this file and turn the compressed data into readable, useful data, and what you will see if you do this with a commit hash ID is the actual commit object content:任何 zlib-inflator 都可以读取此文件并将压缩数据转换为可读、有用的数据,如果使用提交哈希 ID 执行此操作,您将看到的是实际的提交对象内容:

$ git cat-file -p HEAD | sed 's/@/ /'
tree 1fd4a47af4942cbdee0bdcb4375612ab521a4a51
parent 5571d085b3c9c2aa9470a10bcf2b8518d3e4ec99
author Junio C Hamano <gitster pobox.com> 1531941857 -0700
committer Junio C Hamano <gitster pobox.com> 1531941857 -0700

Third batch for 2.19 cycle

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

(Here I have used a Git command to do the job, which violates your rule.) The dates are those two time stamps on the author and committer lines. (这里我使用了 Git 命令来完成这项工作,这违反了你的规则。)日期是authorcommitter者行上的那两个时间戳。 You can simply decode them however you like, though remember to use the time zone offset, -0700 here, as well.您可以根据需要简单地解码它们,但请记住在这里也使用时区偏移量-0700 (The internal content is prefixed with a header, in this case commit <size>\0 , as all Git objects have headers.) (内部内容以标题为前缀,在本例中为commit <size>\0 ,因为所有 Git 对象都有标题。)

But loose is not the only way objects get stored.松散并不是对象存储的唯一方式。 Once an object has been lying around loose for a while, Git will eventually pack the object into a pack file .一旦一个对象散落了一段时间,Git 最终会将对象打包到一个打包文件中。 The format of pack files is quite complicated.打包文件的格式相当复杂。 It is documented—see the Git technical documentation file —but it's subject to change and not worth coding when git cat-file -p already does all the work for you.记录在案——请参阅Git 技术文档文件——但它可能会发生变化,并且当git cat-file -p已经为您完成所有工作时不值得编码。

So, just use a Git command.所以,只需使用 Git 命令。 This does mean you need Git installed, but you probably need that anyway.这确实意味着您需要安装 Git,但无论如何您可能都需要它。

Can't you do something like this.你不能做这样的事情吗? ORS is the line separator just in case you have multiple rows in the file. ORS 是行分隔符,以防文件中有多行。

awk 'BEGIN { ORS=" " }; awk 'BEGIN { ORS=" " }; { print $1 }'./.git/FETCH_HEAD { 打印 $1 }'./.git/FETCH_HEAD

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

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