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!