Support searching changes by file extension

Change-Id: I76319392dfa91e18ad4be6d9673951d03aa68a00
This commit is contained in:
Dave Borowitz 2019-01-11 11:42:56 -08:00
parent 6205cad8aa
commit e93969399a
7 changed files with 105 additions and 1 deletions

View File

@ -289,6 +289,14 @@ named exactly `Foo.java` and does not match `AbstractFoo.java`.
Regular expression matching can be enabled by starting the string
with `^`. In this mode `file:` is an alias of `path:` (see above).
[[extension]]
extension:'EXT', ext:'EXT'::
+
Matches any change touching a file with extension 'EXT', case-insensitive. The
extension is defined as the portion of the filename following the final `.`.
Files with no `.` in their name have no extension and cannot be matched with
this operator; use `file:` instead.
[[star]]
star:'LABEL'::
+

View File

@ -37,6 +37,7 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.Files;
import com.google.common.primitives.Longs;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.common.data.SubmitRequirement;
@ -185,6 +186,26 @@ public class ChangeField {
public static final FieldDef<ChangeData, Iterable<String>> FILE_PART =
exact(ChangeQueryBuilder.FIELD_FILEPART).buildRepeatable(ChangeField::getFileParts);
/** File extensions of each file modified in the current patch set. */
public static final FieldDef<ChangeData, Iterable<String>> EXTENSION =
exact(ChangeQueryBuilder.FIELD_EXTENSION).buildRepeatable(ChangeField::getExtensions);
public static Set<String> getExtensions(ChangeData cd) throws OrmException {
try {
return cd.currentFilePaths()
.stream()
// Use case-insensitive file extensions even though other file fields are case-sensitive.
// If we want to find "all Java files", we want to match both .java and .JAVA, even if we
// normally care about case sensitivity. (Whether we should change the existing file/path
// predicates to be case insensitive is a separate question.)
.map(f -> Files.getFileExtension(f).toLowerCase(Locale.US))
.filter(e -> !e.isEmpty())
.collect(toSet());
} catch (IOException e) {
throw new OrmException(e);
}
}
/** Owner/creator of the change. */
public static final FieldDef<ChangeData, Integer> OWNER =
integer(ChangeQueryBuilder.FIELD_OWNER).build(changeGetter(c -> c.getOwner().get()));

View File

@ -101,7 +101,9 @@ public class ChangeSchemaDefinitions extends SchemaDefinitions<ChangeData> {
// Bump Lucene version requires reindexing
@Deprecated static final Schema<ChangeData> V50 = schema(V49);
static final Schema<ChangeData> V51 = schema(V50, ChangeField.TOTAL_COMMENT_COUNT);
@Deprecated static final Schema<ChangeData> V51 = schema(V50, ChangeField.TOTAL_COMMENT_COUNT);
static final Schema<ChangeData> V52 = schema(V51, ChangeField.EXTENSION);
public static final String NAME = "changes";
public static final ChangeSchemaDefinitions INSTANCE = new ChangeSchemaDefinitions();

View File

@ -137,6 +137,7 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
public static final String FIELD_COMMIT = "commit";
public static final String FIELD_COMMITTER = "committer";
public static final String FIELD_EXACTCOMMITTER = "exactcommitter";
public static final String FIELD_EXTENSION = "extension";
public static final String FIELD_CONFLICTS = "conflicts";
public static final String FIELD_DELETED = "deleted";
public static final String FIELD_DELTA = "delta";
@ -732,6 +733,16 @@ public class ChangeQueryBuilder extends QueryBuilder<ChangeData> {
return new EqualsPathPredicate(FIELD_PATH, path);
}
@Operator
public Predicate<ChangeData> ext(String ext) {
return extension(ext);
}
@Operator
public Predicate<ChangeData> extension(String ext) {
return new FileExtensionPredicate(ext);
}
@Operator
public Predicate<ChangeData> label(String name)
throws QueryParseException, OrmException, IOException, ConfigInvalidException {

View File

@ -0,0 +1,42 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.query.change;
import com.google.gerrit.server.index.change.ChangeField;
import com.google.gwtorm.server.OrmException;
import java.util.Locale;
public class FileExtensionPredicate extends ChangeIndexPredicate {
private static String clean(String ext) {
if (ext.startsWith(".")) {
ext = ext.substring(1);
}
return ext.toLowerCase(Locale.US);
}
FileExtensionPredicate(String value) {
super(ChangeField.EXTENSION, clean(value));
}
@Override
public boolean match(ChangeData object) throws OrmException {
return ChangeField.getExtensions(object).contains(value);
}
@Override
public int getCost() {
return 0;
}
}

View File

@ -1366,6 +1366,24 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
assertQuery("path:^dir.file.*", change);
}
@Test
public void byExtension() throws Exception {
assume().that(getSchema().hasField(ChangeField.EXTENSION)).isTrue();
TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(repo, newChangeWithFiles(repo, "foo.h", "foo.cc"));
Change change2 = insert(repo, newChangeWithFiles(repo, "bar.H", "bar.CC"));
Change change3 = insert(repo, newChangeWithFiles(repo, "dir/baz.h", "dir/baz.cc"));
Change change4 = insert(repo, newChangeWithFiles(repo, "Quux.java"));
assertQuery("extension:java", change4);
assertQuery("ext:java", change4);
assertQuery("ext:.java", change4);
assertQuery("ext:jAvA", change4);
assertQuery("ext:.jAvA", change4);
assertQuery("ext:cc", change3, change2, change1);
}
@Test
public void byComment() throws Exception {
TestRepository<Repo> repo = createProject("repo");

View File

@ -36,6 +36,8 @@
'conflicts:',
'deleted:',
'delta:',
'ext:',
'extension:',
'file:',
'from:',
'has:',