Skip to content

Commit

Permalink
feat(git): added option to use --exclude option on git describe (#963)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Martini <drmartini@drmartini.local>
  • Loading branch information
drmartini7 and Daniel Martini committed Dec 27, 2022
1 parent 5c02079 commit 2b484bf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions config/release-it.json
Expand Up @@ -11,6 +11,7 @@
"commitMessage": "Release ${version}",
"commitArgs": [],
"tag": true,
"tagExclude": null,
"tagName": null,
"tagMatch": null,
"tagAnnotation": "Release ${version}",
Expand Down
11 changes: 8 additions & 3 deletions docs/git.md
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions docs/pre-releases.md
Expand Up @@ -43,13 +43,13 @@ release-it major

<img src="./assets/release-it-prerelease.gif?raw=true" height="524">

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`:
Expand Down
3 changes: 2 additions & 1 deletion lib/plugin/GitBase.js
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/GitRelease.js
Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions test/git.init.js
Expand Up @@ -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');
Expand Down

0 comments on commit 2b484bf

Please sign in to comment.