Line data Source code
1 : // Copyright (C) 2022 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.change; 16 : 17 : import com.google.gerrit.index.query.QueryParseException; 18 : import com.google.gerrit.server.index.change.ChangeField; 19 : import dk.brics.automaton.RegExp; 20 : import dk.brics.automaton.RunAutomaton; 21 : 22 : public class RegexMessagePredicate extends ChangeRegexPredicate { 23 : protected final RunAutomaton pattern; 24 : 25 : public RegexMessagePredicate(String re) throws QueryParseException { 26 5 : super(ChangeField.COMMIT_MESSAGE_EXACT, re); 27 : 28 5 : if (re.startsWith("^")) { 29 5 : re = re.substring(1); 30 : } 31 : 32 5 : if (re.endsWith("$") && !re.endsWith("\\$")) { 33 0 : re = re.substring(0, re.length() - 1); 34 : } 35 : 36 : try { 37 4 : this.pattern = new RunAutomaton(new RegExp(re).toAutomaton()); 38 1 : } catch (IllegalArgumentException e) { 39 1 : throw new QueryParseException(String.format("invalid regular expression: %s", re), e); 40 4 : } 41 4 : } 42 : 43 : @Override 44 : public boolean match(ChangeData cd) { 45 2 : return pattern.run(cd.commitMessage()); 46 : } 47 : 48 : @Override 49 : public int getCost() { 50 0 : return 1; 51 : } 52 : }