Harshad Solanki
Harshad Solanki

Reputation: 93

Regex for filtering

I have installed Git parameter plugin in jenkins. There is an option for filtering branches from Git which needs a regex. So what I get in my branches dropdown is "origin/master". I need a regular expression where I need only word "master" and want to exclude "origin/" from all branches name.

Upvotes: 2

Views: 6854

Answers (3)

mkilmanas
mkilmanas

Reputation: 3485

Well, that's the thing with regexps - they don't quite "exclude" things, they only match (and that match can optionally be used for replace).

So you need to think more about the structure of the branch names and see what rule could describe it best.

#[^/]+$# (# is used as regexp delimiter here; if your system does not support arbitrary delimiters, the equivalent with slashes is /[^\/]+$/ which is harder to read but works just the same) could be one option - it will match everything until the end of the string/line that does not contain slashes. This works as long as your branch names do not contain more slashes (e.g. origin/feature/super-cool-stuff would result in super-cool-stuff where one might actually expect feature/super-cool-stuff).

In other words there is no single correct answer to this without knowing what the branch naming rules are.

Upvotes: 1

VonC
VonC

Reputation: 1326686

You can use the strip option of format:

git for-each-ref --format='%(refname:strip=3)' --shell --no-merged @ refs
# or
git branch -a --format='%(refname:strip=3)'

That way, refs/remotes/origin is not part of the output, and you can grep on your remote branches.

Regarding Jenkins and its branch filtering in the "Branch to Build", you can use:

^master

In order to isolate master only, not origin/master.
You could use a negative lookahead as in this answer to exclude any branch with origin:

:^(?!.*origin/).*$

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521979

If you want to exclude origin/ from the branch name, then you can try only searching for local branches:

git branch | grep master

Note that git branch with no options only searches local branches, -r searches only remote tracking branches, and -a searches everything.

Upvotes: 0

Related Questions