From 9dd0605356f65a438759d53306ae182742e4641e Mon Sep 17 00:00:00 2001 From: Rafael Hengles Date: Wed, 1 Jan 2020 12:32:02 -0200 Subject: [PATCH 1/2] feat(version): using 'allow-same-version', git commit --allow-empty and git tag -f --- lib/version.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/version.js b/lib/version.js index a8c2a648c7473..570039b60966a 100644 --- a/lib/version.js +++ b/lib/version.js @@ -287,7 +287,8 @@ function checkGit (localData, cb) { module.exports.buildCommitArgs = buildCommitArgs function buildCommitArgs (args) { args = args || [ 'commit' ] - if (!npm.config.get('commit-hooks')) args.push('-n') + if (!npm.config.get('commit-hooks')) args.push('--no-verify') + if (npm.config.get('allow-same-version')) args.push('--allow-empty') return args } @@ -296,12 +297,16 @@ function _commit (version, localData, cb) { const message = npm.config.get('message').replace(/%s/g, version) const signTag = npm.config.get('sign-git-tag') const signCommit = npm.config.get('sign-git-commit') - const commitArgs = buildCommitArgs([ - 'commit', + const commitArgs = [ + ...buildCommitArgs(['commit']), ...(signCommit ? ['-S', '-m'] : ['-m']), message - ]) - const flagForTag = signTag ? '-sm' : '-m' + ] + const flagForTag = '-'.concat( + signTag ? 's' : '', + npm.config.get('allow-same-version') ? 'f' : '', + 'm' + ) stagePackageFiles(localData, options).then(() => { return git.exec(commitArgs, options) From 17f082f354157b125442bc93d0e4b965bb6ba44e Mon Sep 17 00:00:00 2001 From: Rafael Hengles Date: Tue, 7 Jan 2020 14:41:36 -0300 Subject: [PATCH 2/2] refactor and tests --- lib/version.js | 33 +++++++++++++++----------- test/tap/version-allow-same-version.js | 30 +++++++++++++++++++++++ test/tap/version-commit-hooks.js | 2 +- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/lib/version.js b/lib/version.js index 570039b60966a..6619a8ba9d0d7 100644 --- a/lib/version.js +++ b/lib/version.js @@ -286,27 +286,32 @@ function checkGit (localData, cb) { module.exports.buildCommitArgs = buildCommitArgs function buildCommitArgs (args) { - args = args || [ 'commit' ] - if (!npm.config.get('commit-hooks')) args.push('--no-verify') - if (npm.config.get('allow-same-version')) args.push('--allow-empty') - return args + const add = [] + args = args || [] + if (args[0] === 'commit') args.shift() + if (!npm.config.get('commit-hooks')) add.push('-n') + if (npm.config.get('allow-same-version')) add.push('--allow-empty') + return ['commit', ...add, ...args] +} + +module.exports.buildTagFlags = buildTagFlags +function buildTagFlags () { + return '-'.concat( + npm.config.get('sign-git-tag') ? 's' : '', + npm.config.get('allow-same-version') ? 'f' : '', + 'm' + ) } function _commit (version, localData, cb) { const options = { env: process.env } const message = npm.config.get('message').replace(/%s/g, version) - const signTag = npm.config.get('sign-git-tag') const signCommit = npm.config.get('sign-git-commit') - const commitArgs = [ - ...buildCommitArgs(['commit']), + const commitArgs = buildCommitArgs([ + 'commit', ...(signCommit ? ['-S', '-m'] : ['-m']), message - ] - const flagForTag = '-'.concat( - signTag ? 's' : '', - npm.config.get('allow-same-version') ? 'f' : '', - 'm' - ) + ]) stagePackageFiles(localData, options).then(() => { return git.exec(commitArgs, options) @@ -314,7 +319,7 @@ function _commit (version, localData, cb) { if (!localData.existingTag) { return git.exec([ 'tag', npm.config.get('tag-version-prefix') + version, - flagForTag, message + buildTagFlags(), message ], options) } }).nodeify(cb) diff --git a/test/tap/version-allow-same-version.js b/test/tap/version-allow-same-version.js index 41310e4eb2e27..14506aac7d928 100644 --- a/test/tap/version-allow-same-version.js +++ b/test/tap/version-allow-same-version.js @@ -22,6 +22,21 @@ t.test('setup', t => { t.test('without --allow-same-version', t => { npm.config.set('allow-same-version', false) + + const version = require('../../lib/version') + + const commit1 = version.buildCommitArgs() + const commit2 = version.buildCommitArgs([ 'commit' ]) + const commit3 = version.buildCommitArgs([ 'commit', '-m', 'some commit message' ]) + + t.same(commit1, [ 'commit' ]) + t.same(commit2, [ 'commit' ]) + t.same(commit3, [ 'commit', '-m', 'some commit message' ]) + + const tag = version.buildTagFlags() + + t.same(tag, '-m') + npm.commands.version(['0.0.1'], function (err) { t.isa(err, Error, 'got an error') t.like(err.message, /Version not changed/) @@ -31,6 +46,21 @@ t.test('without --allow-same-version', t => { t.test('with --allow-same-version', t => { npm.config.set('allow-same-version', true) + + const version = require('../../lib/version') + + const commit1 = version.buildCommitArgs() + const commit2 = version.buildCommitArgs([ 'commit' ]) + const commit3 = version.buildCommitArgs([ 'commit', '-m', 'some commit message' ]) + + t.same(commit1, [ 'commit', '--allow-empty' ]) + t.same(commit2, [ 'commit', '--allow-empty' ]) + t.same(commit3, [ 'commit', '--allow-empty', '-m', 'some commit message' ]) + + const tag = version.buildTagFlags() + + t.same(tag, '-fm') + npm.commands.version(['0.0.1'], function (err) { if (err) { throw err diff --git a/test/tap/version-commit-hooks.js b/test/tap/version-commit-hooks.js index 028767ab4e5ba..36694d7eae959 100644 --- a/test/tap/version-commit-hooks.js +++ b/test/tap/version-commit-hooks.js @@ -33,7 +33,7 @@ test('npm version with commit-hooks disabled', function (t) { t.same(args1, [ 'commit', '-n' ]) t.same(args2, [ 'commit', '-n' ]) - t.same(args3, [ 'commit', '-m', 'some commit message', '-n' ]) + t.same(args3, [ 'commit', '-n', '-m', 'some commit message' ]) t.end() }) })