From fb8622b54b373c206790b3927cf3b3d2fcaacf97 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sat, 19 Mar 2022 15:33:11 -0300 Subject: [PATCH] Allow wildcards for required branch(es) (closes #877) --- docs/git.md | 2 +- lib/plugin/git/Git.js | 3 ++- package.json | 1 + test/git.init.js | 13 +++++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/git.md b/docs/git.md index 7f33a357..63f7eb4c 100644 --- a/docs/git.md +++ b/docs/git.md @@ -96,7 +96,7 @@ This is disabled by default, but release-it can exit the process when the curren } ``` -Use an array to allow releases from more branch names. +Use an array to allow releases from more branch names. Wildcards are also allowed (e.g. `release/*`). ### Clean working directory diff --git a/lib/plugin/git/Git.js b/lib/plugin/git/Git.js index eb265055..e5fdca3b 100644 --- a/lib/plugin/git/Git.js +++ b/lib/plugin/git/Git.js @@ -1,6 +1,7 @@ const { EOL } = require('os'); const _ = require('lodash'); const execa = require('execa'); +const matcher = require('wildcard-match'); const { format, e } = require('../../util'); const GitBase = require('../GitBase'); const prompts = require('./prompts'); @@ -94,7 +95,7 @@ class Git extends GitBase { async isRequiredBranch() { const branch = await this.getBranchName(); const requiredBranches = _.castArray(this.options.requireBranch); - return requiredBranches.includes(branch); + return matcher(requiredBranches)(branch); } async hasUpstreamBranch() { diff --git a/package.json b/package.json index 57b1f4fe..77dd0fab 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "update-notifier": "5.1.0", "url-join": "4.0.1", "uuid": "8.3.2", + "wildcard-match": "5.1.2", "yaml": "1.10.2", "yargs-parser": "20.2.9" }, diff --git a/test/git.init.js b/test/git.init.js index b2f48f6a..f5dd4c3d 100644 --- a/test/git.init.js +++ b/test/git.init.js @@ -24,6 +24,19 @@ test.serial('should throw if on wrong branch', async t => { await t.throwsAsync(gitClient.init(), { message: /^Must be on branch dev/ }); }); +test.serial('should not throw if required branch matches', async t => { + const options = { git: { requireBranch: 'ma?*' } }; + const gitClient = factory(Git, { options }); + await t.notThrowsAsync(gitClient.init()); +}); + +test.serial('should not throw if one of required branch matches', async t => { + const options = { git: { requireBranch: ['release/*', 'hotfix/*'] } }; + const gitClient = factory(Git, { options }); + sh.exec('git checkout -b release/v1'); + await t.notThrowsAsync(gitClient.init()); +}); + test.serial('should throw if there is no remote Git url', async t => { const gitClient = factory(Git, { options: { git } }); sh.exec('git remote remove origin');