Skip to content

Commit df5a94a

Browse files
authored
Merge pull request #5 from posos-tech/master
feat(tag): add an option to force tag replacement
2 parents 1974279 + e284dde commit df5a94a

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ If you do not want to have any tag prefix you can use the `-t` flag and provide
324324

325325
> Note: simply -t or --tag-prefix without any value will fallback to the default 'v'
326326
327+
### Tag replacement
328+
329+
If you've already run `standard-version` when creating your release, you may want to alter the release content and changelog without bumping the version, by using `standard-version --skip.bump`. By default, tagging with an already existing tag make `git` fails. You can add the `--tag-force` flag to make use of `-f` option when calling `git tag`, then the existing version tag will be replaced.
330+
327331
### CLI Help
328332

329333
```sh

command.js

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ const yargs = require('yargs')
7373
type: 'string',
7474
default: defaults.tagPrefix
7575
})
76+
.option('tag-force', {
77+
describe: 'Allow tag replacement',
78+
type: 'boolean',
79+
default: defaults.tagForce
80+
})
7681
.option('scripts', {
7782
describe:
7883
'Provide scripts to execute for lifecycle events (prebump, precommit, etc.,)',

defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const defaults = {
1111
scripts: {},
1212
skip: {},
1313
dryRun: false,
14+
tagForce: false,
1415
gitTagFallback: true,
1516
preset: require.resolve('conventional-changelog-conventionalcommits')
1617
}

lib/lifecycles/tag.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ module.exports = async function (newVersion, pkgPrivate, args) {
1414
}
1515

1616
async function execTag (newVersion, pkgPrivate, args) {
17-
let tagOption
17+
const tagOption = []
1818
if (args.sign) {
19-
tagOption = '-s'
19+
tagOption.push('-s')
2020
} else {
21-
tagOption = '-a'
21+
tagOption.push('-a')
22+
}
23+
if (args.tagForce) {
24+
tagOption.push('-f')
2225
}
2326
checkpoint(args, 'tagging release %s%s', [args.tagPrefix, newVersion])
24-
await runExecFile(args, 'git', ['tag', tagOption, args.tagPrefix + newVersion, '-m', `${formatCommitMessage(args.releaseCommitMessageFormat, newVersion)}`])
27+
await runExecFile(args, 'git', ['tag', ...tagOption, args.tagPrefix + newVersion, '-m', `${formatCommitMessage(args.releaseCommitMessageFormat, newVersion)}`])
2528
const currentBranch = await runExecFile('', 'git', ['rev-parse', '--abbrev-ref', 'HEAD'])
2629
let message = 'git push --follow-tags origin ' + currentBranch.trim()
2730
if (pkgPrivate !== true && bump.getUpdatedConfigs()['package.json']) {

test/core.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,26 @@ describe('with mocked git', function () {
775775
gitArgs.should.have.lengthOf(0)
776776
})
777777

778+
it('--tag-force forces tag replacement', async function () {
779+
const gitArgs = [
780+
['add', 'CHANGELOG.md', 'package.json'],
781+
['commit', 'CHANGELOG.md', 'package.json', '-m', 'chore(release): 1.0.1'],
782+
['tag', '-a', '-f', 'v1.0.1', '-m', 'chore(release): 1.0.1'],
783+
['rev-parse', '--abbrev-ref', 'HEAD']
784+
]
785+
const execFile = (_args, cmd, cmdArgs) => {
786+
cmd.should.equal('git')
787+
const expected = gitArgs.shift()
788+
cmdArgs.should.deep.equal(expected)
789+
if (expected[0] === 'rev-parse') return Promise.resolve('master')
790+
return Promise.resolve('')
791+
}
792+
mock({ bump: 'patch', changelog: 'foo\n', execFile })
793+
794+
await exec('--tag-force', true)
795+
gitArgs.should.have.lengthOf(0)
796+
})
797+
778798
it('fails if git add fails', async function () {
779799
const gitArgs = [['add', 'CHANGELOG.md', 'package.json']]
780800
const execFile = (_args, cmd, cmdArgs) => {

test/git.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ describe('git', function () {
193193
await exec('-n')
194194
})
195195

196+
it('replaces tags if version not bumped', async function () {
197+
mock({ bump: 'minor', tags: ['v1.0.0'] })
198+
await exec({})
199+
shell.exec('git describe').stdout.should.match(/v1\.1\.0/)
200+
await exec('--tag-force --skip.bump')
201+
shell.exec('git describe').stdout.should.match(/v1\.1\.0/)
202+
})
203+
196204
it('allows the commit phase to be skipped', async function () {
197205
const changelogContent = 'legacy header format<a name="1.0.0">\n'
198206
writePackageJson('1.0.0')

0 commit comments

Comments
 (0)