From 82be55cfaab739f9e51e37db8a22096209f750c9 Mon Sep 17 00:00:00 2001 From: Rafael Hengles Date: Wed, 1 Jan 2020 12:32:02 -0200 Subject: [PATCH] feat(version): using 'allow-same-version', git commit --allow-empty and git tag -f PR-URL: https://github.com/npm/cli/pull/648 Credit: @rhengles Close: #648 Reviewed-by: @ruyadorno --- lib/version.js | 22 +++++++++++++------ test/tap/version-allow-same-version.js | 30 ++++++++++++++++++++++++++ test/tap/version-commit-hooks.js | 2 +- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/lib/version.js b/lib/version.js index a8c2a648c7473..6619a8ba9d0d7 100644 --- a/lib/version.js +++ b/lib/version.js @@ -286,22 +286,32 @@ function checkGit (localData, cb) { module.exports.buildCommitArgs = buildCommitArgs function buildCommitArgs (args) { - args = args || [ 'commit' ] - if (!npm.config.get('commit-hooks')) args.push('-n') - 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', ...(signCommit ? ['-S', '-m'] : ['-m']), message ]) - const flagForTag = signTag ? '-sm' : '-m' stagePackageFiles(localData, options).then(() => { return git.exec(commitArgs, options) @@ -309,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() }) })