From eea73a6694039438978b24c6ec2a94c8d8380e58 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 24 Dec 2019 18:30:14 -0500 Subject: [PATCH] Use describe to determine last tag (MAJOR) --- dist/index.js | 30 ++++++++++++++++++++---------- index.js | 27 +++++++++++++++++---------- index.test.js | 22 +++++++++++++++++++++- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/dist/index.js b/dist/index.js index 4fb52c0..f1cad44 100644 --- a/dist/index.js +++ b/dist/index.js @@ -661,6 +661,7 @@ const setOutput = (major, minor, patch, increment) => { .replace('${increment}', increment); core.info(`Version is ${major}.${minor}.${patch}+${increment}`); + core.info(`To create a release for this `) core.setOutput("version", version); core.setOutput("major", major.toString()); core.setOutput("minor", minor.toString()); @@ -679,7 +680,7 @@ async function run() { const majorPattern = core.getInput('major_pattern', { required: true }); const minorPattern = core.getInput('minor_pattern', { required: true }); - const releasePattern = `refs/tags/${tagPrefix}*`; + const releasePattern = `${tagPrefix}*`; let major = 0, minor = 0, patch = 0, increment = 0; let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim(); @@ -690,19 +691,28 @@ async function run() { return; } - let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + //let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); - let tag = (await cmd( - 'git', - `for-each-ref`, - `--format='%(refname:short)'`, - `--sort=-committerdate`, - `--no-contains`, commit, - releasePattern - )).split(eol)[0].trim().replace(/'/g, ""); + let tag = ''; + try { + tag = (await cmd( + 'git', + `describe`, + `--tags`, + `--abbrev=0`, + `--match=${releasePattern}`, + `${branch}~1` + )).trim(); + } + catch (err) { + tag = ''; + } let root; if (tag === '') { + if (remoteExists) { + core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); + } // no release tags yet, use the initial commit as the root root = ''; } else { diff --git a/index.js b/index.js index 321e311..7e847d7 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,7 @@ const setOutput = (major, minor, patch, increment) => { .replace('${increment}', increment); core.info(`Version is ${major}.${minor}.${patch}+${increment}`); + core.info(`To create a release for this `) core.setOutput("version", version); core.setOutput("major", major.toString()); core.setOutput("minor", minor.toString()); @@ -43,7 +44,7 @@ async function run() { const majorPattern = core.getInput('major_pattern', { required: true }); const minorPattern = core.getInput('minor_pattern', { required: true }); - const releasePattern = `refs/tags/${tagPrefix}*`; + const releasePattern = `${tagPrefix}*`; let major = 0, minor = 0, patch = 0, increment = 0; let lastCommitAll = (await cmd('git', 'rev-list', '-n1', '--all')).trim(); @@ -54,16 +55,22 @@ async function run() { return; } - let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + //let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); - let tag = (await cmd( - 'git', - `for-each-ref`, - `--format='%(refname:short)'`, - `--sort=-committerdate`, - `--no-contains`, commit, - releasePattern - )).split(eol)[0].trim().replace(/'/g, ""); + let tag = ''; + try { + tag = (await cmd( + 'git', + `describe`, + `--tags`, + `--abbrev=0`, + `--match=${releasePattern}`, + `${branch}~1` + )).trim(); + } + catch (err) { + tag = ''; + } let root; if (tag === '') { diff --git a/index.test.js b/index.test.js index 8fdae2d..533cbd2 100644 --- a/index.test.js +++ b/index.test.js @@ -170,7 +170,7 @@ test('Version pulled from last release branch', () => { repo.clean(); }); - +/* Removed for now test('Tags on branches are used', () => { // This test checks that tags are counted correctly even if they are not on @@ -196,6 +196,7 @@ test('Tags on branches are used', () => { repo.clean(); }); +*/ test('Merged tags do not affect version', () => { @@ -265,3 +266,22 @@ test('Version prefixes are not required/can be empty', () => { repo.clean(); }); + +test('Tag order comes from commit order, not tag create order', () => { + const repo = createTestRepo(); // 0.0.0+0 + + repo.makeCommit('Initial Commit'); // 0.0.1+0 + repo.makeCommit('Second Commit'); // 0.0.1+1 + repo.makeCommit('Third Commit'); // 0.0.1+2 + repo.exec('git tag v2.0.0'); + repo.exec('sleep 2'); + repo.exec('git tag v1.0.0 HEAD~1'); + repo.makeCommit('Fourth Commit'); // 0.0.1+2 + + const result = repo.runAction(); + + + expect(result).toMatch('Version is 2.0.1+0'); + + repo.clean(); +}); \ No newline at end of file