From 1b213dece8082fb2b2e59eff59cd83de20e4c6b7 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 2 Aug 2025 10:54:00 +0100 Subject: [PATCH] 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/ --- scripts/build-pre.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/scripts/build-pre.js b/scripts/build-pre.js index 8b2e29d8f3..4b34f6d414 100644 --- a/scripts/build-pre.js +++ b/scripts/build-pre.js @@ -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) {