Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit e9f1ad8

Browse files
ekmartiniarna
authored andcommitted
version: strip tag-version-prefix from 'from-git' target tag
PR-URL: #10717 Credit: @ekmartin
1 parent 9ed2849 commit e9f1ad8

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

lib/version.js

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ function parseLastGitTag (cb) {
7373
}
7474

7575
var tag = stdout.trim()
76+
var prefix = npm.config.get('tag-version-prefix')
77+
// Strip the prefix from the start of the tag:
78+
if (tag.indexOf(prefix) === 0) tag = tag.slice(prefix.length)
7679
var version = semver.valid(tag)
7780
if (!version) return cb(new Error(tag + ' is not a valid version'))
7881
cb(null, version)

test/tap/version-from-git.js

+68-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ var cache = path.resolve(pkg, 'cache')
1515

1616
var json = { name: 'cat', version: '0.1.2' }
1717

18-
test('npm version from-git with a valid tag creates new commit', function (t) {
18+
test('npm version from-git with a valid tag creates a new commit', function (t) {
1919
var version = '1.2.3'
20-
var tag = 'v' + version
2120
setup()
22-
createTag(t, tag, runVersion)
21+
createTag(t, version, runVersion)
2322

2423
function runVersion (er) {
2524
t.ifError(er, 'git tag ran without error')
@@ -40,16 +39,15 @@ test('npm version from-git with a valid tag creates new commit', function (t) {
4039
function checkCommit (er, log, stderr) {
4140
t.ifError(er, 'git log ran without issue')
4241
t.notOk(stderr, 'no error output')
43-
t.ok(log.indexOf(version) !== -1, 'commited from subdirectory')
42+
t.ok(log.indexOf(version) !== -1, 'commit was created')
4443
t.end()
4544
}
4645
})
4746

4847
test('npm version from-git with a valid tag updates the package.json version', function (t) {
4948
var version = '1.2.3'
50-
var tag = 'v' + version
5149
setup()
52-
createTag(t, tag, runVersion)
50+
createTag(t, version, runVersion)
5351

5452
function runVersion (er) {
5553
t.ifError(er, 'git tag ran without error')
@@ -68,6 +66,70 @@ test('npm version from-git with a valid tag updates the package.json version', f
6866
}
6967
})
7068

69+
test('npm version from-git strips tag-version-prefix', function (t) {
70+
var version = '1.2.3'
71+
var prefix = 'custom-'
72+
var tag = prefix + version
73+
setup()
74+
createTag(t, tag, runVersion)
75+
76+
function runVersion (er) {
77+
t.ifError(er, 'git tag ran without error')
78+
npm.config.set('sign-git-tag', false)
79+
npm.config.set('tag-version-prefix', prefix)
80+
npm.commands.version(['from-git'], checkVersion)
81+
}
82+
83+
function checkVersion (er) {
84+
var git = require('../../lib/utils/git.js')
85+
t.ifError(er, 'version command ran without error')
86+
git.whichAndExec(
87+
['log', '--pretty=medium'],
88+
{ cwd: pkg, env: process.env },
89+
checkCommit
90+
)
91+
}
92+
93+
function checkCommit (er, log, stderr) {
94+
t.ifError(er, 'git log ran without issue')
95+
t.notOk(stderr, 'no error output')
96+
t.ok(log.indexOf(tag) === -1, 'commit should not include prefix')
97+
t.ok(log.indexOf(version) !== -1, 'commit should include version')
98+
t.end()
99+
}
100+
})
101+
102+
test('npm version from-git only strips tag-version-prefix if it is a prefix', function (t) {
103+
var prefix = 'test'
104+
var version = '1.2.3-' + prefix
105+
setup()
106+
createTag(t, version, runVersion)
107+
108+
function runVersion (er) {
109+
t.ifError(er, 'git tag ran without error')
110+
npm.config.set('sign-git-tag', false)
111+
npm.config.set('tag-version-prefix', prefix)
112+
npm.commands.version(['from-git'], checkVersion)
113+
}
114+
115+
function checkVersion (er) {
116+
var git = require('../../lib/utils/git.js')
117+
t.ifError(er, 'version command ran without error')
118+
git.whichAndExec(
119+
['log'],
120+
{ cwd: pkg, env: process.env },
121+
checkCommit
122+
)
123+
}
124+
125+
function checkCommit (er, log, stderr) {
126+
t.ifError(er, 'git log ran without issue')
127+
t.notOk(stderr, 'no error output')
128+
t.ok(log.indexOf(version) !== -1, 'commit should include the full version')
129+
t.end()
130+
}
131+
})
132+
71133
test('npm version from-git with an existing version', function (t) {
72134
var tag = 'v' + json.version
73135
setup()

0 commit comments

Comments
 (0)