Skip to content

Commit 78a8812

Browse files
authored
avoid annotations for binary files (#4476)
fixes #4473
1 parent 86b9d98 commit 78a8812

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Locale;
4444
import java.util.Map;
4545
import java.util.Objects;
46+
import java.util.Optional;
4647
import java.util.SortedMap;
4748
import java.util.TreeMap;
4849
import java.util.TreeSet;
@@ -723,8 +724,9 @@ public static void writeDumpedXref(String contextPath,
723724
* Get the genre of a file.
724725
*
725726
* @param file The file to inspect
726-
* @return The genre suitable to decide how to display the file
727+
* @return The genre suitable to decide how to display the file or {@code null} if not found
727728
*/
729+
@Nullable
728730
public static AbstractAnalyzer.Genre getGenre(String file) {
729731
return getGenre(find(file));
730732
}
@@ -733,24 +735,23 @@ public static AbstractAnalyzer.Genre getGenre(String file) {
733735
* Get the genre of a bulk of data.
734736
*
735737
* @param in A stream containing the data
736-
* @return The genre suitable to decide how to display the file
738+
* @return The genre suitable to decide how to display the file or {@code null} if not found
737739
* @throws java.io.IOException If an error occurs while getting the content
738740
*/
741+
@Nullable
739742
public static AbstractAnalyzer.Genre getGenre(InputStream in) throws IOException {
740743
return getGenre(find(in));
741744
}
742745

743746
/**
744747
* Get the genre for a named class (this is most likely an analyzer).
745748
*
746-
* @param factory the analyzer factory to get the genre for
747-
* @return The genre of this class (null if not found)
749+
* @param factory the analyzer factory to get the genre for or {@code null} if not found
750+
* @return The genre of this class (or {@code null} if not found)
748751
*/
752+
@Nullable
749753
public static AbstractAnalyzer.Genre getGenre(AnalyzerFactory factory) {
750-
if (factory != null) {
751-
return factory.getGenre();
752-
}
753-
return null;
754+
return Optional.ofNullable(factory).map(AnalyzerFactory::getGenre).orElse(null);
754755
}
755756

756757
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
import org.jetbrains.annotations.Nullable;
5151
import org.jetbrains.annotations.VisibleForTesting;
52+
import org.opengrok.indexer.analysis.AbstractAnalyzer;
53+
import org.opengrok.indexer.analysis.AnalyzerGuru;
5254
import org.opengrok.indexer.configuration.CommandTimeoutType;
5355
import org.opengrok.indexer.configuration.Configuration;
5456
import org.opengrok.indexer.configuration.Configuration.RemoteSCM;
@@ -258,11 +260,12 @@ private Annotation getAnnotation(File file, @Nullable String rev, boolean fallba
258260

259261
/**
260262
* Annotate given file using repository method. Makes sure that the resulting annotation has the revision set.
261-
* @param file file object to generate the annotaiton for
263+
* @param file file object to generate the annotation for
262264
* @param rev revision to get the annotation for or {@code null} for latest revision of given file
263-
* @return annotation object
265+
* @return annotation object or {@code null}
264266
* @throws IOException on error when getting the annotation
265267
*/
268+
@Nullable
266269
private Annotation getAnnotationFromRepository(File file, @Nullable String rev) throws IOException {
267270
if (!env.getPathAccepter().accept(file)) {
268271
LOGGER.log(Level.FINEST, "file ''{0}'' not accepted for annotation", file);
@@ -681,7 +684,7 @@ public boolean hasHistoryCacheForFile(File file) {
681684
* Check if we can annotate the specified file.
682685
*
683686
* @param file the file to check
684-
* @return <code>true</code> if the file is under version control and the
687+
* @return whether the file is under version control, can be annotated and the
685688
* version control system supports annotation
686689
*/
687690
public boolean hasAnnotation(File file) {
@@ -691,6 +694,16 @@ public boolean hasAnnotation(File file) {
691694
return false;
692695
}
693696

697+
AbstractAnalyzer.Genre genre = AnalyzerGuru.getGenre(file.toString());
698+
if (genre == null) {
699+
LOGGER.log(Level.INFO, "will not produce annotation for ''{0}'' with unknown genre", file);
700+
return false;
701+
}
702+
if (genre.equals(AbstractAnalyzer.Genre.DATA) || genre.equals(AbstractAnalyzer.Genre.IMAGE)) {
703+
LOGGER.log(Level.INFO, "no sense to produce annotation for binary file ''{0}''", file);
704+
return false;
705+
}
706+
694707
Repository repo = getRepository(file);
695708
if (repo == null) {
696709
LOGGER.log(Level.FINEST, "cannot find repository for ''{0}'' to check annotation presence", file);

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialAnnotationParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.history;
2424

@@ -37,18 +37,18 @@
3737
import org.opengrok.indexer.web.Util;
3838

3939
/**
40-
* handles parsing the output of the {@code hg annotate} command
41-
* into an annotation object.
40+
* Handles parsing the output of the {@code hg annotate} command
41+
* into an {@link Annotation} object.
4242
*/
4343
class MercurialAnnotationParser implements Executor.StreamHandler {
4444
private static final Logger LOGGER = LoggerFactory.getLogger(MercurialAnnotationParser.class);
4545

4646
private Annotation annotation = null;
47-
HashMap<String, HistoryEntry> revs;
48-
File file;
47+
private final HashMap<String, HistoryEntry> revs;
48+
private final File file;
4949

5050
/**
51-
* Pattern used to extract author/revision from {@code hg annotate}.
51+
* Pattern used to extract author/revision from the {@code hg annotate} command.
5252
*/
5353
private static final Pattern ANNOTATION_PATTERN = Pattern.compile("^\\s*(\\d+):");
5454

opengrok-indexer/src/main/java/org/opengrok/indexer/history/SCCSRepositoryAnnotationParser.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.history;
2424

@@ -42,18 +42,21 @@ public class SCCSRepositoryAnnotationParser implements Executor.StreamHandler {
4242
private static final Logger LOGGER = LoggerFactory.getLogger(SCCSRepositoryAnnotationParser.class);
4343

4444
/**
45-
* Store annotation created by processStream.
45+
* Store annotation created by {@link #processStream(InputStream)}.
4646
*/
4747
private final Annotation annotation;
4848

4949
private final Map<String, String> authors;
5050

51+
private final File file;
52+
5153
/**
5254
* Pattern used to extract revision from the {@code sccs get} command.
5355
*/
5456
private static final Pattern ANNOTATION_PATTERN = Pattern.compile("^([\\d.]+)\\s+");
5557

5658
SCCSRepositoryAnnotationParser(File file, Map<String, String> authors) {
59+
this.file = file;
5760
this.annotation = new Annotation(file.getName());
5861
this.authors = authors;
5962
}
@@ -69,8 +72,7 @@ public Annotation getAnnotation() {
6972

7073
@Override
7174
public void processStream(InputStream input) throws IOException {
72-
try (BufferedReader in = new BufferedReader(
73-
new InputStreamReader(input))) {
75+
try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) {
7476
String line;
7577
int lineno = 0;
7678
while ((line = in.readLine()) != null) {
@@ -86,8 +88,8 @@ public void processStream(InputStream input) throws IOException {
8688
annotation.addLine(rev, author, true);
8789
} else {
8890
LOGGER.log(Level.SEVERE,
89-
"Error: did not find annotations in line {0}: [{1}]",
90-
new Object[]{lineno, line});
91+
"Error: did not find annotations in line {0} for ''{2}'': [{1}]",
92+
new Object[]{lineno, line, file});
9193
}
9294
}
9395
}

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ private void populateGenreIfEmpty(DiffData data, InputStream[] inArray) {
360360
for (int i = 0; i < 2 && data.genre == null; i++) {
361361
try {
362362
data.genre = AnalyzerGuru.getGenre(inArray[i]);
363+
if (data.genre == null) {
364+
data.errorMsg = "Unable to determine the file type";
365+
}
363366
} catch (IOException e) {
364367
data.errorMsg = "Unable to determine the file type: "
365368
+ Util.htmlize(e.getMessage());

0 commit comments

Comments
 (0)