From 772e39b710a6827632162d44e78aebddcfd113bc Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 8 Sep 2019 17:54:41 +0100 Subject: [PATCH] Tools: Improved git-changelog so that it is less error prone --- Tools/git-changelog.js | 41 +++++++++++++++++++++++++++++++++++++---- Tools/release-cli.js | 12 +----------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Tools/git-changelog.js b/Tools/git-changelog.js index 295a2c2314..7d6f79dae6 100644 --- a/Tools/git-changelog.js +++ b/Tools/git-changelog.js @@ -175,14 +175,47 @@ function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } +function decreaseTagVersion(tag) { + const s = tag.split('.'); + let num = Number(s.pop()); + num--; + if (num < 0) throw new Error('Cannot decrease tag version: ' + tag); + s.push('' + num); + return s.join('.'); +} + +// This function finds the first relevant tag starting from the given tag. +// The first "relevant tag" is the one that exists, and from which there are changes. +async function findFirstRelevantTag(baseTag) { + let tag = decreaseTagVersion(baseTag); + while (true) { + try { + const logs = await gitLog(tag); + if (logs.length) return tag; + } catch (error) { + if (error.message.indexOf('unknown revision') >= 0) { + // We skip the error - it means this particular tag has never been created + } else { + throw error; + } + } + + tag = decreaseTagVersion(tag); + } +} + async function main() { const argv = require('yargs').argv; - if (!argv._.length) throw new Error('Tag name must be specified'); + if (!argv._.length) throw new Error('Tag name must be specified. Provide the tag of the new version and git-changelog will walk backward to find the changes to the previous relevant tag.'); - const sinceTagName = argv._[0]; - const platform = platformFromTag(sinceTagName); + const fromTagName = argv._[0]; + + const platform = platformFromTag(fromTagName); + + const toTagName = await findFirstRelevantTag(fromTagName); + + const logsSinceTags = await gitLog(toTagName); - const logsSinceTags = await gitLog(sinceTagName); const filteredLogs = filterLogs(logsSinceTags, platform); let changelog = createChangeLog(filteredLogs); diff --git a/Tools/release-cli.js b/Tools/release-cli.js index 39cd71f8ff..c6eb5f8cde 100644 --- a/Tools/release-cli.js +++ b/Tools/release-cli.js @@ -50,18 +50,8 @@ async function insertChangelog(tag, changelog) { // Start with node Tools/release-cli.js --changelog-from cli-v1.0.126 // to specify from where the changelog should be created async function main() { - const argv = require('yargs').argv; - process.chdir(appDir); - const packageJson = await fs.readFile('package.json', 'UTF-8'); - const packageConf = JSON.parse(packageJson); - - const previousVersion = 'v' + packageConf.version; - let changelogFrom = 'cli-' + previousVersion; - - if (argv.changelogFrom) changelogFrom = argv.changelogFrom; - const newVersion = await execCommand('npm version patch'); console.info('Building ' + newVersion + '...'); const newTag = 'cli-' + newVersion; @@ -75,7 +65,7 @@ async function main() { await execCommand('npm publish'); - const changelog = await execCommand('node ' + rootDir + '/Tools/git-changelog ' + changelogFrom); + const changelog = await execCommand('node ' + rootDir + '/Tools/git-changelog ' + newTag); const newChangelog = await insertChangelog(newTag, changelog);