git获取当前分支名,hash值的方式参考这篇git 获取当前分支,当前hash值。
在nodejs中使用需要使用execa
这个库帮助我们执行git命令,可以封装两个方法:
function getGitHash() {
const res = execa.commandSync('git rev-parse --short HEAD');
return res.stdout;
}
function getGitBranch() {
const res = execa.commandSync('git rev-parse --abbrev-ref HEAD');
return res.stdout;
}