0

First, I've seen and read Git commit date.

How can I get the date of the current HEAD commit in git repo without execution any "git" command? Example: I'm execution commands from PHP and I cannot call exec, but I want to get current commit details.

I have access to .git folder of this repo, but it doesn't seem to be useful for commit details. I was only able to get:

  • Current branch from .git/HEAD
  • Current commit id (SHA) from .git/refs/heads/<branch>

But no commit message and no commit date. Is there any way to get it from the fileset?

rubenvb
  • 74,642
  • 33
  • 187
  • 332
The Godfather
  • 4,235
  • 4
  • 39
  • 61
  • 2
    How about using a PHP library to access the git repository? I'm sure libgit2 will have some PHP bindings. – rubenvb Aug 13 '18 at 16:24

2 Answers2

5

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. Some objects are loose: stored as separate files. Such a file is 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:

$ 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. You can simply decode them however you like, though remember to use the time zone offset, -0700 here, as well. (The internal content is prefixed with a header, in this case commit <size>\0, as all Git objects have headers.)

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. 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.

So, just use a Git command. This does mean you need Git installed, but you probably need that anyway.

torek
  • 448,244
  • 59
  • 642
  • 775
0

Can't you do something like this. ORS is the line separator just in case you have multiple rows in the file.

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

farman
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '22 at 18:11