Skip to content

Commit 82be55c

Browse files
rhenglesruyadorno
authored andcommitted
feat(version): using 'allow-same-version', git commit --allow-empty and git tag -f
PR-URL: #648 Credit: @rhengles Close: #648 Reviewed-by: @ruyadorno
1 parent a5496c0 commit 82be55c

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

lib/version.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,30 +286,40 @@ function checkGit (localData, cb) {
286286

287287
module.exports.buildCommitArgs = buildCommitArgs
288288
function buildCommitArgs (args) {
289-
args = args || [ 'commit' ]
290-
if (!npm.config.get('commit-hooks')) args.push('-n')
291-
return args
289+
const add = []
290+
args = args || []
291+
if (args[0] === 'commit') args.shift()
292+
if (!npm.config.get('commit-hooks')) add.push('-n')
293+
if (npm.config.get('allow-same-version')) add.push('--allow-empty')
294+
return ['commit', ...add, ...args]
295+
}
296+
297+
module.exports.buildTagFlags = buildTagFlags
298+
function buildTagFlags () {
299+
return '-'.concat(
300+
npm.config.get('sign-git-tag') ? 's' : '',
301+
npm.config.get('allow-same-version') ? 'f' : '',
302+
'm'
303+
)
292304
}
293305

294306
function _commit (version, localData, cb) {
295307
const options = { env: process.env }
296308
const message = npm.config.get('message').replace(/%s/g, version)
297-
const signTag = npm.config.get('sign-git-tag')
298309
const signCommit = npm.config.get('sign-git-commit')
299310
const commitArgs = buildCommitArgs([
300311
'commit',
301312
...(signCommit ? ['-S', '-m'] : ['-m']),
302313
message
303314
])
304-
const flagForTag = signTag ? '-sm' : '-m'
305315

306316
stagePackageFiles(localData, options).then(() => {
307317
return git.exec(commitArgs, options)
308318
}).then(() => {
309319
if (!localData.existingTag) {
310320
return git.exec([
311321
'tag', npm.config.get('tag-version-prefix') + version,
312-
flagForTag, message
322+
buildTagFlags(), message
313323
], options)
314324
}
315325
}).nodeify(cb)

test/tap/version-allow-same-version.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ t.test('setup', t => {
2222

2323
t.test('without --allow-same-version', t => {
2424
npm.config.set('allow-same-version', false)
25+
26+
const version = require('../../lib/version')
27+
28+
const commit1 = version.buildCommitArgs()
29+
const commit2 = version.buildCommitArgs([ 'commit' ])
30+
const commit3 = version.buildCommitArgs([ 'commit', '-m', 'some commit message' ])
31+
32+
t.same(commit1, [ 'commit' ])
33+
t.same(commit2, [ 'commit' ])
34+
t.same(commit3, [ 'commit', '-m', 'some commit message' ])
35+
36+
const tag = version.buildTagFlags()
37+
38+
t.same(tag, '-m')
39+
2540
npm.commands.version(['0.0.1'], function (err) {
2641
t.isa(err, Error, 'got an error')
2742
t.like(err.message, /Version not changed/)
@@ -31,6 +46,21 @@ t.test('without --allow-same-version', t => {
3146

3247
t.test('with --allow-same-version', t => {
3348
npm.config.set('allow-same-version', true)
49+
50+
const version = require('../../lib/version')
51+
52+
const commit1 = version.buildCommitArgs()
53+
const commit2 = version.buildCommitArgs([ 'commit' ])
54+
const commit3 = version.buildCommitArgs([ 'commit', '-m', 'some commit message' ])
55+
56+
t.same(commit1, [ 'commit', '--allow-empty' ])
57+
t.same(commit2, [ 'commit', '--allow-empty' ])
58+
t.same(commit3, [ 'commit', '--allow-empty', '-m', 'some commit message' ])
59+
60+
const tag = version.buildTagFlags()
61+
62+
t.same(tag, '-fm')
63+
3464
npm.commands.version(['0.0.1'], function (err) {
3565
if (err) {
3666
throw err

test/tap/version-commit-hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test('npm version <semver> with commit-hooks disabled', function (t) {
3333

3434
t.same(args1, [ 'commit', '-n' ])
3535
t.same(args2, [ 'commit', '-n' ])
36-
t.same(args3, [ 'commit', '-m', 'some commit message', '-n' ])
36+
t.same(args3, [ 'commit', '-n', '-m', 'some commit message' ])
3737
t.end()
3838
})
3939
})

0 commit comments

Comments
 (0)