Reputation: 175
I want to print the latest commit hash in my Node.js application.
I found a similar discussion here, but it's not what I have in mind. I want to read this information from within Node.js, without running git
commands in a terminal.
So if possible, I want to read the .git
folder with fs
, find the latest commit hash and write it to a variable. This file looks relevant: .git/logs/refs/heads/master
.
Can I simply read the last non-empty line of this file and parse the commit hash from there? Any pitfalls I should look for? Any better ways? It's safe to assume I have only one branch (master)
Thanks for any help!
Upvotes: -1
Views: 490
Reputation: 94726
To get the hash for the current (pointed to by the HEAD
) commit:
git rev-parse HEAD
To get the hash for the last commit on master
branch (in case you moved HEAD
to a different commit using git checkout
or git switch
):
git rev-parse master
Upvotes: 1