Skip to content

Commit

Permalink
feat(version): using 'allow-same-version', git commit --allow-empty a…
Browse files Browse the repository at this point in the history
…nd git tag -f

PR-URL: #648
Credit: @rhengles
Close: #648
Reviewed-by: @ruyadorno
  • Loading branch information
rhengles authored and ruyadorno committed Jan 9, 2020
1 parent e0a7a2f commit 4b30f3c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,40 @@ 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)
}).then(() => {
if (!localData.existingTag) {
return git.exec([
'tag', npm.config.get('tag-version-prefix') + version,
flagForTag, message
buildTagFlags(), message
], options)
}
}).nodeify(cb)
Expand Down
30 changes: 30 additions & 0 deletions test/tap/version-allow-same-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/tap/version-commit-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('npm version <semver> 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()
})
})
Expand Down

0 comments on commit 4b30f3c

Please sign in to comment.