diff --git a/index.js b/index.js index 9bad0aca..0a7b7ede 100644 --- a/index.js +++ b/index.js @@ -108,7 +108,7 @@ async function run(context, plugins) { nextRelease.notes = await plugins.generateNotes({...context, commits, lastRelease, nextRelease}); logger.log('Create tag %s', nextRelease.gitTag); - await tag(nextRelease.gitTag, nextRelease.gitHead, {cwd, env}); + await tag(nextRelease, {cwd, env}); await push(options.repositoryUrl, {cwd, env}); context.branch.tags.push({ version: nextRelease.version, @@ -174,7 +174,7 @@ async function run(context, plugins) { logger.warn(`Skip ${nextRelease.gitTag} tag creation in dry-run mode`); } else { // Create the tag before calling the publish plugins as some require the tag to exists - await tag(nextRelease.gitTag, nextRelease.gitHead, {cwd, env}); + await tag(nextRelease, {cwd, env}); await push(options.repositoryUrl, {cwd, env}); logger.success(`Created tag ${nextRelease.gitTag}`); } diff --git a/lib/definitions/constants.js b/lib/definitions/constants.js index 491a1a71..9de9d957 100644 --- a/lib/definitions/constants.js +++ b/lib/definitions/constants.js @@ -14,6 +14,8 @@ const SECRET_REPLACEMENT = '[secure]'; const SECRET_MIN_SIZE = 5; +const TAG_MESSAGE_FORMAT = `release \${version}`; + module.exports = { RELEASE_TYPE, FIRST_RELEASE, @@ -23,4 +25,5 @@ module.exports = { RELEASE_NOTES_SEPARATOR, SECRET_REPLACEMENT, SECRET_MIN_SIZE, + TAG_MESSAGE_FORMAT, }; diff --git a/lib/git.js b/lib/git.js index 4f53ed78..ef7ac9e2 100644 --- a/lib/git.js +++ b/lib/git.js @@ -1,8 +1,9 @@ -const {matches, pick, memoize} = require('lodash'); +const {matches, pick, memoize, template} = require('lodash'); const gitLogParser = require('git-log-parser'); const getStream = require('get-stream'); const execa = require('execa'); const debug = require('debug')('semantic-release:git'); +const {TAG_MESSAGE_FORMAT} = require('./definitions/constants'); Object.assign(gitLogParser.fields, {hash: 'H', message: 'B', gitTags: 'd', committerDate: {key: 'ci', type: Date}}); @@ -222,14 +223,17 @@ async function verifyAuth(repositoryUrl, branch, execaOpts) { /** * Tag the commit head on the local repository. * - * @param {String} tagName The name of the tag. - * @param {String} ref The Git reference to tag. + * @param {Object} release The release associated with the tag. * @param {Object} [execaOpts] Options to pass to `execa`. * * @throws {Error} if the tag creation failed. */ -async function tag(tagName, ref, execaOpts) { - await execa('git', ['tag', tagName, ref], execaOpts); +async function tag(release, execaOpts) { + await execa( + 'git', + ['tag', release.gitTag, release.gitHead, '-a', '-m', template(TAG_MESSAGE_FORMAT)(release)], + execaOpts + ); } /** diff --git a/test/git.test.js b/test/git.test.js index d804966d..994a68dc 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -183,7 +183,7 @@ test('Add tag on head commit', async t => { const {cwd} = await gitRepo(); const commits = await gitCommits(['Test commit'], {cwd}); - await tag('tag_name', 'HEAD', {cwd}); + await tag({gitTag: 'tag_name', gitHead: 'HEAD', version: '1.0.0'}, {cwd}); await t.is(await gitCommitTag(commits[0].hash, {cwd}), 'tag_name'); }); @@ -193,7 +193,7 @@ test('Push tag to remote repository', async t => { const {cwd, repositoryUrl} = await gitRepo(true); const commits = await gitCommits(['Test commit'], {cwd}); - await tag('tag_name', 'HEAD', {cwd}); + await tag({gitTag: 'tag_name', gitHead: 'HEAD', version: '1.0.0'}, {cwd}); await push(repositoryUrl, {cwd}); t.is(await gitRemoteTagHead(repositoryUrl, 'tag_name', {cwd}), commits[0].hash); @@ -207,7 +207,7 @@ test('Push tag to remote repository with remote branch ahead', async t => { await gitCommits(['Second'], {cwd: tmpRepo}); await gitPush('origin', 'master', {cwd: tmpRepo}); - await tag('tag_name', 'HEAD', {cwd}); + await tag({gitTag: 'tag_name', gitHead: 'HEAD', version: '1.0.0'}, {cwd}); await push(repositoryUrl, {cwd}); t.is(await gitRemoteTagHead(repositoryUrl, 'tag_name', {cwd}), commits[0].hash); diff --git a/test/helpers/git-utils.js b/test/helpers/git-utils.js index 7240fe1d..ba367358 100644 --- a/test/helpers/git-utils.js +++ b/test/helpers/git-utils.js @@ -195,7 +195,7 @@ export function gitTagHead(tagName, execaOpts) { * @return {String} The sha of the commit associated with `tagName` on the remote repository. */ export async function gitRemoteTagHead(repositoryUrl, tagName, execaOpts) { - return (await execa.stdout('git', ['ls-remote', '--tags', repositoryUrl, tagName], execaOpts)) + return (await execa.stdout('git', ['ls-remote', repositoryUrl, `${tagName}^{}`], execaOpts)) .split('\n') .filter(tag => Boolean(tag)) .map(tag => tag.match(/^(\S+)/)[1])[0];