Skip to content
This repository was archived by the owner on Jan 2, 2020. It is now read-only.

Commit b363962

Browse files
committed
Add support for matching patterns. Such release/* or dev-*.
This allows users to exclude a 'family' of branches without knowing the branch names. For example, allowing only deployment to a staging envrionment from a branch beginning with "release/"
1 parent 5352d46 commit b363962

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"test": "grunt --verbose"
3131
},
3232
"dependencies": {
33-
"shelljs": "0.7.x"
33+
"shelljs": "0.7.x",
34+
"minimatch": "3.0.x"
3435
},
3536
"devDependencies": {
3637
"buster": "0.7.x",

tasks/checkbranch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var shell = require("shelljs");
4+
var minimatch = require("minimatch");
45

56
module.exports = function (grunt) {
67

@@ -25,12 +26,11 @@ module.exports = function (grunt) {
2526
if (e) {
2627
grunt.fail.fatal("Failed to detect the current branch");
2728
}
28-
2929
var branch = branchOutput.trim();
30-
if (!negate && branch !== expectedBranch) {
30+
if (!negate && !minimatch(branch, expectedBranch)) {
3131
grunt.fail.fatal("Only '" + expectedBranch + "' branch is allowed, and you're on '" + branch + "' branch.");
3232
}
33-
else if (negate && branch === expectedBranch) {
33+
else if (negate && minimatch(branch, expectedBranch) ) {
3434
grunt.fail.fatal("Anything except '" + expectedBranch + "' branch is allowed, and you're on '" + branch + "' branch.");
3535
}
3636

test/checkbranch.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,46 @@ buster.testCase("grunt-checkbranch", {
9696
expect(output.stdout).toMatch(GRUNT_FATAL);
9797
expect(output.stdout).toMatch("Failed to detect");
9898
expect(output.code).toEqual(1, "Incorrect grunt output code");
99-
}
99+
},
100+
101+
"should proceed when branch matches pattern": function () {
102+
shell.pushd('tmp');
103+
shell.exec("git checkout -b release/1.2.3");
104+
shell.popd();
105+
106+
var output = execGrunt("checkbranch:release/*");
107+
expect(output.stdout).toMatch(GRUNT_SUCCESS);
108+
expect(output.code).toEqual(0, "Incorrect grunt output code");
109+
},
110+
111+
"should not proceed when branch does not match pattern": function () {
112+
shell.pushd('tmp');
113+
shell.exec("git checkout -b release");
114+
shell.popd();
115+
116+
var output = execGrunt("checkbranch:release/*");
117+
expect(output.stdout).toMatch(GRUNT_FATAL);
118+
expect(output.code).toEqual(1, "Incorrect grunt output code");
119+
},
120+
121+
"should proceed when branch does not match negated pattern": function () {
122+
shell.pushd('tmp');
123+
shell.exec("git checkout -b master");
124+
shell.popd();
125+
126+
var output = execGrunt("checkbranch:!dev-*");
127+
expect(output.stdout).toMatch(GRUNT_SUCCESS);
128+
expect(output.code).toEqual(0, "Incorrect grunt output code");
129+
},
130+
131+
"should not proceed when branch matches negated pattern": function () {
132+
shell.pushd('tmp');
133+
shell.exec("git checkout -b release/1.2.3");
134+
shell.popd();
135+
136+
var output = execGrunt("checkbranch:!release/*");
137+
expect(output.stdout).toMatch(GRUNT_FATAL);
138+
expect(output.code).toEqual(1, "Incorrect grunt output code");
139+
},
100140

101141
});

0 commit comments

Comments
 (0)