Skip to content

Commit

Permalink
Add support for matching with regular expressions
Browse files Browse the repository at this point in the history
Add the -r, --regex option (using same option name as apropos, killall,
locate, whatis, and others) to treat the argument as a regular
expression against which the current branch name is matched.

Fixes: #22

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
  • Loading branch information
kevinoid committed May 31, 2018
1 parent a39d946 commit b1834f0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,27 @@ the `--help` output:
--git-dir <dir> set the path to the repository
--git-path <path> set the path to the git binary
-q, --quiet suppress warning message if branch differs
-r, --regex match <branch name> as a regular expression
-v, --verbose print a message if the branch matches
-V, --version output the version number

## Additional Command Examples

### Regular Expression Matching

To check that the current branch starts with `release/` using a regular
expression:

```
$ git-branch-is -r "^release/"
Error: Current branch "master" does not match "^release/".
$ echo $?
1
```

Note: Be careful to quote patterns to avoid shell expansion or special
handling (e.g. POSIX shells expand `*` and `cmd.exe` treats `^` specially).

## API Usage

To use the API with a callback function:
Expand Down
28 changes: 27 additions & 1 deletion bin/git-branch-is.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function gitBranchIsCmd(args, callback) {
.option('--git-dir <dir>', 'set the path to the repository')
.option('--git-path <path>', 'set the path to the git binary')
.option('-q, --quiet', 'suppress warning message if branch differs')
.option('-r, --regex', 'match <branch name> as a regular expression')
.option('-v, --verbose', 'print a message if the branch matches')
.version(packageJson.version)
.parse(args);
Expand All @@ -81,13 +82,38 @@ function gitBranchIsCmd(args, callback) {
command.gitArgs = command.gitArg;

var expectedBranch = command.args[0];

var expectedBranchRegExp;
if (command.regex) {
try {
expectedBranchRegExp = new RegExp(expectedBranch);
} catch (errRegExp) {
callback(null, {
code: 2,
stderr: 'Error: Invalid RegExp "' + expectedBranch + '": ' +
errRegExp + '\n'
});
return undefined;
}
}

gitBranchIs.getBranch(command, function(err, currentBranch) {
if (err) {
callback(err);
return;
}

if (currentBranch !== expectedBranch) {
if (expectedBranchRegExp) {
if (!expectedBranchRegExp.test(currentBranch)) {
callback(null, {
code: 1,
stderr: command.quiet ? '' :
'Error: Current branch "' + currentBranch + '" does not match "' +
expectedBranch + '".\n'
});
return;
}
} else if (currentBranch !== expectedBranch) {
callback(null, {
code: 1,
stderr: command.quiet ? '' :
Expand Down
66 changes: 66 additions & 0 deletions test/git-branch-is-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@ describe('git-branch-is', function() {
});
});

it('exit 0 silently for matching anchored regex branch name', function(done) {
var args = ARGS.concat('-r', '^' + BRANCH_CURRENT + '$');
gitBranchIsCmd(args, function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 0);
assert(!result.stdout);
assert(!result.stderr);
done();
});
});

it('exit 0 silently for matching substr regex branch name', function(done) {
var args = ARGS.concat('-r', BRANCH_CURRENT.slice(1, -1));
gitBranchIsCmd(args, function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 0);
assert(!result.stdout);
assert(!result.stderr);
done();
});
});

it('exit 0 silently for matching empty regex branch name', function(done) {
var args = ARGS.concat('-r', '');
gitBranchIsCmd(args, function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 0);
assert(!result.stdout);
assert(!result.stderr);
done();
});
});

it('exit 1 with warning for non-match regex branch name', function(done) {
gitBranchIsCmd(ARGS.concat('-r', 'invalid'), function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 1);
assert(!result.stdout);
assertMatch(result.stderr, /\binvalid\b/);
assertMatch(result.stderr, BRANCH_CURRENT_RE);
done();
});
});

it('exit 2 with warning for invalid regex', function(done) {
gitBranchIsCmd(ARGS.concat('-r', 'b[ad'), function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 2);
assert(!result.stdout);
assertMatch(result.stderr, /\bb\[ad\b/);
done();
});
});

// --quiet does not suppress notification of caller errors
// If this behavior is desired, consider using repeated -q option.
it('exit 2 with warning for invalid regex with quiet', function(done) {
gitBranchIsCmd(ARGS.concat('-q', '-r', 'b[ad'), function(err, result) {
assert.ifError(err);
assert.strictEqual(result.code, 2);
assert(!result.stdout);
assertMatch(result.stderr, /\bb\[ad\b/);
done();
});
});

it('exit code 1 silently with quiet option', function(done) {
var args = ARGS.concat('-q', 'invalid');
gitBranchIsCmd(args, function(err, result) {
Expand Down

0 comments on commit b1834f0

Please sign in to comment.