From 40dc8b9e50b625983a61ac4e65b51463e9431dcc Mon Sep 17 00:00:00 2001 From: Daniel Martini Date: Wed, 21 Dec 2022 15:06:16 -0300 Subject: [PATCH] feat(git): added option to use --exclude option on git describe --- config/release-it.json | 1 + docs/git.md | 11 ++++++++--- docs/pre-releases.md | 6 +++--- lib/plugin/GitBase.js | 3 ++- lib/plugin/GitRelease.js | 2 +- test/git.init.js | 17 +++++++++++++++++ 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/config/release-it.json b/config/release-it.json index 835e3fd3..06a01f46 100644 --- a/config/release-it.json +++ b/config/release-it.json @@ -11,6 +11,7 @@ "commitMessage": "Release ${version}", "commitArgs": [], "tag": true, + "tagExclude": null, "tagName": null, "tagMatch": null, "tagAnnotation": "Release ${version}", diff --git a/docs/git.md b/docs/git.md index 090f9ea8..66f6ecee 100644 --- a/docs/git.md +++ b/docs/git.md @@ -55,13 +55,18 @@ that this represents a glob (not a regex): Example: `git.tagMatch: "[0-9]*\.[0-9]*\.[0-9]*"` -Or only `"[!-]*"`, as this would match everything that excludes a hyphen, which is normally used excusively in -pre-releaseses. - This could also be useful when using a plugin to determine the next tag: Example: `git.tagMatch: "[0-9][0-9].[0-1][0-9].[0-9]*"` +## Tag Exclude + +Use `git.tagExclude` to override the normal behavior to find the latest tag. For example whendoing a major release and +you want to exclude any sort of pre-releases, use `*[-]*`, as this would exclude everything with a hyphen, which is +normally used exclusively in pre-releases. + +Example: `git.tagExclude: *[-]*` + ## Extra arguments In case extra arguments should be provided to Git, these options are available: diff --git a/docs/pre-releases.md b/docs/pre-releases.md index 3bf22fd1..bed98218 100644 --- a/docs/pre-releases.md +++ b/docs/pre-releases.md @@ -43,13 +43,13 @@ release-it major -When all commits since the latest major tag should be added to the changelog, use `--git.tagMatch`: +When all commits since the latest major tag should be added to the changelog, use `--git.tagExclude`: ```bash -release-it major --git.tagMatch='[0-9]*\\.[0-9]*\\.[0-9]*' +release-it major --git.tagExclude='*[-]*' ``` -This will find the latest major matching tag, skipping the pre-release tags. +This will find the latest major matching tag, excluding the pre-release tags, which normally include `-` in their name. Let's go back to when the latest release was `2.0.0-rc.0`. We added new features, which we don't want in the v2 release yet, but instead in a later v2.1. A new pre-release id can be made for the minor release after in `2.1.0-alpha.0`: diff --git a/lib/plugin/GitBase.js b/lib/plugin/GitBase.js index ee8bd053..605def54 100644 --- a/lib/plugin/GitBase.js +++ b/lib/plugin/GitBase.js @@ -93,7 +93,8 @@ class GitBase extends Plugin { getLatestTagName() { const context = Object.assign({}, this.config.getContext(), { version: '*' }); const match = format(this.options.tagMatch || this.options.tagName || '${version}', context); - return this.exec(`git describe --tags --match=${match} --abbrev=0`, { options }).then( + const exclude = this.options.tagExclude ? `--exclude=${format(this.options.tagExclude, context)}` : ''; + return this.exec(`git describe --tags --match=${match} --abbrev=0 ${exclude}`, { options }).then( stdout => stdout || null, () => null ); diff --git a/lib/plugin/GitRelease.js b/lib/plugin/GitRelease.js index 35528849..205a6ffa 100644 --- a/lib/plugin/GitRelease.js +++ b/lib/plugin/GitRelease.js @@ -12,7 +12,7 @@ class GitRelease extends GitBase { getInitialOptions(options) { const baseOptions = super.getInitialOptions(...arguments); const git = options.git || defaultConfig.git; - const gitOptions = _.pick(git, ['tagName', 'tagMatch', 'pushRepo', 'changelog']); + const gitOptions = _.pick(git, ['tagExclude', 'tagName', 'tagMatch', 'pushRepo', 'changelog']); return _.defaults(baseOptions, gitOptions); } diff --git a/test/git.init.js b/test/git.init.js index d1e96b11..da479b71 100644 --- a/test/git.init.js +++ b/test/git.init.js @@ -167,6 +167,23 @@ test.serial('should get the latest tag based on tagMatch', async t => { t.is(gitClient.config.getContext('latestTag'), '21.04.3'); }); +test.serial('should get the latest tag based on tagExclude', async t => { + const shell = factory(Shell); + const gitClient = factory(Git, { + options: { git: { tagExclude: '*[-]*' } }, + container: { shell } + }); + sh.exec('git tag 1.0.0'); + sh.exec('git commit --allow-empty -m "commit 1"'); + sh.exec('git tag 1.0.1-rc.0'); + sh.exec('git tag 1.0.1'); + sh.exec('git commit --allow-empty -m "commit 2"'); + sh.exec('git tag 1.1.0-rc.0'); + sh.exec('git push --tags'); + await gitClient.init(); + t.is(gitClient.config.getContext('latestTag'), '1.0.1'); +}); + test.serial('should generate correct changelog', async t => { const gitClient = factory(Git, { options: { git } }); sh.exec('git tag 1.0.0');