better git version info

My previous patch to embed git version info in nodeinfo had some
issues, like it produced `2024.4.4-249-g04d45859e6` when building on
current `develop`, which is arguably correct but not helpful (it
should really say `2025.5.2-dev` somewhere!)

* if we're on a tag, trust `package.json`
* if we're not, *append* the current commit hash

so for example, building on `stable` right now gets you `2025.4.4`,
and building on the parent of this commit gets you
`2025.5.2-dev+g04d45859e6`

the `+` is correct according to https://semver.org/
This commit is contained in:
dakkar 2025-08-02 10:54:00 +01:00
parent 04d45859e6
commit 1b213dece8

View file

@ -7,20 +7,35 @@ const fs = require('fs');
const packageJsonPath = __dirname + '/../package.json'
const { execFileSync } = require('node:child_process');
function build() {
let gitVersion;
try {
gitVersion = execFileSync('git', ['describe', '--tags'], {
encoding: 'utf-8',
});
gitVersion = gitVersion.trim();
} catch (e) {
console.warn("couldn't get git commit details, ignoring",e);
function callGit(args) {
return execFileSync('git', args, {
encoding: 'utf-8',
}).trim();
}
function getGitVersion(versionFromPackageJson) {
const thisTag = callGit(['tag', '--points-at', 'HEAD']);
if (thisTag) {
// we're building from a tag, we don't care about extra details
return null;
}
const commitId = callGit(['rev-parse', '--short', 'HEAD']);
return `${versionFromPackageJson}+g${commitId}`;
}
function build() {
try {
const json = fs.readFileSync(packageJsonPath, 'utf-8')
const meta = JSON.parse(json);
let gitVersion;
try {
gitVersion = getGitVersion(meta.version);
} catch (e) {
console.warn("couldn't get git commit details, ignoring",e);
}
fs.mkdirSync(__dirname + '/../built', { recursive: true });
fs.writeFileSync(__dirname + '/../built/meta.json', JSON.stringify({ version: meta.version, gitVersion }), 'utf-8');
} catch (e) {