Skip to content

Commit 6a2afa6

Browse files
committed
Add support for inverting/negating matching
So that the caller can check that the current branch is not an undesired branch. Fixes: #28 Signed-off-by: Kevin Locke <[email protected]>
1 parent 0528438 commit 6a2afa6

File tree

2 files changed

+87
-21
lines changed

2 files changed

+87
-21
lines changed

bin/git-branch-is.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function gitBranchIsCmd(args, callback) {
6464
.option('--git-dir <dir>', 'set the path to the repository')
6565
.option('--git-path <path>', 'set the path to the git binary')
6666
.option('-i, --ignore-case', 'compare/match branch name case-insensitively')
67+
.option('-I, --invert-match', 'inverts/negates comparison')
6768
.option('-q, --quiet', 'suppress warning message if branch differs')
6869
.option('-r, --regex', 'match <branch name> as a regular expression')
6970
.option('-v, --verbose', 'print a message if the branch matches')
@@ -107,32 +108,40 @@ function gitBranchIsCmd(args, callback) {
107108
return;
108109
}
109110

111+
let errMsg, isMatch;
110112
if (expectedBranchRegExp) {
111-
if (!expectedBranchRegExp.test(currentBranch)) {
112-
callback(null, {
113-
code: 1,
114-
stderr: command.quiet ? ''
115-
: `Error: Current branch "${currentBranch}" does not match "${
116-
expectedBranch}".\n`
117-
});
118-
return;
113+
isMatch = expectedBranchRegExp.test(currentBranch);
114+
if (command.invertMatch) {
115+
isMatch = !isMatch;
116+
}
117+
118+
if (!isMatch && !command.quiet) {
119+
errMsg = command.invertMatch
120+
? `Current branch "${currentBranch}" matches "${expectedBranch}".\n`
121+
: `Current branch "${currentBranch}" does not match "${
122+
expectedBranch}".\n`;
123+
}
124+
} else {
125+
isMatch = currentBranch === expectedBranch
126+
|| (command.ignoreCase
127+
&& currentBranch.toUpperCase() === expectedBranch.toUpperCase());
128+
if (command.invertMatch) {
129+
isMatch = !isMatch;
130+
}
131+
132+
if (!isMatch && !command.quiet) {
133+
errMsg = command.invertMatch
134+
? `Current branch is "${currentBranch}".\n`
135+
: `Current branch is "${currentBranch}", not "${expectedBranch}".\n`;
119136
}
120-
} else if (currentBranch !== expectedBranch
121-
&& (!command.ignoreCase
122-
|| currentBranch.toUpperCase() !== expectedBranch.toUpperCase())) {
123-
callback(null, {
124-
code: 1,
125-
stderr: command.quiet ? ''
126-
: `Error: Current branch is "${currentBranch}", not "${
127-
expectedBranch}".\n`
128-
});
129-
return;
130137
}
131138

132139
callback(null, {
133-
code: 0,
134-
stdout: !command.verbose ? ''
135-
: `Current branch is "${currentBranch}".\n`
140+
code: isMatch ? 0 : 1,
141+
stderr: errMsg && `Error: ${errMsg}`,
142+
stdout: isMatch && command.verbose
143+
? `Current branch is "${currentBranch}".\n`
144+
: null
136145
});
137146
});
138147
return undefined;

test/git-branch-is-cmd.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ describe('git-branch-is', () => {
6868
});
6969
});
7070

71+
it('exit code 0 silently for inverted different branch name', (done) => {
72+
const args = ARGS.concat('-I', 'invalid');
73+
gitBranchIsCmd(args, (err, result) => {
74+
assert.ifError(err);
75+
assert.strictEqual(result.code, 0);
76+
assert(!result.stdout);
77+
assert(!result.stderr);
78+
done();
79+
});
80+
});
81+
82+
it('exit code 1 with warning for inverted same branch name', (done) => {
83+
const args = ARGS.concat('-I', BRANCH_CURRENT);
84+
gitBranchIsCmd(args, (err, result) => {
85+
assert.ifError(err);
86+
assert.strictEqual(result.code, 1);
87+
assert(!result.stdout);
88+
assertMatch(result.stderr, BRANCH_CURRENT_RE);
89+
done();
90+
});
91+
});
92+
7193
it('exit 0 silently for matching anchored regex branch name', (done) => {
7294
const args = ARGS.concat('-r', `^${BRANCH_CURRENT}$`);
7395
gitBranchIsCmd(args, (err, result) => {
@@ -137,6 +159,28 @@ describe('git-branch-is', () => {
137159
});
138160
});
139161

162+
it('exit 0 silently for inverted not matching regex branch name', (done) => {
163+
const args = ARGS.concat('-I', '-r', 'invalid');
164+
gitBranchIsCmd(args, (err, result) => {
165+
assert.ifError(err);
166+
assert.strictEqual(result.code, 0);
167+
assert(!result.stdout);
168+
assert(!result.stderr);
169+
done();
170+
});
171+
});
172+
173+
it('exit 1 with warning for inverted match regex branch name', (done) => {
174+
const args = ARGS.concat('-I', '-r', `^${BRANCH_CURRENT}$`);
175+
gitBranchIsCmd(args, (err, result) => {
176+
assert.ifError(err);
177+
assert.strictEqual(result.code, 1);
178+
assert(!result.stdout);
179+
assertMatch(result.stderr, BRANCH_CURRENT_RE);
180+
done();
181+
});
182+
});
183+
140184
it('exit 2 with warning for invalid regex', (done) => {
141185
gitBranchIsCmd(ARGS.concat('-r', 'b[ad'), (err, result) => {
142186
assert.ifError(err);
@@ -295,6 +339,19 @@ describe('git-branch-is', () => {
295339
});
296340
});
297341

342+
// Unlike an commands with expression arguments (e.g. find, test), follow
343+
// the typical argument convention that repeated flag arguments are ignored.
344+
it('does not double-invert', (done) => {
345+
const args = ARGS.concat('-I', '-I', 'invalid');
346+
gitBranchIsCmd(args, (err, result) => {
347+
assert.ifError(err);
348+
assert.strictEqual(result.code, 0);
349+
assert(!result.stdout);
350+
assert(!result.stderr);
351+
done();
352+
});
353+
});
354+
298355
it('returns a Promise with the result', () => {
299356
const promise = gitBranchIsCmd(ARGS.concat(BRANCH_CURRENT));
300357
assert(promise instanceof global.Promise);

0 commit comments

Comments
 (0)